(九) MyBatis从入门到入土——延迟加载、鉴别器以及继承

冢狐 2021-02-23 18:21:09
mybatis 入门 加载 延迟 入土


这是mybatis系列第9篇,没看前文的建议先去【Java冢狐】公众号中查看前文,方便理解和掌握。在上一篇中我们介绍了关于MyBatis的自动映射是如何开启以及使用的,想必大家对于这方面的知识有所了解了。

今天要给大家带来的主要是MyBatis延迟加载以及鉴别器相关方面的知识以及内容。

延迟加载

延迟加载介绍

所谓的延迟加载就是将数据加载时机推迟,其中比较典型的应用就是推迟嵌套查询的执行时机。

因为在mybatis中经常用到关联查询,但是并不是任何时候都需要立即返回关联查询结果。就比如查询订单信息,并不一定需要及时返回订单对应的用户信息或者订单详情信息等,所以当我们遇到这种情况时就需要一种机制,当需要查看关联的数据时,再去执行对应的查询,返回需要的结果。

这种需求在mybatis中可以使用延迟加载机制来实现。

延迟加载2种设置方式

MyBatis对于延迟加载提供了两种设置方式,分别是:

  • 全局配置的方式
  • sqlmap中配置的方式

从这两种方式的名称来看就能发现这两种延迟加载的区别,第一种方法会对所有的关联查询有效,而第二种方法只会对相关设置的查询有效。

下面我们就分别看一下这两种延迟加载的使用方式。

全局配置延迟加载

要想实现全局配置延迟加载就要通过mybatis的配置文件。

mybatis配置文件中通过下面两个属性来控制延迟加载:

<settings>
<!--打开延迟加载的开关 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 当为true的时候,调用任意延迟属性,会去加载所有延迟属性,如果为false,则调用某个属性的时候,只会加载指定的属性 -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
  • lazyLoadingEnabled:这个属性比较好理解,是否开启延迟加载,默认为false,如果需要开启延迟加载,将其设置为true
  • aggressiveLazyLoading:当为true的时候,调用任意延迟属性,会去加载所有延迟属性,如果为false,则调用某个属性的时候,只会加载指定的属性

关于全局配置延迟加载的说明就是这些,下面我们来用一个具体的例子来说明下全局配置延迟加载是如何使用的。

需求

这次我们要使用MyBatis实现的需求就是通过订单id查询订单的各种信息,诸如:订单用户信息、订单明细列表。其中订单用户信息和订单明细信息采用延迟加载的方式来进行获取。

mybatis配置

按照前面的介绍第一步就是通过mybatis的配置文件来进行设置。如下所示:

<settings>
<!--打开延迟加载的开关 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 当为true的时候,调用任意延迟属性,会去加载所有延迟属性,如果为false,则调用某个属性的时候,只会加载指定的属性 -->
<setting name="aggressiveLazyLoading" value="true"/>
</settings>

OrderMapper.xml

进行全局配置的设置完毕以后,我们就进行我们正常开发的途径,首先就是编写xml文件,在xml文件中编写我们的sql语句。

<resultMap id="orderModelMap1" type="com.zhonghu.chat09.demo5.model.OrderModel">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="create_time" property="createTime"/>
<result column="up_time" property="upTime"/>
<!-- 通过订单中user_id作为条件,查询下单人信息 -->
<association property="userModel" select="com.zhonghu.chat09.demo5.mapper.UserMapper.getById1" column="user_Id"/>
<!-- 通过订单id作为条件,查询详情列表 -->
<collection property="orderDetailModelList" select="com.zhonghu.chat09.demo5.mapper.OrderDetailMapper.getListByOrderId1" column="id"/>
</resultMap>
<select id="getById1" resultMap="orderModelMap1">
<![CDATA[
SELECT
a.id ,
a.user_id,
a.create_time,
a.up_time
FROM
orders a
WHERE
a.id = #{value}
]]>
</select>

上面的orderModelMap1元素下面有两个关联查询,我们也写一下。

UserMapper.xml

