MyBatis lazy loading, first level cache, second level cache settings MyBatis lazy loading, first level cache, second level cache settings

MyBatis lazy loading, L1 cache, L2 cache settings

 

what is lazy loading 

        The association and collection tags in resultMap have the function of lazy loading.

        Lazy loading means that when the query is associated, the main information is loaded first by using lazy loading. Load the associated information when using the associated information.


Set up lazy loading

    Lazy loading needs to be set in the <settings> tag     in the SqlMapConfig.xml file.

        lazyLoadingEnabled、aggressiveLazyLoading

setting item

describe

allowance

Defaults

lazyLoadingEnabled

Lazy loading is set globally. If set to 'false', all associated ones will be initialized loaded.

true | false

false

aggressiveLazyLoading

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

true | false

true

 

1
2
3
4
5
6
7
8
9
10
<!-- 开启延迟加载 -->
     < settings >
         <!-- lazyLoadingEnabled:延迟加载启动,默认是false -->
         < setting  name = "lazyLoadingEnabled"  value = "true" />
         <!-- aggressiveLazyLoading:积极的懒加载,false的话按需加载,默认是true -->
         < setting  name = "aggressiveLazyLoading"  value = "false" />
         
         <!-- 开启二级缓存,默认是false -->
         < setting  name = "cacheEnabled"  value = "true" />
     </ settings >

 

 

 

 

What is query cache

    

The first level cache of Mybatis refers to SqlSession . The scope of the first level cache is a SqlSession . Mybatis turns on the first level cache by default .

In the same SqlSession , execute the same query SQL , the first time it will query the database and write it to the cache; the second time it will be taken directly from the cache. When an addition, deletion or modification occurs between two queries when executing SQL , the cache of the SqlSession is cleared.

 

The second level cache of Mybatis refers to the mapper mapping file . The scope of the second-level cache is the mapper mapping file content under the same namespace , which is shared by multiple SqlSessions . Mybatis needs to manually set up the second level cache .

In the mapper file under the same namespace , execute the same query SQL , the first time it will query the database and write it to the cache; the second time it will be taken directly from the cache. When an addition, deletion or modification occurs between two queries when executing SQL , the second-level cache is emptied.

 

The principle of the first level cache

wKiom1WII2CwQRhlAADBHk2wFdY170.jpg

The first- level cache area is divided according to the SqlSession unit.

 

Each query will first go to the cache to find, if not found, then go to the database query, and then write the result to the cache. The internal cache of Mybatis uses a HashMap , the key is hashcode+statementId+sql statement . Value is the java object to which the query result set is mapped.

 

SqlSession will clear the SQLSession cache after executing the operations such as insert , update , delete , etc. to commit .

 

 

 

 

 

Second-level cache principle

wKioL1WIJXvA4ngUAADEvZunxso732.jpg

The second level cache is mapper level. Mybatis does not open the second level cache by default.

 

The first call to SQL under mapper to query user information. The queried information will be stored in the L2 cache area corresponding to the mapper.

The second time calls the same SQL in the mapper mapping file under the same namespace to query user information. The result will be fetched from the corresponding L2 cache.

If you call the addition, deletion and modification SQL in the mapper mapping file under the same namespace , and execute the commit operation. At this time , the second-level cache under the namespace will be cleared .

 

Enable L2 cache

1. Add the following content to the core configuration file SqlMapConfig.xml (turn on the second-level cache master switch):  

cacheEnabled is set to  true

wKiom1WIJFChnBasAADcJX3IbNs777.jpg

 

 

2. In the mapping file, add the following content to enable the second-level cache:

wKiom1WIJHGj-78eAACCk6Tv9vs396.jpg

 

implement serialization

    

Since the data in the second-level cache is not necessarily stored in the memory, and its storage medium is various, it is necessary to serialize the cached objects.

If the class has a parent class, then the parent class should also implement serialization.

wKioL1WIJmXQfEQ4AAC1EcHDT6w451.jpg

Disable L2 cache

Setting userCache=false in this statement can disable the second-level cache of the current select statement, that is, every query is a query in the database, which is true by default , that is, the statement uses the second-level cache.

wKiom1WIJPvRdgaUAAC-FQgNUyI548.jpg

Flush the second level cache

