首页 > 代码库 > MyBatis应用开发(18)延迟加载之延迟加载触发方法(lazyLoadTriggerMethods)
MyBatis应用开发(18)延迟加载之延迟加载触发方法(lazyLoadTriggerMethods)
1.1.1. 使用lazyLoadTriggerMethods
lazyLoadTriggerMethods默认情况下仅仅支持自动将equals,clone,hashCode,toString这几个方法定义为延迟加载的加载触发方法。
如果将Person的doLazyLoadingNow()方法加入这个列表中,则调用doLazyLoadingNow()方法将会导致Person上的所有延迟加载属性的关联对象被执行加载。
在SqlMapConfig.xml中配置了doLazyLoadingNow方法。
<setting name="lazyLoadTriggerMethods" value="doLazyLoadingNow,equals,clone,hashCode,toString" />
为Person类添加doLazyLoadingNow()方法:
public class Person { /** * 调用此方法触发person上的所有延迟加载属性立即执行加载 **/ public void doLazyLoadingNow(){ System.out.println("doLazyLoadingNow is running...."); }
单元测试代码:
//打开Session。 session = sessionBuilder.openSession(); //找到MyBatis自动实现的PersonMapper接口的代理对象。 PersonMapper personMapper = session.getMapper(PersonMapper.class); //嵌套查询。 List<Person> persons = personMapper.findAllPersons(); System.out.println("---------------------"); //输出查询结果。 for(Person person : persons){ System.out.println(person.getId() +","+person.getName()); System.out.println("----------------"); //调用此方法触发person上的所有延迟加载属性立即执行加载。 person.doLazyLoadingNow(); } System.out.println("---------------------"); for(Person person : persons){ IdCard idCard = person.getIdCard(); System.out.println("================"); System.out.println(idCard); }
运行结果:
0 [main] DEBUG com.test.mybatis3.mapper.PersonMapper.findAllPersons - ==> Preparing: select * from t_person
42 [main] DEBUG com.test.mybatis3.mapper.PersonMapper.findAllPersons - ==> Parameters:
63 [main] TRACE com.test.mybatis3.mapper.PersonMapper.findAllPersons - <== Columns: id, name, status
63 [main] TRACE com.test.mybatis3.mapper.PersonMapper.findAllPersons - <== Row: lisi, li si, 0
156 [main] TRACE com.test.mybatis3.mapper.PersonMapper.findAllPersons - <== Row: zhangsan, zhang san, 0
157 [main] DEBUG com.test.mybatis3.mapper.PersonMapper.findAllPersons - <== Total: 2
---------------------
lisi,li si
----------------
158 [main] DEBUG com.test.mybatis3.mapper.PersonMapper.findIdCardByPersonId - ==> Preparing: select * from t_idcard where personid=?
158 [main] DEBUG com.test.mybatis3.mapper.PersonMapper.findIdCardByPersonId - ==> Parameters: lisi(String)
159 [main] TRACE com.test.mybatis3.mapper.PersonMapper.findIdCardByPersonId - <== Columns: personid, cardid
159 [main] TRACE com.test.mybatis3.mapper.PersonMapper.findIdCardByPersonId - <== Row: lisi, 222222222222222222
159 [main] DEBUG com.test.mybatis3.mapper.PersonMapper.findIdCardByPersonId - <== Total: 1
doLazyLoadingNow is running....
zhangsan,zhang san
----------------
160 [main] DEBUG com.test.mybatis3.mapper.PersonMapper.findIdCardByPersonId - ==> Preparing: select * from t_idcard where personid=?
160 [main] DEBUG com.test.mybatis3.mapper.PersonMapper.findIdCardByPersonId - ==> Parameters: zhangsan(String)
161 [main] TRACE com.test.mybatis3.mapper.PersonMapper.findIdCardByPersonId - <== Columns: personid, cardid
161 [main] TRACE com.test.mybatis3.mapper.PersonMapper.findIdCardByPersonId - <== Row: zhangsan, 1111111111111111
161 [main] DEBUG com.test.mybatis3.mapper.PersonMapper.findIdCardByPersonId - <== Total: 1
doLazyLoadingNow is running....
---------------------
================
IdCard [cardid=222222222222222222, personid=lisi, person=null]
================
IdCard [cardid=1111111111111111, personid=zhangsan, person=null]
观察运行结果,可以发现,在执行Person的doLazyLoadingNow()方法时,就已经执行了关联属性idCard对应的SQL。
MyBatis应用开发(18)延迟加载之延迟加载触发方法(lazyLoadTriggerMethods)