<!-- 根据用户id查询用户信息 -->
<select id="getById1" resultType="com.zhonghu.chat09.demo5.model.UserModel">
<![CDATA[
SELECT id,name FROM user where id = #{user_id}
]]>
</select>

OrderDetailMapper.xml

<!-- 根据订单di查询订单明细列表 -->
<select id="getListByOrderId1" resultType="com.zhonghu.chat09.demo5.model.OrderDetailModel">
<![CDATA[
SELECT
a.id,
a.order_id AS orderId,
a.goods_id AS goodsId,
a.num,
a.total_price AS totalPrice
FROM
order_detail a
WHERE
a.order_id = #{order_id}
]]>
</select>

对应的3个Model

上面我们写了三个xml,那么接来下我们编写一下xml对应的Model。

@Getter
@Setter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class OrderModel {
private Integer id;
private Integer userId;
private Long createTime;
private Long upTime;
private UserModel userModel;
//订单详情列表
private List<OrderDetailModel> orderDetailModelList;
}
@Getter
@Setter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class UserModel {
private Integer id;
private String name;
}
@Getter
@Setter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class OrderDetailModel {
private Integer id;
private Integer orderId;
private Integer goodsId;
private Integer num;
private Double totalPrice;
}

测试用例

写完了Model,我们的代码基本就大功告成了,接下来我们就来看一看延迟加载的使用效果。

com.zhonghu.chat09.demo5.Demo5Test#getById1
@Test
public void getById1() throws IOException {
//指定mybatis全局配置文件
mybatisConfig = "demo5/mybatis-config.xml";
this.before();
OrderModel orderModel = null;
try (SqlSession sqlSession = this.sqlSessionFactory.openSession(true);) {
OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
orderModel = mapper.getById1(1);
}
log.info("-------分割线--------");
log.info("{}", orderModel.getUserModel());
}

运行输出

01:55.343 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById1 - ==> Preparing: SELECT a.id , a.user_id, a.create_time, a.up_time FROM orders a WHERE a.id = ?
01:55.372 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById1 - ==> Parameters: 1(Integer)
01:55.431 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById1 - <== Total: 1
01:55.431 [main] INFO c.j.chat05.demo5.Demo5Test - -------分割线--------
01:55.432 [main] DEBUG c.j.c.d.m.O.getListByOrderId1 - ==> Preparing: SELECT a.id, a.order_id AS orderId, a.goods_id AS goodsId, a.num, a.total_price AS totalPrice FROM order_detail a WHERE a.order_id = ?
01:55.432 [main] DEBUG c.j.c.d.m.O.getListByOrderId1 - ==> Parameters: 1(Integer)
01:55.435 [main] DEBUG c.j.c.d.m.O.getListByOrderId1 - <== Total: 2
01:55.439 [main] DEBUG c.j.c.d.mapper.UserMapper.getById1 - ==> Preparing: SELECT id,name FROM user where id = ?
01:55.439 [main] DEBUG c.j.c.d.mapper.UserMapper.getById1 - ==> Parameters: 2(Integer)
01:55.441 [main] DEBUG c.j.c.d.mapper.UserMapper.getById1 - <== Total: 1
01:55.441 [main] INFO c.j.chat05.demo5.Demo5Test - UserModel(id=2, name=Java冢狐)

从日志中可以看出,总共有3次查询,后面2次查询在分割线之后出现的,说明是调用了orderModel.getUserModel()触发后面2次查询动作。

代码中我们调用的是获取用户信息,而订单列表信息也被加载了,这个主要是由于aggressiveLazyLoading被设置为true了,当使用到一个延迟加载的属性时,其他的延迟加载的属性也会被一起加载,所以触发了2个关联的查询。

下面我们看看将aggressiveLazyLoading设置为false的效果

<settings>
<!--打开延迟加载的开关 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 当为true的时候,调用任意延迟属性,会去加载所有延迟属性,如果为false,则调用某个属性的时候,只会加载指定的属性 -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>

