Spring + SpringMVC + MyBatis deep learning and setup (7) -- MyBatis delayed loading and springmvcmybatis
Reprinted please indicate the source: http://www.cnblogs.com/Joanna-Yan/p/6953005.html
As mentioned above: Spring + SpringMVC + MyBatis deep learning and Construction (6) -- MyBatis association query
1. What is delayed loading?
The resultMap can implement advanced ing (one-to-one and one-to-many ing using association and collection ),Association and collection functions support delayed loading.
Requirements:
If you query an order and associate it with the user information. If you want to query the order information first, you can check the user information when you need to 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.
2. Use association to implement latency loading of 2.1
Query orders and associate query user information
2.2mapper.xml
You need to define the statement corresponding to the two ER er methods.
(1) only query order information
SELECT * FROM orders
In the statement of the query orderUse association to delay loading (execution) the following statement (association query user information ).
<! -- Query order association query user --> <select id = "findOrdersUserLazyLoading" resultMap = "OrdersUserLazyLoadingResultMap"> SELECT * FROM orders </select>
(2) associate to query user information
Use user_id in the order information queried above to associate and query user information
Use findUserById in UserMapper. xml
<select id="findUserById" parameterType="int" resultType="user"> select * from user where id=#{value} </select>
Run findOrdersUserLazyLoading first, and then execute fingUserById when you need to query the user. Then, configure the delayed loading execution using the resultMap definition.
2.3 delayed loading of resultMap
Use select in association to specify the id of the statement for delayed loading to be executed.
<! -- Delayed loading of the resultMap --> <resultMap type = "joanna. yan. mybatis. entity. Orders" id = "OrdersUserLazyLoadingResultMap"> <! -- 1. ing the order information --> <id column = "id" property = "id"/> <result column = "user_id" property = "userId"/> <result column = "number" property = "number"/> <result column = "createtime" property = "createtime"/> <result column = "note" property = "note"/> <! -- 2. Implement delayed 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). 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, namespace must be added to the front. Column: the column in the order information associated with user information query. It is the SQL statement used for user_id association query. It is understood as: SELECT orders. *, (SELECT username from user where orders. user_id = user. id) username, (SELECT sex from user where orders. user_id = user. id) sex FROM orders --> <association property = "user" javaType = "joanna. yan. mybatis. entity. user "select =" joanna. yan. mybatis. mapper. userMapper. findUserById "column =" user_id "> </association> </resultMap>
2.4mapper.java
// Query order association query users. When user information is queried, the public List <Orders> findOrdersUserLazyLoading () throws Exception is delayed;
2.5 test 2.5.1 test ideas
(1) execute the above mapper method (findOrdersUserLazyLoading), internally Call joanna. yan. mybatis. mapper. OrdersCustomMapper in findOrdersUserLazyLoading to query only orders information (single table ).
(2) traverse the List <Orders> queried in the previous step in the program. When we call getUser () in Orders, we start to perform delayed loading.
(3) Delayed loading: Call findUserById in UserMapper. xml to obtain user information.
2.5.2 delayed loading Configuration
By default, delayed loading is not enabled for mybatis. You must configure setting in SqlMapConfig. xml.
Configure in the mybatis core configuration file:
LazyLoadingEnabled, aggressiveLazyLoading
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:
<! -- Global configuration parameters. If necessary, set --> <settings> <! -- Enable delayed loading --> <setting name = "lazyLoadingEnabled" value = "true"/> <! -- Change positive loading to passive loading, that is, loading as needed --> <setting name = "aggressiveLazyLoading" value = "false"/> </settings>
2.5.3 test code
@ Test public void findOrdersUserLazyLoadingTest () throws Exception {SqlSession sqlSession = sqlSessionFactory. openSession (); OrdersCustomMapper ordersCustomMapper = sqlSession. getMapper (OrdersCustomMapper. class); List <Orders> list = ordersCustomMapper. findOrdersUserLazyLoading (); for (Orders orders: list) {// execute getUser () to query User information. Here, user User = orders is loaded as needed. getUser (); System. out. println (user);} sqlSession. close ();}
2.6 thoughts on delayed Loading
How to Implement delayed loading without using the association and collection functions provided by mybatis?
The implementation method is as follows:
Define two ER er methods:
(1) query the order list
(2) query user information by user ID
Implementation idea: first query the first mapper method to obtain the order information list
In the test program, the second mapper method is called as needed to query user information.
In short, using the delayed loading method, you can first query a simple SQL statement (preferably a single table or associated query), and then load other information of the associated query as needed.
If this article is helpful to you, please let me know ~