1.引入
通过上一个内容,我们对resultMap有了一个基本的认识。同时我们封装的情况是属于一对一的一个情况。但是呢,有时候我们也会遇到一对多的情况。那么接下来我们一起来看一下如何处理一对多的情况。
2.使用resultMap处理一对多的情况(Collection)
(1).定义相关的实体对象
//部门实体对象:一个部门对应多个员工
public class Department {
private Integer id;
private String departmentName;
private List<Employee> emps;
}
(2).编写查询接口以及相关查询方法
public interface DepartmentMapper {
public Department getDeptByIdPlus(Integer id);
}
(3).在映射文件中编写对应的配置内容信息,这一个时候我们使用resultMap进行自定义封装
<!--嵌套结果集的方式,使用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>
(4).查询操作测试
DepartmentMapper mapper = openSession.getMapper(DepartmentMapper.class);
Department department = mapper.getDeptByIdPlus(1);
System.out.println(department);
System.out.println(department.getEmps());
总结:
1.注意使用collection定义关联集合类型的属性的封装规则 。其实就是嵌套的定义。
3.使用resultMap处理一对多的情况,使用分步查询以及延时加载
(1).编写查询接口以及相关查询方法
①:按照编号id查询部门相关的信息内容
//使用resultMap封装Collection,使用分步查询以及延时加载
public interface DepartmentMapper {
public Department getDeptByIdStep(Integer id);
}
②:按照部门编号id查询员工相关信息
//按照部门编号id查询出对应的员工信息
public interface EmployeeMapperPlus {
public List<Employee> getEmpsByDeptId(Integer deptId);
}
(2).在映射文件中编写对应的配置内容信息
①:在部门表映射文件中配置分步查询内容
<!-- collection:分段查询 -->
<resultMap type="com.atguigu.mybatis.bean.Department" id="MyDeptStep">
<id column="id" property="id"/>
<id column="dept_name" property="departmentName"/>
<collection property="emps"
select="com.atguigu.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"
column="id" fetchType="lazy">
</collection>
</resultMap>
<!-- public Department getDeptByIdStep(Integer id); -->
<select id="getDeptByIdStep" resultMap="MyDeptStep">
select id,dept_name from tbl_dept where id=#{id}
</select>
②:在员工映射文件中配置分步查询内容
<select id="getEmpsByDeptId" resultType="com.atguigu.mybatis.bean.Employee">
select * from tbl_employee where d_id=#{deptId}
</select>
(3).查询操作测试
DepartmentMapper mapper = openSession.getMapper(DepartmentMapper.class);
Department deptByIdStep = mapper.getDeptByIdStep(1);
System.out.println(deptByIdStep.getDepartmentName());
System.out.println(deptByIdStep.getEmps());
总结:
1.注意俩个映射文件中的方法相互调用:
4.在3的基础上,我们做一个改进,如果我们需要传递多个值,如何处理
<!-- 扩展:多列的值传递过去:
将多列的值封装map传递;
column="{key1=column1,key2=column2}"
fetchType="lazy":表示使用延迟加载,该属性也可以针对单个做延迟加载,之前是在全局配置文
件中做的设置;
- lazy:延迟
- eager:立即
-->
调整如下: