mybatis 延迟加载

mybatis 延迟加载

相关定义

1.什么是延迟加载
在真正使用数据时才发起查询,不用的时候不查询。按需加载(懒加载)
2.什么是立即加载
不管用不用,只要一调用方法,马上发起查询。

配置

在mybatis 配置页面中 加入

 <!--配置参数 -->
    <settings>
        <!--开启Mybatis支持延迟加载-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!--触发方法立即加载,否则延迟加载-->
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

查询出账户信息及其对应的用户信息
IAccountDao.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.itheima.dao.IAccountDao">
    <resultMap id="accountUserMap" type="account">
        <id column="id" property="id"></id>
        <result column="uid" property="uid"></result>
        <result column="money" property="money"></result>
        <!--select 属性指定的内容:查询用户的唯一标识;
            column属性指定的内容:用户根据id查询时,所需要的参数的值-->
        <association property="user" column="uid" select="com.itheima.dao.IUserDao.findById"></association>
    </resultMap>

    <select id="findAll" resultMap="accountUserMap">
        select * from account
    </select>
</mapper>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.itheima.dao.IUserDao">

    <select id="findAll" resultType="UsER">
        select * from user
    </select>
    <select id="findById" parameterType="int" resultType="User" >
        select * from user where id = #{uid}
    </select>
</mapper>

当开启延迟加载的时候 当不使用用户数据时 会不查询 输出结果

    main] DEBUG ansaction.jdbc.JdbcTransaction  - Opening JDBC Connection
2021-03-18 10:01:30,035 598    [           main] DEBUG source.pooled.PooledDataSource  - Created connection 1615056168.
2021-03-18 10:01:30,035 598    [           main] DEBUG ansaction.jdbc.JdbcTransaction  - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6043cd28]
2021-03-18 10:01:30,037 600    [           main] DEBUG theima.dao.IAccountDao.findAll  - ==>  Preparing: select * from account 
2021-03-18 10:01:30,068 631    [           main] DEBUG theima.dao.IAccountDao.findAll  - ==> Parameters: 
2021-03-18 10:01:30,242 805    [           main] DEBUG theima.dao.IAccountDao.findAll  - <==      Total: 3

当没有开启延迟加载的时候 会立即加载出来 输出结果

main] DEBUG ansaction.jdbc.JdbcTransaction  - Opening JDBC Connection
2021-03-18 10:04:54,286 656    [           main] DEBUG source.pooled.PooledDataSource  - Created connection 352359770.
2021-03-18 10:04:54,287 657    [           main] DEBUG ansaction.jdbc.JdbcTransaction  - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1500955a]
2021-03-18 10:04:54,289 659    [           main] DEBUG theima.dao.IAccountDao.findAll  - ==>  Preparing: select * from account 
2021-03-18 10:04:54,317 687    [           main] DEBUG theima.dao.IAccountDao.findAll  - ==> Parameters: 
2021-03-18 10:04:54,342 712    [           main] DEBUG .itheima.dao.IUserDao.findById  - ====>  Preparing: select * from user where id = ? 
2021-03-18 10:04:54,343 713    [           main] DEBUG .itheima.dao.IUserDao.findById  - ====> Parameters: 46(Integer)
2021-03-18 10:04:54,350 720    [           main] DEBUG .itheima.dao.IUserDao.findById  - <====      Total: 1
2021-03-18 10:04:54,351 721    [           main] DEBUG .itheima.dao.IUserDao.findById  - ====>  Preparing: select * from user where id = ? 
2021-03-18 10:04:54,351 721    [           main] DEBUG .itheima.dao.IUserDao.findById  - ====> Parameters: 45(Integer)
2021-03-18 10:04:54,360 730    [           main] DEBUG .itheima.dao.IUserDao.findById  - <====      Total: 1
2021-03-18 10:04:54,361 731    [           main] DEBUG theima.dao.IAccountDao.findAll  - <==      Total: 3
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

牛虻qq

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

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

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

打赏作者

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

抵扣说明:

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

余额充值