再次运行测试用例输出

12:19.236 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById1 - ==> Preparing: SELECT a.id , a.user_id, a.create_time, a.up_time FROM orders a WHERE a.id = ?
12:19.268 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById1 - ==> Parameters: 1(Integer)
12:19.336 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById1 - <== Total: 1
12:19.337 [main] INFO c.j.chat05.demo5.Demo5Test - -------分割线--------
12:19.338 [main] DEBUG c.j.c.d.mapper.UserMapper.getById1 - ==> Preparing: SELECT id,name FROM user where id = ?
12:19.338 [main] DEBUG c.j.c.d.mapper.UserMapper.getById1 - ==> Parameters: 2(Integer)
12:19.340 [main] DEBUG c.j.c.d.mapper.UserMapper.getById1 - <== Total: 1
12:19.341 [main] INFO c.j.chat05.demo5.Demo5Test - UserModel(id=2, name=Java冢狐)

通过这两次对比,我们可以很轻易的看出延迟加载开启与否的效果区别。

sqlmap中设置延迟加载

上面的篇幅我们介绍了全局的延迟加载是如何起作用以及如何使用的。全局的方式会对所有的关联查询起效,影响范围比较大,mybatis也提供了在关联查询中进行设置的方式,只会对当前设置的关联查询起效。

关联查询,一般我们使用association、collection,这两个元素都有个属性fetchType,通过这个属性可以指定关联查询的加载方式。

fetchType有两个值

  • eager:立即加载
  • lazy:延迟加载

下面我们来实现一个需求:还是通过订单id查询订单信息,并获取关联的用户信息、订单详细列表,用户信息我们要求立即加载,而订单详情我们要求延迟加载。

mapper xml如下

<resultMap id="orderModelMap2" type="com.zhonghu.chat09.demo5.model.OrderModel">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="create_time" property="createTime"/>
<result column="up_time" property="upTime"/>
<!-- 通过订单中user_id作为条件,查询下单人信息 -->
<association property="userModel" fetchType="eager" select="com.zhonghu.chat09.demo5.mapper.UserMapper.getById1" column="user_Id"/>
<!-- 通过订单id作为条件,查询详情列表 -->
<collection property="orderDetailModelList" fetchType="lazy" select="com.zhonghu.chat09.demo5.mapper.OrderDetailMapper.getListByOrderId1" column="id"/>
</resultMap>
<select id="getById2" resultMap="orderModelMap2">
<![CDATA[
SELECT
a.id ,
a.user_id,
a.create_time,
a.up_time
FROM
orders a
WHERE
a.id = #{value}
]]>
</select>

重点注意上面配置中association、collection这2个元素的fetchType属性,eager表示立即加载,lazy表示延迟加载。

测试用例

com.zhonghu.chat09.demo5.Demo5Test#getById2
@Test
public void getById2() throws IOException {
//指定mybatis全局配置文件
mybatisConfig = "demo5/mybatis-config2.xml";
this.before();
OrderModel orderModel = null;
try (SqlSession sqlSession = this.sqlSessionFactory.openSession(true);) {
OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
orderModel = mapper.getById2(1);
}
log.info("-------分割线--------");
log.info("{}", orderModel.getOrderDetailModelList());
}

运行输出

36:54.284 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById2 - ==> Preparing: SELECT a.id , a.user_id, a.create_time, a.up_time FROM orders a WHERE a.id = ?
36:54.321 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById2 - ==> Parameters: 1(Integer)
36:54.385 [main] DEBUG c.j.c.d.mapper.UserMapper.getById1 - ====> Preparing: SELECT id,name FROM user where id = ?
36:54.385 [main] DEBUG c.j.c.d.mapper.UserMapper.getById1 - ====> Parameters: 2(Integer)
36:54.387 [main] DEBUG c.j.c.d.mapper.UserMapper.getById1 - <==== Total: 1
36:54.389 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById2 - <== Total: 1
36:54.390 [main] INFO c.j.chat05.demo5.Demo5Test - -------分割线--------
36:54.392 [main] DEBUG c.j.c.d.m.O.getListByOrderId1 - ==> Preparing: SELECT a.id, a.order_id AS orderId, a.goods_id AS goodsId, a.num, a.total_price AS totalPrice FROM order_detail a WHERE a.order_id = ?
36:54.392 [main] DEBUG c.j.c.d.m.O.getListByOrderId1 - ==> Parameters: 1(Integer)
36:54.397 [main] DEBUG c.j.c.d.m.O.getListByOrderId1 - <== Total: 2
36:54.398 [main] INFO c.j.chat05.demo5.Demo5Test - [OrderDetailModel(id=1, orderId=1, goodsId=1, num=2, totalPrice=16.00), OrderDetailModel(id=2, orderId=1, goodsId=1, num=1, totalPrice=16.00)]

注意输出中的分割线,可以分析得出,用户信息是和订单信息一起立即查出来的,而订单详情,是在我们调用orderModel.getOrderDetailModelList()获取订单列表的时候,采取懒加载的。

鉴别器(discriminator)

有时候,一个数据库查询可能会返回多个不同的结果集(但总体上还是有一定的联系的), 鉴别器(discriminator)元素就是被设计来应对这种情况的,鉴别器的概念很好理解——它很像 Java 语言中的 switch 语句。

discriminator标签常用的两个属性如下:

  • column:该属性用于设置要进行鉴别比较值的列。
  • javaType:该属性用于指定列的类型,保证使用相同的java类型来比较值。

discriminator标签可以有1个或多个case标签,case标签有一个比较重要的属性:

  • value:该值为discriminator指定column用来匹配的值,当匹配的时候,结果会走这个case关联的映射。

我们使用鉴别器实现一个功能:通过订单id查询订单信息,当传入的订单id为1的时候,获取订单信息及下单人信息;当传入的订单id为2的时候,获取订单信息、下单人信息、订单明细信息;其他情况默认只查询订单信息。

OrderMapper.xml

<resultMap id="orderModelMap1" type="com.zhonghu.chat09.demo6.model.OrderModel">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="create_time" property="createTime"/>
<result column="up_time" property="upTime"/>
<!-- 鉴别器 -->
<discriminator javaType="int" column="id">
<case value="1">
<!--通过用户id查询用户信息-->
<association property="userModel" select="com.zhonghu.chat09.demo6.mapper.UserMapper.getById1" column="user_Id"/>
</case>
<case value="2">
<!--通过用户id查询用户信息-->
<association property="userModel" select="com.zhonghu.chat09.demo6.mapper.UserMapper.getById1" column="user_Id"/>
<!--通过订单id查询订单列表-->
<collection property="orderDetailModelList" select="com.zhonghu.chat09.demo6.mapper.OrderDetailMapper.getListByOrderId1" column="id"/>
</case>
</discriminator>
</resultMap>
<select id="getById1" resultMap="orderModelMap1">
<![CDATA[
SELECT
a.id ,
a.user_id,
a.create_time,
a.up_time
FROM
orders a
WHERE
a.id = #{value}
]]>
</select>

注意上面的discriminator,这部分是关键,discriminator内部的case会和每行查询结果中的id字段进行匹配,匹配成功了case内部的关联查询会被执行,未匹配上的,只会走discriminator外部默认配置的映射映射规则。

UserMapper.xml

<!-- 通过用户id查询用户信息 -->
<select id="getById1" resultType="com.zhonghu.chat09.demo6.model.UserModel">
<![CDATA[
SELECT id,name FROM user where id = #{user_id}
]]>
</select>

OrderDetailMapper.xml

<!-- 通过订单id查询订单明细列表 -->
<select id="getListByOrderId1" resultType="com.zhonghu.chat09.demo6.model.OrderDetailModel">
<![CDATA[
SELECT
a.id,
a.order_id AS orderId,
a.goods_id AS goodsId,
a.num,
a.total_price AS totalPrice
FROM
order_detail a
WHERE
a.order_id = #{order_id}
]]>
</select>

对应的3个Model类

@Getter
@Setter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class OrderModel {
private Integer id;
private Integer userId;
private Long createTime;
private Long upTime;
//用户信息
private UserModel userModel;
//订单详情列表
private List<OrderDetailModel> orderDetailModelList;
}
@Getter
@Setter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class UserModel {
private Integer id;
private String name;
}
@Getter
@Setter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class OrderDetailModel {
private Integer id;
private Integer orderId;
private Integer goodsId;
private Integer num;
private Double totalPrice;
}

测试用例

com.zhonghu.chat09.demo6.Demo6Test#getById1
@Test
public void getById1() throws IOException {
try (SqlSession sqlSession = this.sqlSessionFactory.openSession(true);) {
OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
//查询订单为1的
OrderModel orderModel = mapper.getById1(1);
log.info("{}", orderModel);
log.info("------------------------------------------------------------");
//查询订单为2的
orderModel = mapper.getById1(2);
log.info("{}", orderModel);
log.info("------------------------------------------------------------");
//查询订单为3的
orderModel = mapper.getById1(3);
log.info("{}", orderModel);
}
}

运行输出

58:16.413 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById1 - ==> Preparing: SELECT a.id , a.user_id, a.create_time, a.up_time FROM orders a WHERE a.id = ?
58:16.457 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById1 - ==> Parameters: 1(Integer)
58:16.481 [main] DEBUG c.j.c.d.mapper.UserMapper.getById1 - ====> Preparing: SELECT id,name FROM user where id = ?
58:16.481 [main] DEBUG c.j.c.d.mapper.UserMapper.getById1 - ====> Parameters: 2(Integer)
58:16.488 [main] DEBUG c.j.c.d.mapper.UserMapper.getById1 - <==== Total: 1
58:16.489 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById1 - <== Total: 1
58:16.489 [main] INFO c.j.chat05.demo6.Demo6Test - OrderModel(id=1, userId=2, createTime=1610803573, upTime=1610803573, userModel=UserModel(id=2, name=Java冢狐), orderDetailModelList=null)
58:16.491 [main] INFO c.j.chat05.demo6.Demo6Test - ------------------------------------------------------------
58:16.491 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById1 - ==> Preparing: SELECT a.id , a.user_id, a.create_time, a.up_time FROM orders a WHERE a.id = ?
58:16.492 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById1 - ==> Parameters: 2(Integer)
58:16.493 [main] DEBUG c.j.c.d.mapper.UserMapper.getById1 - ====> Preparing: SELECT id,name FROM user where id = ?
58:16.493 [main] DEBUG c.j.c.d.mapper.UserMapper.getById1 - ====> Parameters: 1(Integer)
58:16.494 [main] DEBUG c.j.c.d.mapper.UserMapper.getById1 - <==== Total: 1
58:16.495 [main] DEBUG c.j.c.d.m.O.getListByOrderId1 - ====> Preparing: SELECT a.id, a.order_id AS orderId, a.goods_id AS goodsId, a.num, a.total_price AS totalPrice FROM order_detail a WHERE a.order_id = ?
58:16.495 [main] DEBUG c.j.c.d.m.O.getListByOrderId1 - ====> Parameters: 2(Integer)
58:16.505 [main] DEBUG c.j.c.d.m.O.getListByOrderId1 - <==== Total: 1
58:16.505 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById1 - <== Total: 1
58:16.506 [main] INFO c.j.chat05.demo6.Demo6Test - OrderModel(id=2, userId=1, createTime=1610803573, upTime=1610803573, userModel=UserModel(id=1, name=冢狐), orderDetailModelList=[OrderDetailModel(id=3, orderId=2, goodsId=1, num=1, totalPrice=8.00)])
58:16.506 [main] INFO c.j.chat05.demo6.Demo6Test - ------------------------------------------------------------
58:16.506 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById1 - ==> Preparing: SELECT a.id , a.user_id, a.create_time, a.up_time FROM orders a WHERE a.id = ?
58:16.506 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById1 - ==> Parameters: 3(Integer)
58:16.508 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById1 - <== Total: 1
58:16.509 [main] INFO c.j.chat05.demo6.Demo6Test - OrderModel(id=3, userId=1, createTime=1610803573, upTime=1610803573, userModel=null, orderDetailModelList=null)

