Mybatis4 - 延迟加载

来源:https://www.bilibili.com/video/BV1V7411w7VW?p=6

上一节:https://blog.csdn.net/qq_40893824/article/details/107371171
下一节:https://blog.csdn.net/qq_40893824/article/details/107383696

延迟加载 = 懒加载 = 惰性加载

对于数据持久层的操作,特定的情况下访问特定的数据库,其他情况不不访问某些表
减少 Java 应用与数据库的交互次数

不同业务需求,要查询不同的表
根据具体的业务需求 动态减少数据表查询 就是延迟加载

在 第一节 建立的 mybatis 工程中 写实验代码:

两次查询

1 repository/ StudentRepository 中,添加代码:
public Student findByIdLazy(long id);

2 repository/ StudentRepository.xml 中,添加代码:

    <resultMap id="StudentLazyMap" type="com.southwind.entity.Student">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <association property="classes" javaType="com.southwind.entity.Classes">
            <id column="cid" property="id"></id>
            <result column="cname" property="name"></result>
        </association>
    </resultMap>

    <select id="findByIdLazy" parameterType="long" resultMap="StudentLazyMap">
        select * from student where id=#{id}
    </select>

3 repository/ ClassesRepository中,添加代码:
public Classes findByIdLazy(long id);

4 repository/ ClassesRepository.xml 中,添加代码:

    <select id="findByIdLazy" parameterType="long"
            resultType="com.southwind.entity.Classes">
        select * from classes where id=#{id}
    </select>

5 test/ Test3 中,添加代码:
之前 其他的查询代码 注释掉

        StudentRepository studentRepository = sqlSession.getMapper(StudentRepository.class);
        Student student = studentRepository.findByIdLazy(1L);
        System.out.println(student);
        ClassesRepository classesRepository = sqlSession.getMapper(ClassesRepository.class);
        System.out.println(classesRepository.findByIdLazy(student.getClasses().getId()));
        sqlSession.close();

运行:

结果合并

repository/ StudentRepository.xml 中,添加代码:

    <resultMap id="StudentLazyMap" type="com.southwind.entity.Student">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <association property="classes" javaType="com.southwind.entity.Classes"
            select="com.southwind.repository.ClassesRepository.findByIdLazy" column="cid">
        </association>
    </resultMap>

其中 select="com.southwind.repository.ClassesRepository.findByIdLazy" column="cid"
就和 ClassesRepository.xml 中 findByIdLazy 关联起来了

test/ Test3 中,添加代码:

        StudentRepository studentRepository = sqlSession.getMapper(StudentRepository.class);
        System.out.println(studentRepository.findByIdLazy(1L));
        sqlSession.close();

运行:

这是一条结果,但过程是两次,两次结果合并了
其两次查询是:

输出 sql 语句

1 test/ Test3 中,添加代码:

        StudentRepository studentRepository = sqlSession.getMapper(StudentRepository.class);
        Student student = studentRepository.findByIdLazy(1L);
        System.out.println(student.getName());

想要查询名字

2 resources/ config.xml 中,添加代码:

    <settings>
        <!--    打印 sql 语句    -->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

运行 Test3 的结果:

可以看到 名字查出来了,但多了 查班级的无用查询

若 resources/ config.xml 中,添加代码是:

    <settings>
        <!--    打印 sql 语句    -->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <!--    开启延迟加载    -->
        <setting name="lazyLoadingEnabled" value="true"/>
    </settings>

运行 Test3 的结果:

名字查出来了,没有 查班级的无用查询

上一节:https://blog.csdn.net/qq_40893824/article/details/107371171
下一节:https://blog.csdn.net/qq_40893824/article/details/107383696

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_1403034144

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值