mybatisの使用とソースの分析(11)mybatisの配置は遅延してロードします.


上二章で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 sql MapConfig.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のfetType属性によってカバーできます.もしeagerであれば、このreultMapは負荷が遅くなりません.
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}]}
調べた結果、get CityListを呼び出す前のcityListの属性はnullであり、呼び出し後にSQLに照会することができ、遅延負荷の目的に達したということです.