输出中可以看出,订单1查询了2次,订单2查询了3次,订单3查询了1次;鉴别器算是一个不错的功能。

继承(extends)

继承在java是三大特性之一,可以起到重用代码的作用,而mybatis也有继承的功能,和java中的继承的作用类似,主要在resultMap中使用,可以重用其他resultMap中配置的映射关系。

用法

<resultMap extends="被继承的resultMap的id"></resultMap>

案例

下面我们使用继承来对上面的鉴别器的案例改造一下,优化一下代码

OrderMapper.xml
<resultMap id="orderModelMap2" type="com.zhonghu.chat09.demo6.model.OrderModel">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="create_time" property="createTime"/>
<result column="up_time" property="upTime"/>
<!-- 鉴别器 -->
<discriminator javaType="int" column="id">
<case value="1" resultMap="orderModelMap3" />
<case value="2" resultMap="orderModelMap4" />
</discriminator>
</resultMap>
<resultMap id="orderModelMap3" type="com.zhonghu.chat09.demo6.model.OrderModel" extends="orderModelMap2">
<!--通过用户id查询用户信息-->
<association property="userModel" select="com.zhonghu.chat09.demo6.mapper.UserMapper.getById1" column="user_Id"/>
</resultMap>
<resultMap id="orderModelMap4" type="com.zhonghu.chat09.demo6.model.OrderModel" extends="orderModelMap3">
<!--通过订单id查询订单列表-->
<collection property="orderDetailModelList" select="com.zhonghu.chat09.demo6.mapper.OrderDetailMapper.getListByOrderId1" column="id"/>
</resultMap>
<select id="getById2" resultMap="orderModelMap2">
<![CDATA[
SELECT
a.id ,
a.user_id,
a.create_time,
a.up_time
FROM
orders a
WHERE
a.id = #{value}
]]>
</select>

重点在于上面两个extends属性,上面orderModelMap3继承了orderModelMap2中配置的映射关系(除鉴别器之外),自己又加入了一个association去查询用户信息;orderModelMap4继承了orderModelMap3,自己又加入了一个查询订单列表的collection元素。上面使用extends做到了代码重用,其实和下面这块代码写法效果一样:

