mybatis summary (e) (lazy loading)

Delayed loading of meaning

 

Also known as lazy loading demand query (lazy loading), mybatis support lazy loading, we hope to commonly used one-time cascade through sql data query it directly, but do not remove those unusual cascade of data, but to wait only when taken with the delay loading of these unusual features concatenation may be used.

 

Delayed loaded configuration

 

There are two elements in the configuration settings can be configured in a cascade mybatis

Delay configuration item
Configuration Item effect Configuration Options Description Defaults
lazyLoadingEnabled Delay global switch loaded. When turned on, all associations will be delayed loading. In a particular relationship, the switch state can be covered by setting the property fetchType true|false false
aggressiveLazyLoading When enabled, the call will delay for any delay loading the object properties with the properties of the full load; on the contrary, the property of each loaded on demand. true|false Version 3.4.1 (included) before is true, then is false

 

 

 

 

 

lazyLoadingEnabled delay represents the total load switch, if it is set to false, even if the switch is set to true intrusive will not take effect.

aggressiveLazyLoading represented intrusive delay loading switch, the default version 3.4.1 true, then the default is false.

<settings>
    <setting name="lazyLoadingEnabled" value="true"/>
    <setting name="aggressiveLazyLoading" value="true"/>
</settings>

 

 Delayed loading of the advantages and disadvantages

 

Advantages: start with the single-table query, and then go to related queries from the association table when needed, greatly improving the performance of the database, because the single-table queries related queries much faster than the speed of multiple tables.

Disadvantages: Because data is used only when needed, will perform database queries, so that when large quantities of data query, because the query time-consuming work, it may cause the user to wait longer time, resulting in poor user experience.

 

Load opportunity

 

Direct loading: after executing select statement to load the main object, immediately execute a query to select the associated object.
Invasive delay: When you perform a query on the primary loading objects, will not perform queries on the associated object. But when you want to access the main load object details properties, you will immediately execute select query associated objects.
Depth Delay: when the query is executed on the primary loading objects, will not perform queries on the associated object. Select the associated object is not performed when accessing the main details of loading the object of inquiry. Only when the real details to access the associated object, select queries will be executed on the associated object.

 

How to implement lazy loading

 

 

MyBatis in the delay load settings, resultMap only for the collection and association work, can be applied to one to one, one to many, many to one, all-many relationship in the query.

Note that, the application required lazy loading, loading Query Query objects associated with the main object must be separately select statement can not select the use of multi-table joins are performed queries. Because multi-table join queries, and its essence is a query on a table, query on a table formed by the connection of multiple tables. All information will be a one-time query multiple tables out.

 

Alone-many multi-table query

Before modification mapper.xml file

<! - Find a player based on the id team -> 
< the SELECT the above mentioned id = "selectPlayerByTeamId" resultType = "Player" >
    select id,name from t_player WHERE tid=#{id}
</select>

<! - association mapping between attributes -> 
<! - the set of data from select query, the query condition is queried selectTeamByIdAlone ID -> 
< The resultMap ID = "teamMapAlone" type = "Team" > 
    < ID column = "ID" Property = "ID" /> 
    < Result column = "name" Property = "name" /> 
    < Collection Property = "playerList" ofType = "Player" SELECT = "selectPlayerByTeamId" column = "ID" />
</resultMap>

<select id="selectTeamByIdAlone" resultMap="teamMapAlone">
    SELECT id,name FROM t_team where id=#{id}
</select>

 

Modified dao previous methods and the test class interfaces:

// DAO Interface 
Team selectTeamByIdAlone ( int the above mentioned id);

// test class 
@Test
 public  void selectTeamByIdAlone () {
    Team team = teamDao.selectTeamByIdAlone(1);
}

After the execution, you can see sql statement issued at the console in two portions, respectively, query t_team and t_player table.

 

Open-invasive delay

Add the following content mybatis.xml file, note the location of the content must be between the properties and typeAliases, in mybatis.xml configuration file, some labels are required in the order written:

<! - Global parameter -> 
< Settings > 
    <! - delay loading the main switch -> 
    < Setting name = "lazyLoadingEnabled" value = "to true" /> 
    <! - invasive loaded switch delay -> 
    <! - default before the 3.4.1 version is true, then the default is false -> 
    < Setting name = "aggressiveLazyLoading" value = "to true" /> 
</ Settings >

 

After the above configuration is good, then perform the above test class method, you'll find in the console only issued a statement a sql query t_team, it is because we opened the intrusive switch lazy loading, not in java program access Team in any property, it is not going to query player mybatis its associated data objects.

Modified test method:

@Test
public void selectTeamByIdAlone() {
    Team team = teamDao.selectTeamByIdAlone(1);
    System.out.println(team.getName());
}

After executing the above method again will see the issue two SQL statements are queries t_team console and t_player, because access to the Team of the property name in the java program, so the player will object data mybatis its associated check out.

 

Turn on lazy loading depth

Review mybatis.xml file: master switch is turned on, the aggressiveLazyLoading close it.

<! - Global parameter -> 
< Settings > 
    <! - delay loading the main switch -> 
    < Setting name = "lazyLoadingEnabled" value = "to true" /> 
    <! - invasive loaded switch delay -> 
    <! - default before the 3.4.1 version is true, then the default is false -> 
    < Setting name = "aggressiveLazyLoading" value = "false" /> 
</ Settings >

Continue to implement the above test method, you can see the console only print a query t_team table sql statement.

Modified test method is as follows:

@Test
public void selectTeamByIdAlone() {
    Team team = teamDao.selectTeamByIdAlone(1);
    System.out.println(team.getName());
    System.out.println(team.getPlayerList().size());
}

After executing the above test method, you can see the console print a two sql statements are queries t_team and t_player, which shows when you turn on the depth of the delay, as long as the player-related data without the use of code, mybatis will not be sql queries only when the issue will go to the real use of the sql statement query.

 

Lazy Loading (fetchType) in a single resultMap

Above all through the depth of the delay in loading mybatis.xml unified configuration file, and then if just want some depth inquiry support lazy loading, then you can add fetchType collection or association property in resultMap is configured to delay lazy is on depth, configuration depth is not eager to open delay. fetchType substituted global property is set configuration parameters lazyLoadingEnabled

 

Lazy loading summary

 

By the above example can be found in the depth of the most lazy way to load, you can let mybatis in this way reducing the sql queries when the query is executed so as to improve the efficiency of the program, but not under all scenarios using lazy loading can increase efficiency, some scenes such as when many queries, you need to have multi-party and check out, so turn on lazy loading but may slow down the program execution efficiency

 

 

 

 

 

 

 

 

 

 

reference:

1. https://www.cnblogs.com/ashleyboy/p/9286814.html

2. https://www.cnblogs.com/hopeofthevillage/p/11415738.html

3. https://blog.csdn.net/Huangyuhua068/article/details/88719383

*4. http://www.monkey1024.com/framework/1378

Guess you like

Origin www.cnblogs.com/flyinghome/p/12361890.html