Reference: Https://mybatis.github.io/mybatis-3/zh/sqlmap-xml.html,
Http://www.yihaomen.com/article/java/428.htm
Many applications, in order to improve performance, increase the cache, especially the data obtained from the database. By default, the first-level cache of MyBatis is turned on by default. Similar to hibernate, the so-called first-level cache, which is based on the same sqlsession query statement, that is, session-level cache, non-global cache, or non-level two cache.
If you want to implement the MyBatis level two cache, there are generally two ways to do this:
1. Use the MyBatis built-in cache mechanism.
2. Adopt the three-party cache framework, such as Ehcache, Oscache and so on.
Use the MyBatis built-in cache mechanism.
Add the <cache/> statement to the SQL statement mapping file, and the corresponding model class implements the Java Serializable interface, because the cache is plainly a process of serialization and deserialization, so this interface needs to be implemented. The simple <cache/> means the following:
1. All SELECT statements in the mapping file will be cached.
2. All insert,update and DELETE statements in the mapping file will empty the cache.
3. The cache uses the "seldom used" algorithm to recycle
4. The cache will not be emptied by the set time.
5. Each cache can store a reference to 1024 lists or objects (regardless of the result of the query).
6. The cache will act as a "read/write" cache, meaning that the acquired object is not shared and is secure to the caller. There will be no other callers or line submersible in the modification.
All attributes of a cached element can be modified by attributes. Like what:
Program code
<cache eviction= "FIFO" flushinterval= "60000" size= "Up" readonly= "true"/>
using Ehcache to implement the MyBatis level two cache
The first thing you need to do is download the jar package on MyBatis's official website: https://code.google.com/p/mybatis/When writing a document: Mybatis-ehcache-1.0.2.zip, which includes
Program code
Mybatis-ehcache-1.0.2.jar
Ehcache-core-2.6.5.jar
Slf4j-api-1.6.1.jar
Of course, the use of Ehcache must be added under the classpath ehcache configuration file Ehcache.xml:
Program code
<cache name= "Default"
Maxelementsinmemory= "10000"
Eternal= "false"
timetoidleseconds= "3600"
Timetoliveseconds= "10"
Overflowtodisk= "true"
Diskpersistent= "true"
Diskexpirythreadintervalseconds= "120"
Maxelementsondisk= "10000"
/>
So how to configure in the SQL mapping file, refer to the following:
Program code
<cache type= "Org.mybatis.caches.ehcache.LoggingEhcache" >
<property name= "Timetoidleseconds" value= "3600"/><!--1 hour-->
<property name= "Timetoliveseconds" value= "3600"/><!--1 hour-->
<property name= "maxentrieslocalheap" value= "/>"
<property name= "Maxentrieslocaldisk" value= "10000000"/>
<property name= "Memorystoreevictionpolicy" value= "LRU"/>
</cache>
Summary: Whether it is using MyBatis's own cache or the three-way cache, this configuration is to all the SELECT statements are global cache, but in fact, it is not always the case, for example, I in this series of tutorials in the seventh chapter Http://www.yihaomen.com /article/java/326.htm, you can not use this situation to write your own paging algorithm. You need to disable the cache, so you need the following methods:
Program code
<select id= "Selectarticlelistpage" resultmap= "Resultuserarticlelist" usecache= "false" >
.......
Notice the usecache= "false"? This avoids the use of caching.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
MyBatis configuring your own cache and third-party cache