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

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;
    //部门Bean
    private 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 gender
        FROM t_dept d LEFT JOIN t_employee e
        ON d.id=e.d_id
        WHERE 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 gender
        FROM t_dept d LEFT JOIN t_employee e
        ON d.id=e.d_id
        WHERE 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 ":表示使用立即加载;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值