关联查询包括以下四种方式:
1)一对一关联查询
2)一对多关联查询
3)多对一关联查询
4)多对多关联查询
接下来了解下一对一关联查询
实体类:
package com.mybatisdemo.beans; public class Clazz { private Integer id; private String className; private HeadTeacher headTeacher; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public HeadTeacher getHeadTeacher() { return headTeacher; } public void setHeadTeacher(HeadTeacher headTeacher) { this.headTeacher = headTeacher; } @Override public String toString() { return "Clazz [id=" + id + ", className=" + className + ", headTeacher=" + headTeacher + "]"; } }
package com.mybatisdemo.beans; public class HeadTeacher { private Integer id; private String name; // private Clazz clazz; //这边不引用,防止关联查询导致死循环 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "HeadTeacher [id=" + id + ", name=" + name + "]"; } }
Dao层对应的映射类Mapper:
<?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.mybatisdemo.dao.ClazzDao"> <resultMap type="com.mybatisdemo.beans.Clazz" id="clazzMapper"> <id column="id" property="id"/> <result column="class_name" property="className"/> <!-- <association property="headTeacher" javaType="com.mybatisdemo.beans.HeadTeacher"> <id column="id" property="id"/> <result column="name" property="name"/> </association> --> <association property="headTeacher" javaType="com.mybatisdemo.beans.HeadTeacher" column="id" select="com.mybatisdemo.dao.HeaderTeacherDao.queryHeadTeacherById"/> </resultMap> <select id="queryClazzById" parameterType="int" resultMap="clazzMapper"> select id,class_name,head_teacher_id from clazz where id = #{id} </select> </mapper>
<?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.mybatisdemo.dao.HeaderTeacherDao"> <resultMap type="com.mybatisdemo.beans.HeadTeacher" id="headTeacherMapper"> <id column="id" property="id"/> <result column="name" property="name"/> </resultMap> <select id="queryHeadTeacherById" parameterType="int" resultMap="headTeacherMapper"> select id,name from head_teacher where id = #{id} </select> </mapper>
测试:
@Test public void testClazz() { ClazzDao clazzDao = sqlSession.getMapper(ClazzDao.class); Clazz clazz = clazzDao.queryClazzById(1); System.out.println(clazz); }
PS:表结构
create table if not exists head_teacher( id int(10) not null primary key, name varchar(25) not null ) charset utf8; create table if not exists clazz ( id int(10) not null primary key, class_name varchar(25) not null unique, head_teacher_id int(1), foreign key(head_teacher_id) references head_teacher(id) ) charset utf8;