Java Detailed Explanation of MyBatis Cache

  • 2021-11-14 05:41:44
  • OfStack

Directory What is MyBatis Cache MyBatis Cache Classification 1. 1 Level Cache: SqlSession level, which is on by default and cannot be turned off. (On by default) 2.2 level cache: Mapper level, which is off by default, can turn on level 2 cache. How to use level 2 cache from 1. MyBatis 1.1 config. xml configuration to turn on level 1. 2 Mapper. xml configuration level 1. 3 Entity class implementation serialization interface 2. ehcache level 2 cache (3rd party) 2.1 pom. xml add dependency 2.2 Add ehcache. xml 2.3. config. xml configuration to turn on level 2 cache 2.4 Mapper. xml configuration level 2 cache

What is an MyBatis cache

Caching can reduce the number of interactions between Java and the database, thus improving the running efficiency of the program. For example, if an object with id = 1 is found, the object will be automatically saved to the cache after the first query, and the object can be directly taken out from the cache during the next query without accessing the database again.

MyBatis Cache Classification

1. Level 1 cache: SqlSession level, which is turned on by default and cannot be turned off. (On by default)

When operating the database, it is necessary to create an SqlSession object, in which an HashMap is used to store cached data, and the cached data areas between different SqlSession do not affect each other. The scope of the first-level cache is SqlSession. When the same SQL statement is executed twice in the same SqlSession, the result will be saved in the cache after the first execution, and it will be obtained directly from the cache during the first query. It should be noted that if SqlSession performs DML operations (insert, update, delete), MyBatis must empty the cache to ensure the accuracy of the data.

Level 2.2 cache: Mapper level, closed by default, can be turned on

When making the level cache, multiple SqlSession use the same Mapper SQL statement to operate the database, and the obtained data will exist in the level cache area, which is also used to make HashMap enter the data storage. Compared with the level cache, the range of the level cache is larger, and multiple SqlSession can share the level cache, and the level cache is across SqlSession. The level cache is shared by multiple SqlSession, and its application domain is the same namespace of Mapper. If different SqlSession execute the same namespace statement twice and the parameters are equal, the data will be saved to the level cache after the first successful execution, and the data can be directly taken out from the level cache for the first time.

How to use level 2 cache

1. Level 2 cache of MyBatis

1.1 config. xml configuration turns on level caching

settings>
 <!--  Print SQL-->
 <setting name="logImpl" value="STDOUT_LOGGING" />
 <!--  Turn on delayed loading  -->
 <setting name="lazyLoadingEnabled" value="true"/>
 <!--  Open ⼆ Level cache  -->
 <setting name="cacheEnabled" value="true"/>
</settings>

1.2 Configuring Level Cache in Mapper. xml

<cache></cache>

1.3 Entity classes implement serialization interfaces

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Account implements Serializable {
 private long id;
 private String username;
 private String password;
 private int age;
}

2. ehcache Level 2 Cache (3rd Party)

2.1 pom. xml Add related dependencies

<dependency>
 <groupId>org.mybatis</groupId>
 <artifactId>mybatis-ehcache</artifactId>
 <version>1.0.0</version>
</dependency>
<dependency>
 <groupId>net.sf.ehcache</groupId>
 <artifactId>ehcache-core</artifactId>
 <version>2.4.3</version>
</dependency>

2.2 Add ehcache. xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
 <diskStore/>
 <defaultCache
 maxElementsInMemory="1000"
 maxElementsOnDisk="10000000"
 eternal="false"
 overflowToDisk="false"
 timeToIdleSeconds="120"
 timeToLiveSeconds="120"
 diskExpiryThreadIntervalSeconds="120"
 memoryStoreEvictionPolicy="LRU">
 </defaultCache>
</ehcache>

2.3 config. xml Configuration Turns ON Level Cache

<settings>
 <!--  Print SQL-->
 <setting name="logImpl" value="STDOUT_LOGGING" />
 <!--  Turn on delayed loading  -->
 <setting name="lazyLoadingEnabled" value="true"/>
 <!--  Open ⼆ Level cache  -->
 <setting name="cacheEnabled" value="true"/>
</settings>

2.4 Configuring Level Cache in Mapper. xml

<cache type="org.mybatis.caches.ehcache.EhcacheCache">
 <!--  After the cache is created, the last ⼀ The time of accessing the cache for the next time ⾄ Time interval for cache invalidation  -->
 <property name="timeToIdleSeconds" value="3600"/>
 <!--  Cache ⾃ From the time of creation ⾄ Time interval of failure  -->
 <property name="timeToLiveSeconds" value="3600"/>
 <!--  Cache Reclaim Policy, LRU Indicates the removal of the recent ⽤ Minimum object  -->
 <property name="memoryStoreEvictionPolicy" value="LRU"/>
</cache>

Related articles: