当前位置: 代码迷 >> 综合 >> mybatis中association和collection标签的分步执行和懒加载
  详细解决方案

mybatis中association和collection标签的分步执行和懒加载

热度:81   发布时间:2023-10-20 23:10:39.0

association和collection是mybatis sql映射文件中封装结果集的两个标签

association标签

1、简单级联封装Bean

类似于一个员工对应一个部门的pojo封装

public class Employee {private Integer id;private String lastName;private char gender;private String email;private String did;//部门Beanprivate Department dept;
...

我们在映射文件中封装结果集时怎么封装dept这个属性呢?显然使用resultType不能直接将结果集封装到dept,所以我们使用resultMap来进行结果集重新封装。

<resultMap id="myMap01" type="cn.itsqq.domain.Employee"><id column="id" property="id"/><result column="last_name" property="lastName"/><result column="gender" property="gender"/><result column="email" property="email"/><result column="d_id" property="did"/><!--通过association级联数据库表--><association property="dept" javaType="cn.itsqq.domain.Department"><result column="did" property="id"/><result column="dept_name" property="dept_name"/></association></resultMap>

一步查询

<select id="getDeptByIdPlus"   resultMap="myMap01">SELECT d.id id,d.deptname deptname,e.id eid,e.last_name lastname,e.email email,e.gender genderFROM t_dept d LEFT JOIN t_employee eON d.id=e.d_idWHERE d.id=#{id}
</select>
2、association 分步查询思路
-- 先查出所有的员工信息
SELECT * FROM  t_employee WHERE id=#{id}
-- 再根据查出来的员工信息中的部门id查询对应的部门信息
SELECT * FROM  t_dept WHERE id=#{id}

使用association进行分步执行

<resultMap id="myDefMap" type="cn.itsqq.domain.Employee"><id column="id" property="id"/><result column="last_name" property="lastName"/><result column="gender" property="gender"/><result column="email" property="email"/><result column="d_id" property="did"/><!-- 使用association进行分步查询1、先按照员工id查询员工信息2、根据查询员工信息中的d_id值去部门表查出部门信息3、部门设置到员工中;association定义关联对象的封装规则select:表明当前属性是调用select指定的方法查出的结果column:指定将哪一列的值传给这个方法(数据库列名不是封装数据的属性名)--><association property="dept" select="cn.itsqq.dao.EmployeeDeptMapper.getDeptById" column="d_id"><id column="id" property="id"/><!--属性名和列名最好不要用驼峰命名法"dept_name" 否则查出来的是null--><result column="deptname" property="deptname"/></association></resultMap>

collection标签

1、简单级联封装Bena中集合(List,Map)属性
public class Department {private Integer id;private String deptname;
//集合属性private List<Employee> emps;//部门里面有很多的员工一对多
...
}

使用collection标签一步执行

<resultMap id="myMap01" type="cn.itsqq.domain.Department"><id column="id" property="id"/><result column="deptname" property="deptname"/><!--ofType:需要封装的对象 --><collection property="emps" ofType="cn.itsqq.domain.Employee"><id column="eid" property="id"/><result column="lastname" property="lastName"/><result column="email" property="email"/><result column="gender" property="gender"/></collection></resultMap>

关联查询

 <select id="getDeptByIdPlus"   resultMap="myMap01">SELECT d.id id,d.deptname deptname,e.id eid,e.last_name lastname,e.email email,e.gender genderFROM t_dept d LEFT JOIN t_employee eON d.id=e.d_idWHERE d.id=#{id}</select>
2、collection标签进行多步执行思路
-- 先查出所有的部门信息
SELECT * FROM t_dept  WHERE id=#{id}
-- 再根据查出来的对应部门信息中的部门id查询对应的员工信息
SELECT * FROM  t_employee  WHERE id=#{id}

使用collection标签进行多步执行

 <resultMap id="myMap01" type="cn.itsqq.domain.Department"><id column="id" property="id"/><result column="deptname" property="deptname"/><collection property="emps" select="cn.itsqq.dao.EmployeeMapper.getEmpByDid" column="id" fetchType="lazy"><id column="id" property="id"/><result column="last_name" property="lastName"/><result column="gender" property="gender"/><result column="email" property="email"/><result column="d_id" property="did"/></collection><!--扩展:多列的值传递过去:将多列的值封装map传递;column="{key1=column1,key2=column2}"fetchType="lazy":表示使用延迟加载;- lazy :延迟- eager : 立即--></resultMap>

懒加载操作

在我们进行多步执行的时候,我们会执行两次sql操作,如果我们每次执行都执行两次会消耗资源,所以在我们需要进行第二步执行sql的时候再去执行就减少了资源的浪费。懒加载应运而生。
1、第一种
我们需要在mybatis-config.xml中配置settings标签,这样我们就开启了懒加载操作

<settings><!--显示的指定每个我们需要更改的配置的值,即使是默认的。防止版本更新带来的问题--><!--association进行分步查询时进行懒加载操作--><setting name="lazyLoadingEnabled" value="true"/><setting name="aggressiveLazyLoading" value="false"/>
</settings>

2、第二种
collection标签中我们可以设置fetchType参数来进行懒加载的控制
fetchType="lazy":表示使用延迟加载; fetchType="eager ":表示使用立即加载;

  相关解决方案