版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_44580977/article/details/96484510
因为只有在用户需要的时候再去查询数据库,可以大大提高数据库性能
就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据。
立即加载方式:查询账户(Account)信息并且关联查询用户(User)信息。 SQL语句: select * from account a left outer join user u on u.id = a.uid
延迟加载方式:如果先查询账户(Account)信息即可满足要 求,当我们需要查询用户(User)信息时再查询用户(User)信息。把对用户(User)信息的按需去查询就是延迟加 载。 SQL语句: select * from account 封装到Account类里, 当使用到 Account类 的成员变量 User类时,执行 SQL语句: select * from account where uid = #{uid}
数据库关系,一个账户对应一个用户,一个用户对应多个账户,如下
IAccountDao.xml mapper配置文件
<mapper namespace="com.itheima.dao.IAccountDao">
<!-- 定义封装account和user的resultMap -->
<resultMap id="accountUserMap" type="account">
<id property="id" column="id"></id>
<result property="uid" column="uid"></result>
<result property="money" column="money"></result>
<!-- 一对一的关系映射:配置封装user的内容
select属性指定的内容:查询用户的唯一标识:
column属性指定的内容:用户根据id查询时,所需要的参数的值
-->
<association property="user" column="uid" javaType="user" select="com.itheima.dao.IUserDao.findById"></association>
</resultMap>
<!-- 查询所有 一对一用到的 -->
<select id="findAll" resultMap="accountUserMap">
select * from account
</select>
<!-- 根据用户id查询账户列表 一对多用到的 -->
<select id="findAccountByUid" resultType="account">
select * from account where uid = #{uid}
</select>
</mapper>
IUserDao.xml mapper配置
<mapper namespace="com.itheima.dao.IUserDao">
<!-- 定义User的resultMap-->
<resultMap id="userAccountMap" type="user">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="address" column="address"></result>
<result property="sex" column="sex"></result>
<result property="birthday" column="birthday"></result>
<!-- 配置user对象中accounts集合的映射 -->
<collection property="accounts" ofType="account" select="com.itheima.dao.IAccountDao.findAccountByUid" column="id"></collection>
</resultMap>
<!-- 查询所有 一对多用到的-->
<select id="findAll" resultMap="userAccountMap">
select * from user
</select>
<!-- 根据id查询用户 一对一用到的-->
<select id="findById" parameterType="INT" resultType="user">
select * from user where id = #{uid}
</select>
</mapper>
<!--配置参数-->
<settings>
<!--开启Mybatis支持延迟加载-->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"></setting>
</settings>
官网文档解释
现在已经学会配置延迟加载了,明白什么是延迟加载了嘛?不不明白,再看看下面的测试你可能就知道了,
什么是用到的时候去查数据库了,懒加载(延迟加载)的精髓
测试1
public class Account implements Serializable {
private Integer id;
private Integer uid;
private Double money;
//一个账户对应一个用户,一对一
// 从表实体应该包含一个主表实体的对象引用
private User user;
}
测试类
/**
* 测试查询所有
*/
@Test
public void testFindAll(){
//注意看,这里我们查询了,account 单没有 用他的user变量
List<Account> accounts = accountDao.findAll();
// for(Account account : accounts){
// System.out.println("--------每个account的信息------------");
// System.out.println(account);
// System.out.println(account.getUser());
// }
}
控制台输出
测试二
和测试1相比只是释放了遍历
/**
* 测试查询所有
*/
@Test
public void testFindAll(){
List<Account> accounts = accountDao.findAll();
for(Account account : accounts){
System.out.println("--------每个account的信息------------");
System.out.println(account);
System.out.println(account.getUser());
}
}
控制台输出
当遍历到user的时候,框架会通过account类的id开始去查询user,并完成封装 ,这就是懒加载