<resultMap id="orderModelMap2" type="com.zhonghu.chat09.demo6.model.OrderModel">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="create_time" property="createTime"/>
<result column="up_time" property="upTime"/>
<!-- 鉴别器 -->
<discriminator javaType="int" column="id">
<case value="1" resultMap="orderModelMap3" />
<case value="2" resultMap="orderModelMap4" />
</discriminator>
</resultMap>
<resultMap id="orderModelMap3" type="com.zhonghu.chat09.demo6.model.OrderModel">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="create_time" property="createTime"/>
<result column="up_time" property="upTime"/>
<!--通过用户id查询用户信息-->
<association property="userModel" select="com.zhonghu.chat09.demo6.mapper.UserMapper.getById1" column="user_Id"/>
</resultMap>
<resultMap id="orderModelMap4" type="com.zhonghu.chat09.demo6.model.OrderModel">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="create_time" property="createTime"/>
<result column="up_time" property="upTime"/>
<!--通过用户id查询用户信息-->
<association property="userModel" select="com.zhonghu.chat09.demo6.mapper.UserMapper.getById1" column="user_Id"/>
<!--通过订单id查询订单列表-->
<collection property="orderDetailModelList" select="com.zhonghu.chat09.demo6.mapper.OrderDetailMapper.getListByOrderId1" column="id"/>
</resultMap>
<select id="getById2" resultMap="orderModelMap2">
<![CDATA[
SELECT
a.id ,
a.user_id,
a.create_time,
a.up_time
FROM
orders a
WHERE
a.id = #{value}
]]>
</select>
测试用例
com.zhonghu.chat09.demo6.Demo6Test#getById2
@Test
public void getById2() throws IOException {
try (SqlSession sqlSession = this.sqlSessionFactory.openSession(true);) {
OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
//查询订单为1的
OrderModel orderModel = mapper.getById2(1);
log.info("{}", orderModel);
log.info("------------------------------------------------------------");
//查询订单为2的
orderModel = mapper.getById2(2);
log.info("{}", orderModel);
log.info("------------------------------------------------------------");
//查询订单为3的
orderModel = mapper.getById2(3);
log.info("{}", orderModel);
}
}
运行输出
39:55.936 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById2 - ==> Preparing: SELECT a.id , a.user_id, a.create_time, a.up_time FROM orders a WHERE a.id = ?
39:55.969 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById2 - ==> Parameters: 1(Integer)
39:55.986 [main] DEBUG c.j.c.d.mapper.UserMapper.getById1 - ====> Preparing: SELECT id,name FROM user where id = ?
39:55.987 [main] DEBUG c.j.c.d.mapper.UserMapper.getById1 - ====> Parameters: 2(Integer)
39:55.992 [main] DEBUG c.j.c.d.mapper.UserMapper.getById1 - <==== Total: 1
39:55.993 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById2 - <== Total: 1
39:55.993 [main] INFO c.j.chat05.demo6.Demo6Test - OrderModel(id=1, userId=2, createTime=1610803573, upTime=1610803573, userModel=UserModel(id=2, name=Java冢狐), orderDetailModelList=null)
39:55.994 [main] INFO c.j.chat05.demo6.Demo6Test - ------------------------------------------------------------
39:55.994 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById2 - ==> Preparing: SELECT a.id , a.user_id, a.create_time, a.up_time FROM orders a WHERE a.id = ?
39:55.995 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById2 - ==> Parameters: 2(Integer)
39:55.995 [main] DEBUG c.j.c.d.m.O.getListByOrderId1 - ====> Preparing: SELECT a.id, a.order_id AS orderId, a.goods_id AS goodsId, a.num, a.total_price AS totalPrice FROM order_detail a WHERE a.order_id = ?
39:55.996 [main] DEBUG c.j.c.d.m.O.getListByOrderId1 - ====> Parameters: 2(Integer)
39:56.000 [main] DEBUG c.j.c.d.m.O.getListByOrderId1 - <==== Total: 1
39:56.001 [main] DEBUG c.j.c.d.mapper.UserMapper.getById1 - ====> Preparing: SELECT id,name FROM user where id = ?
39:56.004 [main] DEBUG c.j.c.d.mapper.UserMapper.getById1 - ====> Parameters: 1(Integer)
39:56.005 [main] DEBUG c.j.c.d.mapper.UserMapper.getById1 - <==== Total: 1
39:56.005 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById2 - <== Total: 1
39:56.005 [main] INFO c.j.chat05.demo6.Demo6Test - OrderModel(id=2, userId=1, createTime=1610803573, upTime=1610803573, userModel=UserModel(id=1, name=冢狐), orderDetailModelList=[OrderDetailModel(id=3, orderId=2, goodsId=1, num=1, totalPrice=8.00)])
39:56.005 [main] INFO c.j.chat05.demo6.Demo6Test - ------------------------------------------------------------
39:56.005 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById2 - ==> Preparing: SELECT a.id , a.user_id, a.create_time, a.up_time FROM orders a WHERE a.id = ?
39:56.006 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById2 - ==> Parameters: 3(Integer)
39:56.007 [main] DEBUG c.j.c.d.mapper.OrderMapper.getById2 - <== Total: 1
39:56.007 [main] INFO c.j.chat05.demo6.Demo6Test - OrderModel(id=3, userId=1, createTime=1610803573, upTime=1610803573, userModel=null, orderDetailModelList=null)

总结

