Delayed loading of seven MyBatis

Source: Internet
Author: User

1 Lazy Load 1.1 What is lazy loading

Resultmap can implement advanced Mapping (using association, collection for one-to-one and one-to-many mappings),Associationand collection have lazy loading capabilities.

Demand:

Queries the user information if the order is queried and associated. If you query the order information to meet the requirements, when we need to query user information and then query user information. The on-demand query of user information is deferred loading.

Lazy Loading: The database performance is greatly improved by querying from a single-table query, when needed, and then from related tables, because it is faster to query a single table than to query multiple tables.

1.2 Using association for lazy loading

turn on the delay load switch

Configure in the MyBatis core configuration file:

Lazyloadingenabled, aggressivelazyloading

Setting items

Describe

Allowed values

Default value

Lazyloadingenabled

Global Settings lazy Loading. If set to ' false ', all associated will be initialized to load.

true | False

False

Aggressivelazyloading

When set to ' true ', lazy-loaded objects may be loaded by any lazy property. Otherwise, each property is loaded on demand.

true | False

True

1 <!--Global configuration parameters, when required in the configuration -2     <Settings>3         <!--Open deferred load switch, default does not turn on lazy loading -4         <settingname= "lazyloadingenabled"value= "true"/>5         <!--changes the decrement load to a negative load, which is loaded as needed, and the default is active loading -6         <settingname= "Aggressivelazyloading"value= "false"/>7     </Settings>

1.2.1 Demand

Querying orders and correlating query user information

1.2.2 Mapper.xml

You need to define a method corresponding to the mapper of two statement.

1, only inquiry order information

SELECT * FROM Orders

Use Association to delay loading (executing) satatement ( correlate query user information ) in statement of the query order

User.xml

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <!DOCTYPE Mapper3 Public "-//mybatis.org//dtd Mapper 3.0//en"4 "Http://mybatis.org/dtd/mybatis-3-mapper.dtd">5 <Mappernamespace= "Test">6     <SelectID= "Finduserbyid"ParameterType= "int"Resulttype= "Cn.itcast.po.User">7 SELECT * from user where Id=#{value}8     </Select>9 </Mapper>

2, related query user information

Query user information by USER_ID in the order information above

Using the Finduserbyid in User.xml

1     <!--  -2     <ID= "Findordersuserlazyloading"  resultmap= "ordersuserlazyloading">3        SELECT * FROM Orders4     </Select>

On the top of the first to execute the findordersuserlazyloading, when the need to query the user's time to execute the Finduserbyid, through the RESULTMAP definition of deferred load execution configuration.

1.2.3 Delay Loading Resultmap

Use the Select in association to specify the ID of the statement to be executed for lazy loading.

1     <!--deferred loading of Resultmap -2     <Resultmaptype= "Cn.itcast.po.Orders"ID= "Ordersuserlazyloading">3          <!--Map The order information to the configuration -4         <IDcolumn= "id" Property= "id"/>5         <resultcolumn= "user_id" Property= "UserId"/>6         <resultcolumn= "Number" Property= "Number"/>7         <resultcolumn= "Createtime" Property= "Createtime"/>8         <resultcolumn= "Note" Property= "Note"/>9         Ten       <!--implementing lazy loading of user information One Select: Specifies the ID of the statement to be executed for deferred loading (the statement that queries user information based on user_id) A to use Usermapper.xml in Finduserbyid to complete a query based on user ID (user_id) user information, if Finduserbyid is not in this mapper need to add namespace in front of - column: The columns associated with user information in order information is user_id -          the the SQL for the associated query 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 +            - -            +           <!--implementing lazy loading of user information - A         <Association Property= "User"Select= "Test.finduserbyid"Javatype= "Cn.itcast.po.User"column= "user_id"></Association> at     </Resultmap>
1.2.4 Mapper.java
1     // Query Order association query user, user information is deferred loading 2      Public List<orders> findordersuserlazyloading ()throws Exception;

1.2.5 Test 1.2.5.1 Test ideas:

1, the implementation of the upper Mapper method (Findordersuserlazyloading), The internal call to findordersuserlazyloading in Cn.itcast.mybatis.mapper.OrdersMapperCustom only queries the orders information (single table).

2, in the program to traverse the previous step to query out the LIST<ORDERS>, when we call the GetUser method in Orders, we begin to delay loading.

3, delay loading, to call Usermapper.xml in Finduserbyid this method to obtain user information.

1.2.5.2 Deferred load configuration

MyBatis does not turn on lazy loading by default and needs to be setting configured in Sqlmapconfig.xml.

Configure in the MyBatis core configuration file:

Lazyloadingenabled, aggressivelazyloading

Setting items

Describe

Allowed values

Default value

Lazyloadingenabled

Global Settings lazy Loading. If set to ' false ', all associated will be initialized to load.

true | False

False

Aggressivelazyloading

When set to ' true ', lazy-loaded objects may be loaded by any lazy property. Otherwise, each property is loaded on demand.

true | False

True

Configure in Sqlmapconfig.xml:

1 <!--Global configuration parameters, when required in the configuration -2     <Settings>3         <!--Open deferred load switch, default does not turn on lazy loading -4         <settingname= "lazyloadingenabled"value= "true"/>5         <!--changes the decrement load to a negative load, which is loaded as needed -6         <settingname= "Aggressivelazyloading"value= "false"/>7     </Settings>

1.2.5.3 Test Code
1     //Query Order association query user, user information using lazy loading2 @Test3      Public voidTestfindordersuserlazyloading ()throwsException {4Sqlsession sqlsession = Sqlsessionfactory.opensession ();//Create a proxy object5Ordersmappercustom Ordersmappercustom =sqlsession6. Getmapper (Ordersmappercustom.class);7         //Query Order information (single table)8list<orders> list =ordersmappercustom.findordersuserlazyloading ();9 Ten         //traverse the list of orders above One          for(Orders orders:list) { A             //execute GetUser () to query the user information, here to implement on-demand loading -User User =Orders.getuser (); - System.out.println (user); the         } -  -}

1.2.6 Delay Loading thinking

How to implement lazy loading without using the lazy load feature in the association and collection provided by MyBatis??

The implementation method is as follows:

Define two mapper methods:

1. Inquiry Order List

2. Query user information according to user ID

Implementation ideas:

To query the first mapper method, get the Order information list

In the program (service), on demand to call the second Mapper method to query user information.

Anyway:

Using lazy Load method, the first query simple sql( preferably a single table, you can also associate the query), and then to load the additional information associated with the query.

Delayed loading of seven MyBatis

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.