这篇文章主要讲述Mybatis多表联查xml当中的一些实现方式,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
目录
一对一的场景
也就是对象里面套了一个对象,一般用于学生和班级,或者员工和部门的关系等等。
此处省略了get和set方法等等,主要看重点即可。
Employee类
public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
private Integer dId; //Department关联主键id
private Department dept;
}
Department 类
public class Department {
private Integer id;
private String departmentName;
}
1、resultMap封装对象
column是数据库名称,property就是java属性名称
dept.id,也就是子对象的java属性名称
<resultMap type="com.gzl.mybatis.bean.Employee" id="MyDifEmp">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<result column="did" property="dept.id"/>
<result column="dept_name" property="dept.departmentName"/>
</resultMap>
<select id="getEmpAndDept" resultMap="MyDifEmp">
SELECT e.id id,e.last_name last_name,e.email email,e.gender gender,e.d_id d_id,
d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
WHERE e.d_id=d.id AND e.id=#{id}
</select>
2、resultMap配合association封装对象
<resultMap type="com.gzl.mybatis.bean.Employee" id="MyDifEmp2">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<!-- association可以指定联合的javaBean对象
property="dept":指定哪个属性是联合的对象
javaType:指定这个属性对象的类型[不能省略]
-->
<association property="dept" javaType="com.gzl.mybatis.bean.Department">
<id column="did" property="id"/>
<result column="dept_name" property="departmentName"/>
</association>
</resultMap>
<select id="getEmpAndDept" resultMap="MyDifEmp2">
SELECT e.id id,e.last_name last_name,e.email email,e.gender gender,e.d_id d_id,
d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
WHERE e.d_id=d.id AND e.id=#{id}
</select>
3、resultMap配合collection封装对象
property=“dept”:指定哪个属性是联合的对象
ofType:指定这个属性对象的类型[不能省略]
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp3">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<collection property="dept" ofType="com.atguigu.mybatis.bean.Department">
<!-- 定义这个集合中元素的封装规则 -->
<id column="did" property="id"/>
<result column="dept_name" property="departmentName"/>
</collection>
</resultMap>
<!-- public Employee getEmpAndDept(Integer id);-->
<select id="getEmpAndDept" resultMap="MyDifEmp3">
SELECT e.id id,e.last_name last_name,e.email email,e.gender gender,e.d_id d_id,
d.id did,d.dept_name dept_name FROM tbl_employee e,tbl_dept d
WHERE e.d_id=d.id AND e.id=#{id}
</select>
一对多的场景
也就是对象里面套了一个List,一般用于根据班级查询学生等等。。
Department 类
public class Department {
private Integer id;
private String departmentName;
private List<Employee> emps;
}
Employee 类
public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
}
resultMap配合collection封装对象
<!--嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则 -->
<resultMap type="com.atguigu.mybatis.bean.Department" id="MyDept">
<id column="did" property="id"/>
<result column="dept_name" property="departmentName"/>
<!--
collection定义关联集合类型的属性的封装规则
ofType:指定集合里面元素的类型
-->
<collection property="emps" ofType="com.atguigu.mybatis.bean.Employee">
<!-- 定义这个集合中元素的封装规则 -->
<id column="eid" property="id"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
</collection>
</resultMap>
<!-- public Department getDeptByIdPlus(Integer id); -->
<select id="getDeptByIdPlus" resultMap="MyDept">
SELECT d.id did,d.dept_name dept_name,
e.id eid,e.last_name last_name,e.email email,e.gender gender
FROM tbl_dept d
LEFT JOIN tbl_employee e
ON d.id=e.d_id
WHERE d.id=#{id}
</select>
测试
这是数据库当中执行得出的结果,也就是一对多查出来的两条
collection会帮我们把两条封装到父对象当中。
总结
collection可以用来接单个对象嵌套,也可以接对象里面套list
association只可以接单个对象
平时我们返回结果集对象类型都是用resultType,当我们用自定义的时候需要用resultMap