上两章学习了mybatis一对多、多对一的分步、嵌套查询方式,嵌套的查询方式可以使用延迟加载,达到按需加载的目的,提高查询效率
本项目搭建源码:https://github.com/zhuquanwen/mybatis-learn/releases/tag/for-lazy
搭建过程:
在https://blog.csdn.net/u011943534/article/details/108436984文章基础上搭建,有些过程不详细描述.
1 引入asm和cglib
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.ow2.asm/asm -->
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>8.0.1</version>
</dependency>
2 sqlMapConfig.xml中引入延迟加载的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--mybatis的主配置文件-->
<configuration>
<settings>
<!-- 全局性设置懒加载。如果设为‘false',则所有相关联的都会被初始化加载。 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 当设置为‘true'的时候,懒加载的对象可能被任何懒属性全部加载。否则,每个属性都按需加载。 -->
<setting name="aggressiveLazyLoading" value="false" />
<!-- 懒加载代理方式 CGLIB OR JAVASSIS-->
<setting name="proxyFactory" value="CGLIB"/>
<setting name="lazyLoadTriggerMethods" value=""/>
</settings>
<!--自定义typeHandler-->
<typeHandlers>
.......
</configuration>
上面的延迟加载配置是全局的,可以被ResultMap中的fetchType属性覆盖,如果是eager则此resultMap不会延迟加载
3 单元测试
/**
* 延迟加载
* */
@Test
public void test5() throws IOException {
@Cleanup InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sessionFactory = sqlSessionFactoryBuilder.build(is);
SqlSession session = sessionFactory.openSession();
ProvinceMapper mapper = session.getMapper(ProvinceMapper.class);
MyProvince province = mapper.selectWithCitysById0(1);
System.out.println(111111);
List<City> cityList = province.getCityList();
System.out.println(province);
}
结果:
[2020/09/11 22:08:30,180] [DEBUG] [org.apache.ibatis.datasource.pooled.PooledDataSource:424] - Created connection 251210093.
[2020/09/11 22:08:30,181] [DEBUG] [org.apache.ibatis.transaction.jdbc.JdbcTransaction:100] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@ef9296d]
[2020/09/11 22:08:30,190] [DEBUG] [com.learn.zqw.association.mapper.ProvinceMapper.selectWithCitysById0:143] - ==> Preparing: select p.id as province_id, p.name as province_name from province p where id = ?
[2020/09/11 22:08:30,241] [DEBUG] [com.learn.zqw.association.mapper.ProvinceMapper.selectWithCitysById0:143] - ==> Parameters: 1(Integer)
[2020/09/11 22:08:30,366] [DEBUG] [com.learn.zqw.association.mapper.ProvinceMapper.selectWithCitysById0:143] - <== Total: 1
[2020/09/11 22:08:30,367] [DEBUG] [com.learn.zqw.plugin.TestPlugin:38] - 耗时:749ms
MyProvince{provinceId=1, provinceName='黑龙江', cityList=null}
[2020/09/11 22:08:30,371] [DEBUG] [com.learn.zqw.association.mapper.CityMapper.selectByPid:143] - ==> Preparing: select id,name,pid from city where pid = ?
[2020/09/11 22:08:30,372] [DEBUG] [com.learn.zqw.association.mapper.CityMapper.selectByPid:143] - ==> Parameters: 1(Integer)
[2020/09/11 22:08:30,377] [DEBUG] [com.learn.zqw.association.mapper.CityMapper.selectByPid:143] - <== Total: 6
MyProvince{provinceId=1, provinceName='黑龙江', cityList=[City{id=1, name='哈尔滨', pid=1, province=null}, City{id=2, name='黑河', pid=1, province=null}, City{id=3, name='牡丹江', pid=1, province=null}, City{id=4, name='齐齐哈尔', pid=1, province=null}, City{id=5, name='大庆', pid=1, province=null}, City{id=6, name='鸡西', pid=1, province=null}]}
查看结果发现在调用getCityList前cityList的属性为null,调用后才会执行查询到SQL,达到了延迟加载的目的