MyBatis learning-delay loading, mybatis-Delay

Source: Internet
Author: User

MyBatis learning-delay loading, mybatis-Delay

  • Introduction

  In resultMap, advanced ing can be implemented (one-to-one and one-to-many ing can be implemented using association and collection). association and collection have the delayed loading function. For example, we query the order and associate it with the user information. If you want to query the order information first, you can query the user information. Querying user information on demand is a delayed loading.

Delayed loading: queries from a single table and associated tables are performed when necessary, which greatly improves database performance because querying a single table is faster than querying multiple associated tables.

  • Association for delayed Loading

  If you want to query order information and associated user information, you only need to query order information without viewing user information. If you want to view user information, you need to query user information.

1. ing file:

You need to define the statement corresponding to two mapper methods. One is used for simple queries, and the other is used for queries when queries are required.

(1) only query order information

1 <select id="findOrderLazyLoad" resultMap="ordersUserResultMapLazyLoad">2         select * from3         orders4 </select>

(2) query user information by association:

1 <select id="findUserLazyLoad" parameterType="int" resultType="user">2         select * from user where id = #{value}3 </select>

Run findOrderLazyLoad first, and then run findUserLazyLoad when you need to query the user. The delayed loading execution configuration is configured through the resultMap definition.

 

1 <! -- Associate the order query with the user's resultMap, map the entire query result to the orders, and delay loading --> 2 <resultMap type = "com. luchao. mybatis. first. po. orders "id =" ordersUserResultMapLazyLoad "> 3 <id column =" id "property =" id "/> 4 <result column =" user_id "property =" userId "/> 5 <result column = "number" property = "number"/> 6 <result column = "createtime" property = "createtime"/> 7 <result column = "note" property = "note "/> 8 <! -- Latency loading of User Information select: Specifies the id of the statement to be executed for delayed loading (the statement for querying user information based on user_id) 9. userMapper is used. in xml, findUserById is used to query user information based on the user ID (user_id). If findUserById is not in this mapper, you need to add namespace 10 column in front of it: the column associated with user information query in order information, the SQL statement for user_id association query is interpreted as: SELECT orders. *, (SELECT username 11 from user where orders. user_id = user. id) username, (SELECT sex from user 12 WHERE orders. user_id = user. id) sex FROM orders --> 13 <association property = "user" javaType = "com. luchao. mybatis. first. po. user "14 select =" findUserLazyLoad "column =" user_id "> 15 16 </association> 17 </resultMap>

 

Select: Specifies the id of the statement to be executed for delayed loading (it is the statement for querying user information based on user_id). userMapper is used. in xml, findUserById is used to query user information based on the user ID (user_id). If findUserById is not in the mapper, you need to add namespace in front and column: the column associated with user information query in order information, which is user_id.

The SQL statement of the associated query can be understood:

 

1 SELECT orders.*,2     (SELECT username FROM USER WHERE orders.user_id = user.id)username,3     (SELECT sex FROM USER WHERE orders.user_id = user.id)sex4 FROM orders

 

2. Mapper interface:

1 // query order, order details, and user information by using resultMap to delay loading 2 public List <Orders> findOrderLazyLoad () throws Exception;

3. Delayed loading Configuration:

By default, delayed loading is not enabled for mybatis. You must configure setting in SqlMapConfig. xml. Configure lazyLoadingEnabled and aggressiveLazyLoading in the mybatis core configuration file.

Settings

Description

Allowed value

Default Value

LazyLoadingEnabled

Global settings are lazy. If it is set to 'false', all associated objects will be initialized and loaded.

True | false

False

AggressiveLazyLoading

When it is set to 'true', the lazy objects may be loaded by all lazy attributes. Otherwise, each attribute is loaded as needed.

True | false

True

Configure in SqlMapConfig. xml

1 <settings> 2 <! -- Enable the delayed loading switch --> 3 <setting name = "lazyLoadingEnabled" value = "true"/> 4 <! -- Change positive loading to passive loading, that is, loading as needed --> 5 <setting name = "aggressiveLazyLoading" value = "false"/> 6 </settings>

4. test:

1 public void findOrderLazyLoad () throws Exception {2 // get sqlSession object 3 SqlSession sqlSession = sqlSessionFactory. openSession (); 4 // create the OrderMapper object. MyBatis automatically generates the mapper proxy 5 OrderMapper orderMapper = sqlSession. getMapper (OrderMapper. class); 6 // call the orderMapper method to query the order and user information and load the delayed 7 List <Orders> Orders = orderMapper. findOrderLazyLoad (); 8 for (Orders order: Orders) {9 System. out. println (order. getUser (); 10} 11}

Query results:

1 DEBUG [main]-==> Preparing: select * from orders 2 DEBUG [main]-==> Parameters: 3 DEBUG [main]-<= Total: 3 4 DEBUG [main]-==> Preparing: select * from user where id =? 5 DEBUG [main]-==> Parameters: 1 (Integer) 6 DEBUG [main]-<= Total: 1 7 1-Wang Wu-2-null 8 1-Wang Wu-2-null 9 DEBUG [main]-==> Preparing: select * from user where id =? 10 DEBUG [main]-==> Parameters: 10 (Integer) 11 DEBUG [main]-<= Total: 112 10-Zhang ming3-1-Beijing-Thu Jul 10 00:00:00 CST 2014

We can see that the orders table is queried first, and then the user is viewed again when the user is viewed.

  • Thoughts and conclusions on delayed Loading

If the association function provided by mybatis and the delayed Loading Function in collection are not used, how can we implement delayed loading ??

The implementation method is as follows:

Define two mapper methods: 1. query the order list; 2. query user information based on the user ID.

Implementation ideas:

First query the first mapper method to obtain the order information list.

In a program (service), call the second er method as needed to query user information.

In short: using the delayed loading method, you can first query simple SQL statements (preferably single tables or associated queries), and then load other information of associated queries as needed.

Summary:

Purpose: Query association information in the database. By default, association query is not performed to improve database performance. You can only use resultMap to support delayed loading settings.

Occasion: When only some records need to be associated with other information for query, loading can be delayed as needed. When associated queries are required, an SQL statement is sent to the database to improve database performance.

When all associated query information is required, you can directly return all associated query information without delay loading. You can use resultType or resultMap to complete the ing.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.