当前位置: 首页 > news >正文

Mybatis_day4_Mybatis的延迟加载

  • Mybatis 中一对一,一对多,多对多关系的配置及实现,可以实现对象的关联查询。实际开发过程中很多时候我们并不需要总是在加载用户信息时就一定要加载他的账户信息。此时就是我们所说的延迟加载。


1.1 何为延迟加载?

  • 延迟加载:
    • 就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据。延迟加载也称懒加载.
  • 好处:先从单表查询,需要时再从关联表去关联查询,大大提高数据库性能,因为查询单表要比关联查询多张表速
    度要快。
  • 坏处:因为只有当需要用到数据时,才会进行数据库查询,这样在大批量数据查询时,因为查询工作也要消耗时间,所以可能造成用户等待时间变长,造成用户体验下降。

1.2 实现需求

  • 需求:
  • 查询账户(Account)信息并且关联查询用户(User)信息。如果先查询账户(Account)信息即可满足要求,就不需再查询用户信息,当我们需要查询用户(User)信息时再查询用户(User)信息。把对用户(User)信息的按需去查询就是延迟加载。
  • mybatis第三天实现多表操作时,我们使用了resultMap来实现一对一,一对多,多对多关系的操作。主要是通过association、collection实现一对一及一对多映射。association、collection 具备延迟加载功能。

1.3 使用 assocation 实现延迟加载

  • 需求:查询账户信息同时查询用户信息。
  1. 账户的持久层 DAO 接口
public interface IAccountDao {
/**
* 查询所有账户,同时获取账户的所属用户名称以及它的地址信息
* @return
*/
List<Account> findAll();
}

  1. 账户的持久层映射文件
<mapper namespace="cn.myp666.dao.IAccountDao">
<!-- 建立对应关系 --><resultMap type="account" id="accountMap"><id column="aid" property="id"/><result column="uid" property="uid"/><result column="money" property="money"/><!-- 它是用于指定从表方的引用实体属性的 --><association property="user" javaType="cn.myp666.domain.user"select="cn.myp666.dao.IUserDao.findById"
column="uid"></association></resultMap><select id="findAll" resultMap="accountMap">select * from account
</select>
</mapper>
  • select 属性:
    用于指定查询 user 列表的 sql 语句,所以填写的是该 sql 映射的 id
  • column 属性:
    用于指定 select 属性的 sql 语句的参数来源,上面的参数来自于 account 的 uid 列,所以就写成 uid 这一个字段名了

  1. 用户的持久层接口和映射文件
public interface IUserDao {
/**
* 根据 id 查询
* @param userId
* @return
*/
User findById(Integer userId);
}
<mapper namespace="cn.myp666.dao.IUserDao">
<!-- 根据 id 查询 --><select id="findById" resultType="user" parameterType="int" >select * from user where id = #{uid}</select>
</mapper>

  1. 开启 Mybatis 的延迟加载策略
  • 进入 Mybaits 的官方文档,找到 settings 的说明信息:
    在这里插入图片描述
  • 我们需要在 Mybatis 的配置文件 SqlMapConfig.xml 文件中添加延迟加载的配置。
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>


使用 Collection 实现延迟加载

  • 同样我们也可以在一对多关系配置的结点中配置延迟加载策略。<collection>结点中也有 select 属性,column 属性。
  • 需求:完成加载用户对象时,查询该用户所拥有的账户信息。
  1. 在 User 实体类中加入 List< Account >属性
public class User implements Serializable {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
private List<Account> accounts;public List<Account> getAccounts() {return accounts;
}
public void setAccounts(List<Account> accounts) {this.accounts = accounts;

  1. 编写用户和账户持久层接口的方法
List<User> findAll();
/**
* 根据用户 id 查询账户信息
* @param uid
* @return
*/
List<Account> findByUid(Integer uid);

  1. 编写用户持久层映射配置
<resultMap type="user" id="userMap"><id column="id" property="id"></id><result column="username" property="username"/><result column="address" property="address"/><result column="sex" property="sex"/><result column="birthday" property="birthday"/><!-- collection 是用于建立一对多中集合属性的对应关系
ofType 用于指定集合元素的数据类型
select 是用于指定查询账户的唯一标识(账户的 dao 全限定类名加上方法名称)
column 是用于指定使用哪个字段的值作为条件查询
--><collection property="accounts" ofType="account"select="cn.myp666.dao.IAccountDao.findByUid"column="id"></collection>
</resultMap><!-- 配置查询所有操作 -->
<select id="findAll" resultMap="userMap">select * from user
</select>
  • < collection >标签:
    主要用于加载关联的集合对象
  • select 属性:
    用于指定查询 account 列表的 sql 语句,所以填写的是该 sql 映射的 id
  • column 属性:
    用于指定 select 属性的 sql 语句的参数来源,上面的参数来自于 user 的 id 列,所以就写成 id 这一个字段名了

  1. 编写账户持久层映射配置
<!-- 根据用户 id 查询账户信息 -->
<select id="findByUid" resultType="account" parameterType="int">select * from account where uid = #{uid}
</select>

http://www.taodudu.cc/news/show-1476408.html

相关文章:

  • Oracle通过触发器实现自增长字段
  • 计算机寄存器是如何实现的
  • Leecode热题100---560:和为k的子数组个数
  • CMake编译Opencv报错及解决方案汇总
  • CSS引入方式
  • 华为静态路由跨网段通信eNSP
  • Mybatis_day4_Mybatis的缓存
  • Mybatis_day4_Mybatis的注解开发
  • Mybatis遇坑
  • 关于java中的位运算
  • Spring_day1
  • Spring_day2
  • Spring_day3
  • Spring_day4
  • SpringMVC_day1
  • SpringMVC_day1_常用注解
  • SpringMVC_02
  • Error creating bean with name 'dataSource' defined in class path resource [spring/spring-dao.xml]:
  • SpringSecurity入门
  • 服务注册不进eureka
  • Spring Cloud总结
  • 在此之前的博客地址
  • golang利用反射写入excel的简单工具类
  • 实习工作难点记录
  • c 结构体之位域(位段)
  • 辗转相除求最大公约数,最大公倍数
  • Ubuntu“无法解析或打开软件包的列表或是状态文件”的解决办法。
  • 错误:cc1: error: unrecognized command line option “-m32”
  • 在编写mini2440 helloworld驱动遇到的问题
  • [leetcode] Median of Two Sorted Arrays 寻找两个有序数组的中位数
  • [leetcode] Reverse Integer 反转一个整数
  • [leetcode] Palindrome Number 回文数判断
  • [leetcode] Longest Common Prefix 字符窜最长公共前缀判断
  • [leetcode] Single Number 查找数组中的单数
  • [leetcode] Power of Two 判断一个数是否是2的平方
  • [leetcode] Max Points on a Line 判断最多有多少个点在同一条直线上