延迟加载基本概念
上面我们已经知道使用association、collection可以实现一对一及一对多映射,association、collection还有另外一个延迟加载的功能。
延迟加载(lazy load)是关联对象默认的加载方式,延迟加载机制是为了避免一些无谓的性能开销而提出来的,所谓延迟加载就是当在真正需要数据的时候,才真正执行数据加载操作。
mybatis默认没有开启延迟加载功能,需要在springboot的yml配置文件中启动延迟加载功能:
mybatis-plus:
mapper-locations: "classpath:com/itpsc/mapper/**/*.xml"
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.xml
select * from t_emp;
DeptMapper.xml
select * from t_dept where t_dept.deptno=#{emp.deptno}
测试代码
@Testpublic voidtestQueryEmpLazyLoadDept() {
List empList =empService.queryEmpLazyLoadDept();
System.out.println(empList.size());for(int i=0;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 bySpring==> 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语句,达到延迟加载的作用。