wKioL1WIJvXykyTeAACdJiTWDLM099.jpg

what is lazy loading 

        The association and collection tags in resultMap have the function of lazy loading.

        Lazy loading means that when the query is associated, the main information is loaded first by using lazy loading. Load the associated information when using the associated information.


Set up lazy loading

    Lazy loading needs to be set in the <settings> tag     in the SqlMapConfig.xml file.

        lazyLoadingEnabled、aggressiveLazyLoading

setting item

describe

allowance

Defaults

lazyLoadingEnabled

Lazy loading is set globally. If set to 'false', all associated ones will be initialized loaded.

true | false

false

aggressiveLazyLoading

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

true | false

true

 

1
2
3
4
5
6
7
8
9
10
<!-- 开启延迟加载 -->
     < settings >
         <!-- lazyLoadingEnabled:延迟加载启动,默认是false -->
         < setting  name = "lazyLoadingEnabled"  value = "true" />
         <!-- aggressiveLazyLoading:积极的懒加载,false的话按需加载,默认是true -->
         < setting  name = "aggressiveLazyLoading"  value = "false" />
         
         <!-- 开启二级缓存,默认是false -->
         < setting  name = "cacheEnabled"  value = "true" />
     </ settings >

 

 

 

 

What is query cache

    

The first level cache of Mybatis refers to SqlSession . The scope of the first level cache is a SqlSession . Mybatis turns on the first level cache by default .

In the same SqlSession , execute the same query SQL , the first time it will query the database and write it to the cache; the second time it will be taken directly from the cache. When an addition, deletion or modification occurs between two queries when executing SQL , the cache of the SqlSession is cleared.

 

The second level cache of Mybatis refers to the mapper mapping file . The scope of the second-level cache is the mapper mapping file content under the same namespace , which is shared by multiple SqlSessions . Mybatis needs to manually set up the second level cache .

在同一个namespace下的mapper文件中,执行相同的查询SQL,第一次会去查询数据库,并写到缓存中;第二次直接从缓存中取。当执行SQL时两次查询中间发生了增删改操作,则二级缓存清空。

 

一级缓存原理

wKiom1WII2CwQRhlAADBHk2wFdY170.jpg

一级缓存区域是根据SqlSession为单位划分的。

 

每次查询会先去缓存中找,如果找不到,再去数据库查询,然后把结果写到缓存中。Mybatis的内部缓存使用一个HashMap,keyhashcode+statementId+sql语句。Value为查询出来的结果集映射成的java对象。

 

SqlSession执行insertupdatedelete等操作commit后会清空该SQLSession缓存。

 

 

 

 

 

二级缓存原理

wKioL1WIJXvA4ngUAADEvZunxso732.jpg

二级缓存是mapper级别的。Mybatis默认是没有开启二级缓存。

 

第一次调用mapper下的SQL去查询用户信息。查询到的信息会存到该mapper对应的二级缓存区域内。

The second time calls the same SQL in the mapper mapping file under the same namespace to query user information. The result will be fetched from the corresponding L2 cache.

If you call the addition, deletion and modification SQL in the mapper mapping file under the same namespace , and execute the commit operation. At this time , the second-level cache under the namespace will be cleared .

 

Enable L2 cache

1. Add the following content to the core configuration file SqlMapConfig.xml (turn on the second-level cache master switch):  

cacheEnabled is set to  true

wKiom1WIJFChnBasAADcJX3IbNs777.jpg

 

 

2. In the mapping file, add the following content to enable the second-level cache:

wKiom1WIJHGj-78eAACCk6Tv9vs396.jpg

 

implement serialization

    

Since the data in the second-level cache is not necessarily stored in the memory, and its storage medium is various, it is necessary to serialize the cached objects.

If the class has a parent class, then the parent class should also implement serialization.

wKioL1WIJmXQfEQ4AAC1EcHDT6w451.jpg

Disable L2 cache

Setting userCache=false in this statement can disable the second-level cache of the current select statement, that is, every query is a query in the database, which is true by default , that is, the statement uses the second-level cache.

wKiom1WIJPvRdgaUAAC-FQgNUyI548.jpg

Flush the second level cache

wKioL1WIJvXykyTeAACdJiTWDLM099.jpg

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325356438&siteId=291194637