What is deferred loading
The association and collection tags in resultmap have deferred load functionality.
Lazy loading means that when you associate a query, the master information is loaded first using lazy loading. The associated information is then loaded when the associated information is used.
Setting up lazy loading
need to be Sqlmapconfig.xml file, set the lazy load in the <settings> tab.
Lazyloadingenabled, aggressivelazyloading
description |
allowable 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 |
<!--on delay loading-<settings> <!--lazyloadingenabled: Lazy load starts, default is false--<setting name = "Lazyloadingenabled" value= "true"/> <!--aggressivelazyloading: Positive lazy loading, false if load on demand, default is true--< Setting name= "Aggressivelazyloading" value= "false"/> <!--turn on level two cache, default is false--<setting N Ame= "cacheenabled" value= "true"/> </settings> |
What is a query cache
Mybatis the first-level cache means that sqlsession . The scope of the first-level cache is a sqlsession. Mybatis First- level caching is turned on by default .
in the same Sqlsession , the same query SQL is executed, thefirst time the database is queried and written to the cache, and the second time is taken directly from the cache. the sqlsession cache is emptied when there are additions and deletions in the middle of two queries when executing SQL .
Mybatis the level two cache means that Mapper Mapping Files . The scope of the secondary cache is the content of the mapper map file under the same namespace , and multiple sqlsession are shared. Mybatis need to manually set the boot level two cache .
in the mapper file under the same namespace, the same query SQL is executed, thefirst time the database is queried and written to the cache, and the second time is taken directly from the cache. the level two cache is emptied when there are additions and deletions in the middle of two queries when executing SQL.
First-level caching principle
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/CC/wKiom1WII2CwQRhlAADBHk2wFdY170.jpg "title=" e4{ R@fzrak3{8}sq5k4exku.png "alt=" Wkiom1wii2cwqrhlaadbhk2wfdy170.jpg "/>
the first-level cache area is based on sqlsession divided by the unit.
each query will go to the cache first, if not found, then go to the database query, and then write the results into the cache. the internal cache of Mybatis uses a HashMap,key is the hashcode+statementid+sql statement . Value is a Java object mapped to the resulting set of queries .
sqlsession Execution Insert , Update , Delete and other operations Commit will be emptied after the sqlsession Cache.
Second-level caching principle
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/C9/wKioL1WIJXvA4ngUAADEvZunxso732.jpg "title=" gtsv_ Jp_54%jh~ddo14i78h.png "alt=" Wkiol1wijxva4nguaadevzunxso732.jpg "/>
level Two cache is Mapper level of. Mybatis default is not to turn on level two cache.
for the first time, call SQL under mapper to query the user information. The information that is queried is stored in the corresponding level two cache area of the mapper.
The second time calls the same SQL in the Mapper mapping file under the same namespace to query the user information. Will go to the corresponding level two cache to fetch the result.
if the mapper mapping file under the same namespace is called, the SQLis changed, and a commit operation is performed . This clears the level two cache under the namespace.
Turn on level two caching
1. Add the following to the core profile Sqlmapconfig.xml (turn on the level two cache master switch):
cacheenabled set to true
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/CC/wKiom1WIJFChnBasAADcJX3IbNs777.jpg "title=" Rwot) _ ]cy2y7r@[b10l7s9x.png "alt=" Wkiom1wijfchnbasaadcjx3ibns777.jpg "/>
2. in the mapping file, add the following to turn on level two cache:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/CC/wKiom1WIJHGj-78eAACCk6Tv9vs396.jpg "title=" AF6) 8r5r]hkul0dz1@lq43n.png "alt=" Wkiom1wijhgj-78eaacck6tv9vs396.jpg "/>
Implementing serialization
Because the level two cache data is not necessarily stored in memory, it has a variety of storage media, so the cached object needs to be serialized.
If the class has a parent class, the parent class is also serialized.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/C9/wKioL1WIJmXQfEQ4AAC1EcHDT6w451.jpg "title=" 4{w%o{ Tpvjfz@drb3{olx4n.png "alt=" Wkiol1wijmxqfeq4aac1echdt6w451.jpg "/>
Disable Level Two caching
The statement setting usercache=false can disable the level two cache of the current SELECT statement, that is, each query is to go to the database query , By default , it is truethatthe statement uses a level two cache.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/CC/wKiom1WIJPvRdgaUAAC-FQgNUyI548.jpg "title=" A4 "XVe ~u6lkg ' _f642 ' thhe.png "alt=" Wkiom1wijpvrdgauaac-fqgnuyi548.jpg "/>
Refresh Level Two cache
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/C9/wKioL1WIJvXykyTeAACdJiTWDLM099.jpg "title=" 852io4 {S (XL~QP5 (fov_% ' r.png "alt=" Wkiol1wijvxykyteaacdjitwdlm099.jpg "/>
10.MyBatis lazy load, level cache, level two cache settings