本篇开篇重点介绍了有关MyBatis的延迟加载的相关的内容,对其中的全局延迟加载和部分延迟加载进行了注重的介绍,介绍完延迟加载后,又介绍了一些与之相关的鉴别器和继承的相关内容。

最后

  • 如果觉得看完有收获,希望能关注一下,顺便给我点个赞,这将会是我更新的最大动力,感谢各位的支持
  • 欢迎各位关注我的公众号【java冢狐】,专注于java和计算机基础知识,保证让你看完有所收获,不信你打我
  • 求一键三连:点赞、转发、在看。
  • 如果看完有不同的意见或者建议,欢迎多多评论一起交流。感谢各位的支持以及厚爱。

——我是冢狐,和你一样热爱编程。

欢迎关注公众号“Java冢狐”获取最新消息

版权声明
本文为[冢狐]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/javazhonghu/p/14437466.html

  1. JAVA的函数式接口
  2. JAVA里的元祖类
  3. JQuery Gantt package to create a new ASP.NET project
  4. Binary conversion of Unicode code (Java)
  5. The functional interface of Java
  6. Yuanzu class in Java
  7. Java中的CPU占用高和内存占用高的问题排查
  8. HashMap连环问你能答出几道?
  9. IntelliJ IDEA 还能画思维导图,果然最强 IDE!
  10. java中的反射和代理
  11. Troubleshooting of high CPU and memory usage in Java
  12. How many questions can you answer?
  13. IntelliJ idea can also draw mind maps. It's really the strongest ide!
  14. Reflection and proxy in Java
  15. Java中的CPU占用高和内存占用高的问题排查
  16. Linux OOM(out of memory)
  17. mysql 自定义函数因参数名称报错
  18. Troubleshooting of high CPU and memory usage in Java
  19. Linux OOM(out of memory)
  20. MySQL user defined function error due to parameter name
  21. echarts-gl 3D 地图柱状图可视化GDP
  22. Visualization of histogram of ecarts GL 3D map
  23. 金三银四如何应对Redis面试,一文深入Redis实战实践!
  24. 阿里资深架构师定制金三银四面试整理出来的一份Java核心知识点.pdf
  25. 为什么Java开发工程师工资高,却很多人想转行?
  26. How to deal with the interview of redis!
  27. Ali senior architect customizes a Java core knowledge point sorted out in the interview of golden, silver and four.pdf
  28. Why do java development engineers have high salaries, but many people want to change careers?
  29. 用注解开发SpringMVC
  30. Developing spring MVC with annotations
  31. 编译redis报错/deps/hiredis/libhiredis.a解决
  32. Compile redis report error / DEPs / hirredis / libhirredis. A solution
  33. 用注解开发SpringMVC
  34. Developing spring MVC with annotations
  35. Spring学习笔记-01
  36. Centos安装和卸载docker
  37. Spring learning notes-01
  38. Installing and uninstalling docker in CentOS
  39. Java基础-异常
  40. Java基础-反射
  41. Java基础-继承
  42. k8s部署 (进行中)
  43. Hive-常见调优方式 && 两个面试sql
  44. 死磕Spring之IoC篇 - BeanDefinition 的加载阶段(XML 文件)
  45. Java basics exception
  46. Java Basics - Reflection
  47. Java Basics - inheritance
  48. K8s deployment (in progress)
  49. Hive common tuning methods & two interview SQL
  50. The loading phase of beandefinition (XML file)
  51. 死磕Spring之IoC篇 - BeanDefinition 的加载阶段(XML 文件)
  52. Hive-常见调优方式 && 两个面试sql
  53. The loading phase of beandefinition (XML file)
  54. Hive common tuning methods & two interview SQL
  55. iconv文件编码转换由windows文件放到linux下
  56. The code conversion of iconv file is put into Linux by windows file
  57. SpringBoot2+intellij IDEA开发前环境准备
  58. Preparation of pre development environment for springboot2 + IntelliJ idea
  59. Docker私有仓库部署
  60. Docker private warehouse deployment