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