classes和teacher为一对一关联
classes和student为一对多关联
classes sql
CREATE TABLE `NewTable` ( `class_id` int(11) NOT NULL AUTO_INCREMENT , `class_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , `class_year` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , `teacher_id` int(11) NULL DEFAULT NULL , PRIMARY KEY (`class_id`) ) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=2 ROW_FORMAT=COMPACT ;
teacher sql
CREATE TABLE `NewTable` ( `teacher_id` int(11) NOT NULL AUTO_INCREMENT , `teacher_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , `teacher_sex` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , `teacher_birthday` date NULL DEFAULT NULL , `work_date` int(11) NULL DEFAULT NULL , `professional` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , PRIMARY KEY (`teacher_id`) ) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=2 ROW_FORMAT=COMPACT ;
student sql
CREATE TABLE `NewTable` ( `student_id` int(11) NOT NULL AUTO_INCREMENT , `student_name` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , `student_sex` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , `student_birthday` date NULL DEFAULT NULL , `class_id` int(11) NULL DEFAULT NULL , PRIMARY KEY (`student_id`) ) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=3 ROW_FORMAT=COMPACT ;
classes
package com.mybatis.model; import java.util.List; public class classes { private Integer class_id; private String class_name; private String class_year; private Integer teacher_id; private teacher teacher; private List<student> studentList; public List<student> getStudentList() { return studentList; } public void setStudentList(List<student> studentList) { this.studentList = studentList; } public teacher getTeacher() { return teacher; } public void setTeacher(teacher teacher) { this.teacher = teacher; } public Integer getClass_id() { return class_id; } public void setClass_id(Integer class_id) { this.class_id = class_id; } public String getClass_name() { return class_name; } public void setClass_name(String class_name) { this.class_name = class_name == null ? null : class_name.trim(); } public String getClass_year() { return class_year; } public void setClass_year(String class_year) { this.class_year = class_year == null ? null : class_year.trim(); } public Integer getTeacher_id() { return teacher_id; } public void setTeacher_id(Integer teacher_id) { this.teacher_id = teacher_id; } }
teacher
package com.mybatis.model; import java.util.Date; public class teacher { private Integer teacher_id; private String teacher_name; private String teacher_sex; private Date teacher_birthday; private Integer work_date; private String professional; public teacher() { } public Integer getTeacher_id() { return teacher_id; } public void setTeacher_id(Integer teacher_id) { this.teacher_id = teacher_id; } public String getTeacher_name() { return teacher_name; } public void setTeacher_name(String teacher_name) { this.teacher_name = teacher_name == null ? null : teacher_name.trim(); } public String getTeacher_sex() { return teacher_sex; } public void setTeacher_sex(String teacher_sex) { this.teacher_sex = teacher_sex == null ? null : teacher_sex.trim(); } public Date getTeacher_birthday() { return teacher_birthday; } public void setTeacher_birthday(Date teacher_birthday) { this.teacher_birthday = teacher_birthday; } public Integer getWork_date() { return work_date; } public void setWork_date(Integer work_date) { this.work_date = work_date; } public String getProfessional() { return professional; } public void setProfessional(String professional) { this.professional = professional == null ? null : professional.trim(); } }
注意:teacher类中有一个无参构造,若一对一关联查询时候,被查询类缺少无参构造会报错
student
package com.mybatis.model; import java.util.Date; public class student { private Integer student_id; private String student_name; private String student_sex; private Date student_birthday; private Integer class_id; public student() { super(); } public Integer getStudent_id() { return student_id; } public void setStudent_id(Integer student_id) { this.student_id = student_id; } public String getStudent_name() { return student_name; } public void setStudent_name(String student_name) { this.student_name = student_name == null ? null : student_name.trim(); } public String getStudent_sex() { return student_sex; } public void setStudent_sex(String student_sex) { this.student_sex = student_sex == null ? null : student_sex.trim(); } public Date getStudent_birthday() { return student_birthday; } public void setStudent_birthday(Date student_birthday) { this.student_birthday = student_birthday; } public Integer getClass_id() { return class_id; } public void setClass_id(Integer class_id) { this.class_id = class_id; } }
classesMap
package com.mybatis.dao; import org.springframework.stereotype.Repository; import com.mybatis.model.classes; @Repository public interface classesMapper { classes getClass(Integer class_id); }
teacherMap
package com.mybatis.dao; import org.springframework.stereotype.Repository; import com.mybatis.model.teacher; @Repository public interface teacherMapper { teacher getTeacher(Integer teacher_id); }
studentMap
package com.mybatis.dao; import java.util.List; import org.springframework.stereotype.Repository; import com.mybatis.model.student; @Repository public interface studentMapper { List<student>getStudentByClassID(); }
classesMap.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.mybatis.dao.classesMapper" > <resultMap id="ClassesResultMap" type="com.mybatis.model.classes" > <id column="class_id" property="class_id" jdbcType="INTEGER" /> <result column="class_name" property="class_name" jdbcType="VARCHAR" /> <result column="class_year" property="class_year" jdbcType="VARCHAR" /> <result column="teacher_id" property="teacher_id" jdbcType="INTEGER" /> <association property="teacher" column="teacher_id" select="com.mybatis.dao.teacherMapper.getTeacher" /> <collection property="studentList" column="class_id" javaType="ArrayList" ofType="com.mybatis.model.student" select="com.mybatis.dao.studentMapper.getStudentByClassID"/> </resultMap> <select id="getClasses" resultMap="ClassesResultMap" parameterType="java.lang.Integer" > select * from classes where class_id = #{class_id} </select> </mapper>
注意:一对一用association,一对多用collection,当中ofType和select指定包名方法
teacherMap.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.mybatis.dao.teacherMapper" > <resultMap id="TeacherResultMap" type="com.mybatis.model.teacher" > <id column="teacher_id" property="teacher_id" jdbcType="INTEGER" /> <result column="teacher_name" property="teacher_name" jdbcType="VARCHAR" /> <result column="teacher_sex" property="teacher_sex" jdbcType="VARCHAR" /> <result column="teacher_birthday" property="teacher_birthday" jdbcType="DATE" /> <result column="work_date" property="work_date" jdbcType="INTEGER" /> <result column="professional" property="professional" jdbcType="VARCHAR" /> </resultMap> <select id="getTeacher" resultMap="TeacherResultMap" parameterType="java.lang.Integer" > select * from teacher where teacher_id = #{teacher_id} </select> </mapper>
注意 2个xml的resultMap id不要重复,不然可能报错
studentMap.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.mybatis.dao.studentMapper" > <resultMap id="StudentResultMap" type="com.mybatis.model.student" > <id column="student_id" property="student_id" jdbcType="INTEGER" /> <result column="student_name" property="student_name" jdbcType="VARCHAR" /> <result column="student_sex" property="student_sex" jdbcType="VARCHAR" /> <result column="student_birthday" property="student_birthday" jdbcType="DATE" /> <result column="class_id" property="class_id" jdbcType="INTEGER" /> </resultMap> <select id="getStudentByClassID" parameterType="int" resultMap="StudentResultMap"> select * from student where student.class_id = #{class_id} </select> </mapper>
testController
package com.mybatis.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.mybatis.model.classes; import com.mybatis.model.student; @Controller public class TestController { @Autowired private com.mybatis.dao.classesMapper classesMapper; @RequestMapping(value = "/nv/test1" ,method=RequestMethod.GET) public ResponseEntity<?> indexPage() { classes classes=classesMapper.getClasses(1); List<student>students=classes.getStudentList(); for (student student : students) { System.out.println(student.getStudent_name()); } System.out.println(classes.getClass_name()); System.out.println(classes.getTeacher().getTeacher_name()); System.out.println(classes.getClass_name()); return new ResponseEntity<String>(HttpStatus.OK); } }