Mybatis延迟加载

it2025-01-26  12

什么是延迟加载呢? 通俗的讲就是按需加载,我们需要什么的时候再去进行什么操作。

什么时候会用到延迟加载呢?

多表单独查询的时候。

而且先从单表查询,需要时再从关联表去关联查询,能大大提高数据库性能,因为查询单表要比关联查询多张表速度要快。 延迟加载分为两种:深度延时加载,侵入式延迟加载

如何开启延时加载? 两种方法:

1.在collection标签中:

<select id="selectByMinorId" resultMap="selectMinorMap"> select cid,cname from country where cid =#{id} </select> <resultMap id="selectMinorMap" type="Country"> <id column="cid" property="cid"/> <result column="cname" property="cname"/> <collection column="cid" property="mino" ofType="Minor" select="selectByMinorIds" fetchType="lazy"/> //使用fetch属性的值为true,默认开启深度延迟加载 </resultMap> <select id="selectByMinorIds" resultType="com.example.mytest01.pojo.Minor" > select mname from minor where countryid =#{cid} </select>

2.mybatis.xml中进行配置

<settings> <!--延迟加载总开关,打开后,默认的是深度延时加载 --> <setting name="lazyLoadingEnabled" value="true" /> <!--侵入式延迟加载开关 --> <!--3.4.1版本之前默认是true,之后默认是false --> <setting name="aggressiveLazyLoading" value="true" /> </settings>

加载特点

侵入式延迟: 执行对主加载对象的查询时,不会执行对关联对象的查询。但当要访问主加载对象的详情属性时,就会马上执行关联对象的select查询。

深度延迟: 执行对主加载对象的查询时,不会执行对关联对象的查询。访问主加载对象的详情时也不会执行关联对象的select查询。只有当真正访问关联对象的详情时,才会执行对关联对象的 select 查询。

MyBatis 的延迟加载只是对关联对象的查询有迟延设置,对于主加载对象都是直接执行查询语句的。

举例: mapper.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"> <!-- namespace:填写映射当前的Mapper接口,所有的增删改查的参数和返回值类型, 就可以直接填写缩写,不区分大小写,直接通过方法名去找类型--> <mapper namespace="com.example.mytest01.dao.IMinorDao"> <select id="selectByMinorId" resultMap="selectMinorMap"> select cid,cname from country where cid =#{id} </select> <resultMap id="selectMinorMap" type="Country"> <id column="cid" property="cid"/> <result column="cname" property="cname"/> <collection column="cid" property="mino" ofType="Minor" select="selectByMinorIds"/> </resultMap> <select id="selectByMinorIds" resultType="com.example.mytest01.pojo.Minor" > select mname from minor where countryid =#{cid} </select> </mapper>

当不开启延迟加载时:

@Autowired private IMinorDao m; @Test void hah(){ Country c =m.selectByMinorId(1); //System.out.println(c.getCname()); }

进行两次查询;

@Autowired private IMinorDao m; @Test void hah(){ //Country c =m.selectByMinorId(1); System.out.println(c.getCname()); }

进行两次查询;

重点来了:

当开深度启延迟加载时:
@Autowired private IMinorDao m; @Test void hah(){ Country c =m.selectByMinorId(1); //System.out.println(c.getCname()); }

通过日志观察到,只查询一次,还未进行第二次查询;

.

@Autowired private IMinorDao m; @Test void hah(){ //Country c =m.selectByMinorId(1); System.out.println(c.getCname()); }

当查询主加载对象的详情属性时,依旧只进行了第一次的查询; .

@Autowired private IMinorDao m; @Test void hah(){ //Country c =m.selectByMinorId(1); //System.out.println(c.getCname()); System.out.println(c.getMino()); }

看到这里我们会发现:只有当查询主加载对象的关联属性时,才进行了两次查询

当开启侵入式延迟加载时:
@Autowired private IMinorDao m; @Test void hah(){ Country c =m.selectByMinorId(1); //System.out.println(c.getCname()); }

通过日志观察到,只查询一次,还未进行第二次查询;

@Autowired private IMinorDao m; @Test void hah(){ //Country c =m.selectByMinorId(1); System.out.println(c.getCname()); }

与深度延迟加载不同的时:当查询主属性的详细信息时,Mybatis就已经进行了第二次的关联属性的查询;

最新回复(0)