This is the ninth mybatis series. Without looking at the previous suggestions, we will go to the official account of the Java grave fox to read the foreword for easy understanding and mastery. In the previous article, we introduced how to start and use the automatic mapping of mybatis. I'm sure you know something about this knowledge.
What I want to bring to you today is the knowledge and content related to MyBatis delayed loading and discriminator.
Delayed loading
Introduction to delayed loading
The so-called delayed loading is to delay the time of data loading, and the typical application is to delay the execution time of nested queries.
Because the association query is often used in mybatis, but it is not necessary to return the association query results immediately at any time. For example, querying order information does not necessarily need to return the user information or order details corresponding to the order in time. Therefore, when we encounter this situation, we need a mechanism to execute the corresponding query and return the required results when we need to view the associated data.
This requirement can be realized by using the delayed loading mechanism in mybatis.
There are two settings for delayed loading
MyBatis provides two settings for delayed loading:
- Global configuration mode
- How to configure in sqlmap
From the names of the two methods, we can find the difference between the two delayed loading methods. The first method will be valid for all associated queries, while the second method will only be valid for queries with related settings.
Let's take a look at the use of these two delayed loading methods.
Global configuration deferred loading
To achieve global configuration delay loading, you need to use the configuration file of mybatis.
The mybatis configuration file controls delayed loading through the following two properties:
<settings> <!--Turn on the switch for delayed loading --> <setting name="lazyLoadingEnabled" value="true"/> <!-- should be true When calling any delay attribute, all delay attributes will be loaded. If it is false,When a property is called, only the specified property will be loaded --> <setting name="aggressiveLazyLoading" value="false"/> </settings>
- Lazyloading enabled: this attribute is easy to understand. Whether to enable delayed loading is false by default. If delayed loading needs to be enabled, set it to true
- aggressiveLazyLoading: when it is true, calling any delay attribute will load all delay attributes. If it is false, only the specified attributes will be loaded when calling an attribute
These are the descriptions of global configuration delay loading. Let's use a specific example to illustrate how to use global configuration delay loading.
demand
This time, we need to use MyBatis to query all kinds of order information through order id, such as order user information and order detail list. The order user information and order details are obtained by delayed loading.
mybatis configuration
According to the previous introduction, the first step is to set it through the configuration file of mybatis. As follows:
<settings> <!--Turn on the switch for delayed loading --> <setting name="lazyLoadingEnabled" value="true"/> <!-- should be true When calling any delay attribute, all delay attributes will be loaded. If it is false,When a property is called, only the specified property will be loaded --> <setting name="aggressiveLazyLoading" value="true"/> </settings>
OrderMapper.xml
After the global configuration is set, we will carry out our normal development. The first is to write xml files and write our sql statements in xml files.
<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 order user_id As a condition, query the next person information --> <association property="userModel" select="com.zhonghu.chat09.demo5.mapper.UserMapper.getById1" column="user_Id"/> <!-- By order id As a condition, query the details list --> <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>
There are two associated queries under the orderModelMap1 element above, which we will also write about.
UserMapper.xml
<!-- According to 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 models
We have written three xml above. Next, let's write the Model corresponding to xml.
@Getter @Setter @Builder @ToString @NoArgsConstructor @AllArgsConstructor public class OrderModel { private Integer id; private Integer userId; private Long createTime; private Long upTime; private UserModel userModel; //Order details list 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; }
test case
After writing the Model, our code is basically completed. Next, let's take a look at the effect of delayed loading.
com.zhonghu.chat09.demo5.Demo5Test#getById1 @Test public void getById1() throws IOException { //Specify 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()); }
Run 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 Tomb Fox)
It can be seen from the log that there are three queries in total, and the last two queries appear after the split line, indicating that ordermodel has been called Getusermodel() triggers the next two query actions.
In the code, we call to obtain user information, and the order list information is also loaded. This is mainly because aggressiveLazyLoading is set to true. When a delayed loading attribute is used, other delayed loading attributes will be loaded together, so as to trigger two associated queries.
Let's take a look at the effect of setting aggressiveLazyLoading to false
<settings> <!--Turn on the switch for delayed loading --> <setting name="lazyLoadingEnabled" value="true"/> <!-- should be true When calling any delay attribute, all delay attributes will be loaded. If it is false,When a property is called, only the specified property will be 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 Tomb Fox)
Through these two comparisons, we can easily see the difference between the effect of delayed loading on and off.
Setting deferred loading in sqlmap
In the above pages, we introduced how global deferred loading works and how to use it. The global method will be effective for all associated queries, which has a wide range of influence. mybatis also provides a way to set in the associated query, which will only be effective for the currently set associated query.
For association query, we generally use association and collection. These two elements have an attribute fetchType, which can specify the loading method of association query.
fetchType has two values
- eager: load now
- lazy: delay loading
Now let's realize a requirement: query the order information through the order id and obtain the associated user information and order detail list. We need to load the user information immediately, while we need to delay the loading of order details.
mapper xml is 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 order user_id As a condition, query the next person information --> <association property="userModel" fetchType="eager" select="com.zhonghu.chat09.demo5.mapper.UserMapper.getById1" column="user_Id"/> <!-- By order id As a condition, query the details list --> <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 fetchType attribute of association and collection in the above configuration. eager means immediate loading and lazy means delayed loading.
test case
com.zhonghu.chat09.demo5.Demo5Test#getById2 @Test public void getById2() throws IOException { //Specify 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()); }
Run 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)]
Pay attention to the split line in the output. It can be analyzed that the user information is found immediately together with the order information, and the order details are found when we call ordermodel When getorderdetailmodellist() gets the order list, lazy loading is adopted.
Discriminator
Sometimes, a database query may return multiple different result sets (but there are some connections on the whole). The discriminator element is designed to deal with this situation. The concept of discriminator is well understood - it is very similar to the switch statement in the Java language.
Two common attributes of discriminator tag are as follows:
- Column: this attribute is used to set the column of value to be discriminated and compared.
- javaType: this attribute is used to specify the type of column and ensure that the same java type is used to compare values.
The discriminator tag can have one or more case tags. The case tag has an important attribute:
- Value: this value specifies the matching value of the column for the discriminator. When matching, the result will follow the mapping associated with this case.
We use the discriminator to realize one function: query the order information through the order id. when the incoming order id is 1, obtain the order information and the issuer information; When the incoming order id is 2, get the order information, order issuer information and 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"> <!--By user id Query user information--> <association property="userModel" select="com.zhonghu.chat09.demo6.mapper.UserMapper.getById1" column="user_Id"/> </case> <case value="2"> <!--By user id Query user information--> <association property="userModel" select="com.zhonghu.chat09.demo6.mapper.UserMapper.getById1" column="user_Id"/> <!--By order id Query 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>
Note the discriminator above. This part is the key. The case inside the discriminator will match the id field in the query result of each row. If the matching is successful, the associated query inside the case will be executed. If it is not matched, only the mapping rules configured by default outside the discriminator will be followed.
UserMapper.xml
<!-- By user 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 three Model classes
@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; //Order details list 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; }
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); //Query the with order 1 OrderModel orderModel = mapper.getById1(1); log.info("{}", orderModel); log.info("------------------------------------------------------------"); //Query the with order 2 orderModel = mapper.getById1(2); log.info("{}", orderModel); log.info("------------------------------------------------------------"); //Query the with order 3 orderModel = mapper.getById1(3); log.info("{}", orderModel); } }
Run 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 Tomb 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=Tomb 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)
It can be seen from the output that order 1 has been queried twice, order 2 has been queried three times, and order 3 has been queried once; Discriminator is a good function.
Extensions
Inheritance is one of the three major features in java, which can play the role of reusing code. mybatis also has the function of inheritance, which is similar to the function of inheritance in java. It is mainly used in resultMap and can reuse the mapping relationship configured in other resultmaps.
usage
<resultMap extends="Inherited resultMap of id"></resultMap>
case
Let's use inheritance to transform the above discriminator case and 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"> <!--By user 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 Query 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 focus is on the above two extensions attributes. orderModelMap3 inherits the mapping relationship configured in orderModelMap2 (except the discriminator), and adds an association to query user information; orderModelMap4 inherits orderModelMap3 and adds a collection element to query the order list. The above uses extensions to achieve code reuse. In fact, the effect is the same as that of 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"/> <!--By user 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"/> <!--By user id Query user information--> <association property="userModel" select="com.zhonghu.chat09.demo6.mapper.UserMapper.getById1" column="user_Id"/> <!--By order id Query 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>
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); //Query the with order 1 OrderModel orderModel = mapper.getById2(1); log.info("{}", orderModel); log.info("------------------------------------------------------------"); //Query the with order 2 orderModel = mapper.getById2(2); log.info("{}", orderModel); log.info("------------------------------------------------------------"); //Query the with order 3 orderModel = mapper.getById2(3); log.info("{}", orderModel); } }
Run 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 Tomb 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=Tomb 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 chapter, we focus on the related contents of MyBatis delayed loading, and pay attention to the global delayed loading and partial delayed loading. After introducing the delayed loading, we also introduce some related discriminators and inheritance.
last
- If you think you can get something after reading it, I hope you can pay attention and give me a praise by the way. This will be the biggest driving force for me to update. Thank you for your support
- Welcome to my official account, java, focusing on java and computer knowledge, to ensure that you can see something, and not believe you hit me.
- Seek one key three links: like, forward and watch.
- If you have different opinions or suggestions after reading, you are welcome to comment and communicate together. Thank you for your support and love.
——I'm tsuka fox. I love programming as much as you do.
Welcome to the official account "Java Zhong Fox" for the latest news.