(9) Mybatis from the beginning to the end -- delay loading, discriminator and inheritance

Fox in the grave 2021-02-23 18:21:51
mybatis beginning end delay loading


This is a mybatis The first series 9 piece , Go ahead without looking at the previous suggestions 【Java Grave Fox 】 The official account shows the previous text , Easy to understand and master . In the last article we talked about MyBatis How to turn on and use the automatic mapping of , I think you know something about this .

What we are going to bring to you today is MyBatis Delay loading and discriminator related knowledge and content .

Delay loading

Introduction to delayed loading

The so-called delayed loading is to delay the time of data loading , One of the typical applications is to delay the execution of nested queries .

Because in mybatis Association query is often used in , But it's not always necessary to return the associated query results immediately . For example, query the order information , It is not necessary to return the corresponding user information or order details in time , So when we come across this situation, we need a mechanism , When you need to view the associated data , Then execute the corresponding query , Return required results .

This kind of demand is in the market mybatis You can use the delayed loading mechanism to implement .

Delay loading 2 Settings

MyBatis There are two settings for delayed loading , Namely :

  • The way of global configuration
  • sqlmap The way of configuration in

From the names of the two methods, we can see the difference between the two kinds of delayed loading , The first method will work for all associated queries , The second method is only effective for the query of related settings .

Let's take a look at these two ways of using delayed loading .

Global configuration delay loading

In order to implement global configuration delay loading, we need to go through mybatis Configuration file for .

mybatis In the configuration file, the delay loading is controlled by the following two properties :

<settings>
<!-- Turn on the delay loading switch -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- When it comes to true When , Call any delay property , Will load all delay properties , If false, When calling a property , Only the specified properties are loaded -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
  • lazyLoadingEnabled: This attribute is easy to understand , Whether to turn on delay loading , The default is false, If you need to turn on delayed loading , Set it to true
  • aggressiveLazyLoading: When it comes to true When , Call any delay property , Will load all delay properties , If false, When calling a property , Only the specified properties are loaded

That's all about global configuration delay loading , Now let's use a specific example to illustrate how to use global configuration delay loading .

demand

This time we're going to use MyBatis The realization of the demand is through the order id Query all kinds of information about the order , Such as : Order user information 、 List of order details . The order user information and order details information are obtained by delayed loading .

mybatis To configure

According to the previous introduction, the first step is to pass mybatis Configuration file to set up . As shown below :

<settings>
<!-- Turn on the delay loading switch -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- When it comes to true When , Call any delay property , Will load all delay properties , If false, When calling a property , Only the specified properties are loaded -->
<setting name="aggressiveLazyLoading" value="true"/>
</settings>

OrderMapper.xml

After setting the global configuration , We'll take our normal development approach , The first is to write xml file , stay xml We write our sql sentence .

<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"/>
<!-- Through the order user_id As a condition , Check the information of single person -->
<association property="userModel" select="com.zhonghu.chat09.demo5.mapper.UserMapper.getById1" column="user_Id"/>
<!-- By order id As a condition , Check out the list of details -->
<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>

above orderModelMap1 There are two associated queries under the element , Let's also write about .

UserMapper.xml

<!-- According to the user id Query user information -->
<select id="getById1" resultType="com.zhonghu.chat09.demo5.model.UserModel">
<![CDATA[
SELECT id,name FROM user where id = #{user_id}
]]>
</select>

OrderDetailMapper.xml

<!-- According to the order di Query order details list -->
<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>

Corresponding 3 individual Model

We wrote three on it xml, So let's write it xml Corresponding 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;
// List of order details
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;
}

The test case

Finished writing Model, Our code is basically done , Next, let's take a look at the effect of delayed loading .

com.zhonghu.chat09.demo5.Demo5Test#getById1
@Test
public void getById1() throws IOException {
// Appoint mybatis Global profile
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("------- Split line --------");
log.info("{}", orderModel.getUserModel());
}

