假设每个学生都有一名指导老师,本示例的任务就是查询出学生的详细信息,这就包括学生的指导老师的信息。具体代码如下:
1、表的结构
DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `id` int(11) NOT NULL auto_increment, `name` varchar(10) NOT NULL, `gender` char(1) NOT NULL, `major` varchar(20) NOT NULL, `grade` char(4) NOT NULL, `supervisor_id` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of student -- ---------------------------- INSERT INTO `student` VALUES ('1', '李林', '男', '计算机科学与技术', '2011', '1'); -- ---------------------------- -- Table structure for teacher -- ---------------------------- DROP TABLE IF EXISTS `teacher`; CREATE TABLE `teacher` ( `id` int(11) NOT NULL auto_increment, `name` varchar(10) NOT NULL, `gender` char(1) NOT NULL, `research_area` varchar(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of teacher -- ---------------------------- INSERT INTO `teacher` VALUES ('1', '张伟', '男', '软件工程');
2、对应的JavaBean类
public class Student { private int id; private String name; // 姓名 private String gender; // 性别 private String major; // 专业 private String grade; // 年级 private Teacher supervisor; //指导教师 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } public String getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; } public Teacher getSupervisor() { return supervisor; } public void setSupervisor(Teacher supervisor) { this.supervisor = supervisor; } } public class Teacher { private int id; private String name; // 教师姓名 private String gender; // 教师性别 private String researchArea; // 研究领域 private List<Student> supStudents; // 指导学生 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getResearchArea() { return researchArea; } public void setResearchArea(String researchArea) { this.researchArea = researchArea; } public List<Student> getSupStudents() { return supStudents; } public void setSupStudents(List<Student> supStudents) { this.supStudents = supStudents; } }
3、配置StudentMapper.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="org.zsm.myBatis.day02.inner.IStudentOperation"> <!-- 定义java Bean的属性与数据库的列之间的映射 --> <resultMap type="Student" id="studentResultMap"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="gender" property="gender" /> <result column="major" property="major" /> <result column="grade" property="grade"/> <!-- association处理has-a关系 --> <!--注意:这种方式javaType必须指定,表示supervisor的类型是Teacher,否则会报错 --> <association property="supervisor" javaType="Teacher"> <!-- 教师自身的属性与数据库字段的映射 --> <id property="id" column="t_id"/> <result property="name" column="t_name"/> <result property="gender" column="t_gender"/> <result property="researchArea" column="research_area"/> </association> </resultMap> <!-- SQL语句中以"#{}"的形式引用参数 --> <select id="getById" parameterType="int" resultMap="studentResultMap"> SELECT st.id,st.name,st.gender,st.major,st.grade,t.id t_id,t.name t_name, t.gender t_gender,t.research_area FROM student st, teacher t WHERE st.supervisor_id = t.id AND st.id=#{id} </select> </mapper>
3、测试
public void testAssociation(){ SqlSession session = sqlSessionFactory.openSession(); IStudentOperation studentOperation = session.getMapper(IStudentOperation.class); Student student = studentOperation.getById(1); System.out.println(student.getName()+"的指导老师是:"+student.getSupervisor().getName()); }
myBatis association的两种形式
一、嵌套的resultMap
这种方法的本质就是把教师实体映射从association元素中提取出来,用一个resultMap元素表示。然后association元素再引用这个resultMap元素。
<?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="org.zsm.myBatis.day02.inner.IStudentOperation"> <!-- 定义java Bean的属性与数据库的列之间的映射 --> <resultMap type="Teacher" id="teacherResultMap"> <id property="id" column="t_id"/> <result property="name" column="t_name"/> <result property="gender" column="t_gender"/> <result property="researchArea" column="research_area"/> </resultMap> <resultMap type="Student" id="studentResultMap"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="gender" property="gender" /> <result column="major" property="major" /> <result column="grade" property="grade"/> <!-- 引用teacherResultMap --> <association property="supervisor" resultMap="teacherResultMap"/> </resultMap> <!-- SQL语句中以"#{}"的形式引用参数 --> <select id="getById" parameterType="int" resultMap="studentResultMap"> SELECT st.id,st.name,st.gender,st.major,st.grade,t.id t_id,t.name t_name, t.gender t_gender,t.research_area FROM student st, teacher t WHERE st.supervisor_id = t.id AND st.id=#{id} </select> </mapper>
二、嵌套的select语句
这种方式是使用一条单独的select语句来加载关联的实体(本例中就是教师实体),然后在association元素中引用此select语句(注:此方法会产生N+1问题,尽量不要用嵌套的select语句)
<?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="org.zsm.myBatis.day02.inner.IStudentOperation"> <!-- 定义java Bean的属性与数据库的列之间的映射 --> <resultMap type="Teacher" id="supervisorResultMap"> <id property="id" column="t_id"/> <result property="name" column="t_name"/> <result property="gender" column="t_gender"/> <result property="researchArea" column="research_area"/> </resultMap> <resultMap type="Student" id="studentResultMap"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="gender" property="gender" /> <result column="major" property="major" /> <result column="grade" property="grade"/> <!-- 引用teacherResultMap --> <association property="supervisor" column="supervisor_id" select="selectSupervisor"/> </resultMap> <!-- SQL语句中以"#{}"的形式引用参数 --> <select id="getById" parameterType="int" resultMap="studentResultMap"> select id,name,gender,major,grade,supervisor_id from student where id =#{id} </select> <select id="selectSupervisor" parameterType="int" resultMap="supervisorResultMap"> select id,name,gender,research_area from teacher where id = #{id} </select> </mapper>