延迟加载基本概念

上面我们已经知道使用association、collection可以实现一对一及一对多映射,association、collection还有另外一个延迟加载的功能。

延迟加载(lazy load)是关联对象默认的加载方式,延迟加载机制是为了避免一些无谓的性能开销而提出来的,所谓延迟加载就是当在真正需要数据的时候,才真正执行数据加载操作。

mybatis默认没有开启延迟加载功能,需要在springboot的yml配置文件中启动延迟加载功能:

mybatis-plus:
  mapper-locations: "classpath:com/itpsc/mapper/**/*. "
  type-aliases-package: "com.itpsc.entity"
  global-config:
    db-column-underline: true
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: true #配置的缓存的全局开关
    lazyLoadingEnabled: true #延时加载的开关
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印sql语句,调试用

 

延迟加载例子

比如查询员工信息的时候关联查询员工的部门信息。我们可以先把员工信息查询出来,当遍历员工信息需要查询对应的部门信息的时候,就可以调用员工的getDept()方法去加载部门信息。

EmpMapper.

<!--resultMap 延迟加载-->
  <resultMap id="empLazyLoadDeptMap" type="com.itpsc.entity.Emp" >
    <result column="empno" property="empno" jdbcType="INTEGER" />
    <result column="ename" property="ename" jdbcType="VARCHAR" />
    <result column="job" property="job" jdbcType="VARCHAR" />
    <result column="mgr" property="mgr" jdbcType="INTEGER" />
    <result column="hiredate" property="hiredate" jdbcType="DATE" />
    <result column="sal" property="sal" jdbcType="REAL" />
    <result column="comm" property="comm" jdbcType="REAL" />
    <result column="deptno" property="deptno" jdbcType="INTEGER" />
    <!--延迟加载关联对象,Emp对象中延迟加载Dept对象-->
    <association property="dept" javaType="com.itpsc.entity.Dept"
                 select="com.itpsc.mapper.DeptMapper.queryByDeptno" column="deptno">
    </association>
  </resultMap>
  
  <select id="queryEmpLazyLoadDept" resultMap="empLazyLoadDeptMap">
    select * from t_emp;
  </select>

DeptMapper.

<select id="queryByDeptno" parameterType="int" resultType="com.itpsc.entity.Dept">
        select * from t_dept where t_dept.deptno=#{emp.deptno}
    </select>

测试代码

@Test
    public void testQueryEmpLazyLoadDept() {
        List<Emp> empList = empService.queryEmpLazyLoadDept();
        System.out.println(empList.size());
        for(int i=0;i<empList.size();i++) {
            Emp emp = empList.get(i);
            System.out.println("员工信息:" + emp.toString());
            System.out.println("员工所有部门信息:" + emp.getDept());
        }
    }

运行结果

JDBC Connection [com.mysql.jdbc.JDBC4Connection@1cd6b1bd] will not be managed by Spring
==>  Preparing: select * from t_dept where t_dept.deptno=? 
==> Parameters: 10(Integer)
<==    Columns: deptno, dname, loc
<==        Row: 10, ACCOUNTING, NEW YORK
<==      Total: 1
员工信息:Emp{empno=7934, ename='MILLER', job='CLERK', mgr=7782, hiredate=Sat Jan 23 00:00:00 CST 1982, sal=1300.0, comm=null, deptno=10, skillList=null}
员工所有部门信息:Dept{deptno=10, dname='ACCOUNTING', loc='NEW YORK'}

从运行结果可以看出,调用Emp对象的getDept()方法是,才发出查询部门信息的sql语句,达到延迟加载的作用。

收藏 打印