Operation output

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 - ------- Split line --------
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 Grave Fox )

As can be seen from the log , All in all 3 Queries , Back 2 The second query appears after the split line , The description is called orderModel.getUserModel() Behind the trigger 2 First query action .

What we call in the code is to get user information , The order list information is also loaded , This is mainly due to aggressiveLazyLoading Set to true 了 , When a delay loaded property is used , Other delay loaded properties will also be loaded together , So it triggers 2 An associated query .

Now let's take a look at aggressiveLazyLoading Set to false The effect of

<settings>
<!-- Turn on the delay loading switch -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- When it comes to true When , Call any delay property , Will load all delay properties , If false, When calling a property , Only the specified properties are loaded -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>

Run the test case output again

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 - ------- Split line --------
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 Grave Fox )

Through these two comparisons , We can easily see the difference between the effect of delay loading on and off .

sqlmap Set delay loading in

In the space above, we introduced how global delay loading works and how to use it . A global approach works for all associated queries , The scope of influence is relatively large ,mybatis It also provides a way to set in the associated query , It only works on the currently set associated query .

Relational query , Generally we use association、collection, Both elements have an attribute fetchType, This property allows you to specify how to load the associated query .

fetchType There are two values

  • eager: Immediately load
  • lazy: Delay loading

Now let's implement a requirement : Or by order id Query order information , And get the associated user information 、 Detailed list of orders , We need to load user information immediately , And for order details, we ask for delayed loading .

mapper xml as follows

<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"/>
<!-- Through the order user_id As a condition , Check the information of single person -->
<association property="userModel" fetchType="eager" select="com.zhonghu.chat09.demo5.mapper.UserMapper.getById1" column="user_Id"/>
<!-- By order id As a condition , Check out the list of details -->
<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>

Focus on the configuration above association、collection this 2 An element of fetchType attribute ,eager Means to load immediately ,lazy Indicates delayed loading .

The test case

com.zhonghu.chat09.demo5.Demo5Test#getById2
@Test
public void getById2() throws IOException {
// Appoint mybatis Global profile
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("------- Split line --------");
log.info("{}", orderModel.getOrderDetailModelList());
}

Operation output

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 - ------- Split line --------
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)]

Notice the split line in the output , It can be concluded that , The user information is found immediately together with the order information , And the order details , It's when we call orderModel.getOrderDetailModelList() When getting the order list , Take lazy loading .

Discriminator (discriminator)

occasionally , A database query may return multiple different result sets ( But on the whole, there is a certain connection ), Discriminator (discriminator) Elements are designed to deal with this situation , The concept of discriminator is easy to understand —— It's very much like Java In language switch sentence .

discriminator Two common attributes of tags are as follows :

  • column: This property is used to set the columns to be discriminated against .
  • javaType: This property is used to specify the type of the column , Make sure to use the same java Type to compare values .

discriminator Labels can have 1 One or more case label ,case Tags have an important attribute :

  • value: The value is discriminator Appoint column The value used to match , When it comes to matching , It turns out to be this case Associated mapping .

We use discriminator to implement a function : By order id Query order information , When an incoming order id by 1 When , Get the order information and the order information ; When an incoming order id by 2 When , Get order information 、 Next person information 、 Order details ; In other cases, only order information is queried by default .

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 -->
<discriminator javaType="int" column="id">
<case value="1">
<!-- Through users id Query user information -->
<association property="userModel" select="com.zhonghu.chat09.demo6.mapper.UserMapper.getById1" column="user_Id"/>
</case>
<case value="2">
<!-- Through users id Query user information -->
<association property="userModel" select="com.zhonghu.chat09.demo6.mapper.UserMapper.getById1" column="user_Id"/>
<!-- By order id Check the order list -->
<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>

Notice the top discriminator, This part is the key ,discriminator Inside case It will be compared with the id Field to match , The match was successful case Internal associated queries are executed , Unmatched on , I can only walk discriminator Mapping rules for external default configuration .

UserMapper.xml

<!-- Through users id Query user information -->
<select id="getById1" resultType="com.zhonghu.chat09.demo6.model.UserModel">
<![CDATA[
SELECT id,name FROM user where id = #{user_id}
]]>
</select>

OrderDetailMapper.xml

<!-- By order id Query order details list -->
<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>

Corresponding 3 individual Model class

@Getter
@Setter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class OrderModel {
private Integer id;
private Integer userId;
private Long createTime;
private Long upTime;
// User information
private UserModel userModel;
// List of order details
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;
}

The test case

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);
// The inquiry order is 1 Of
OrderModel orderModel = mapper.getById1(1);
log.info("{}", orderModel);
log.info("------------------------------------------------------------");
// The inquiry order is 2 Of
orderModel = mapper.getById1(2);
log.info("{}", orderModel);
log.info("------------------------------------------------------------");
// The inquiry order is 3 Of
orderModel = mapper.getById1(3);
log.info("{}", orderModel);
}
}

Operation output

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 Grave Fox ), 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= Grave Fox ), 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)

You can see from the output that , Order 1 The query 2 Time , Order 2 The query 3 Time , Order 3 The query 1 Time ; Discriminator is a good function .

Inherit (extends)

Inherit in java It's one of the three characteristics , Can play the role of reusing code , and mybatis It also has inherited functions , and java The role of inheritance in is similar to , Mainly in the resultMap Use in , You can reuse other resultMap Mapping relations configured in .

usage

<resultMap extends=" Inherited resultMap Of id"></resultMap>

Case study

Now let's use inheritance to transform the above discriminator case , Optimize the code

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 -->
<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">
<!-- Through users id Query user information -->
<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">
<!-- By order id Check the order list -->
<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>

The point is the two above extends attribute , above orderModelMap3 Inherited orderModelMap2 Mapping relations configured in ( In addition to the discriminator ), I joined another one association To query user information ;orderModelMap4 Inherited orderModelMap3, I added a query order list collection Elements . The use of extends Code reuse is achieved , In fact, it has the same effect as the following code :

<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 -->
<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"/>
<!-- Through users id Query user information -->
<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"/>
<!-- Through users id Query user information -->
<association property="userModel" select="com.zhonghu.chat09.demo6.mapper.UserMapper.getById1" column="user_Id"/>
<!-- By order id Check the order list -->
<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>
The test case
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);
// The inquiry order is 1 Of
OrderModel orderModel = mapper.getById2(1);
log.info("{}", orderModel);
log.info("------------------------------------------------------------");
// The inquiry order is 2 Of
orderModel = mapper.getById2(2);
log.info("{}", orderModel);
log.info("------------------------------------------------------------");
// The inquiry order is 3 Of
orderModel = mapper.getById2(3);
log.info("{}", orderModel);
}
}
Operation output
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 Grave Fox ), 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= Grave Fox ), 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)

summary

At the beginning of this article, we focus on MyBatis Delayed loading of related content , The global delay loading and partial delay loading are introduced , After introducing delayed loading , It also introduces some related discriminators and inheritance .

Last

  • If you think you've got something to watch , Hope to pay attention to , By the way, give me a compliment , This will be my biggest motivation to update , Thank you for your support
  • Welcome to my official account 【java Grave Fox 】, Focus on java And Computer Basics , I'm sure you'll get something from it , I don't believe you hit me
  • Ask for one key and three links : give the thumbs-up 、 forward 、 Looking at .
  • If you have different opinions or suggestions after reading , Welcome to comment and exchange with us . Thank you for your support and love .

—— I'm Suzuka fox , I love programming as much as you do .

Welcome to the official account “Java Grave Fox ” Get the latest news

版权声明
本文为[Fox in the grave]所创,转载请带上原文链接,感谢
https://javamana.com/2021/02/20210223182104065P.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