Mybatis learning notes - L1 cache, L2 cache, reverse engineering

Mybatis basic edition is released after completion

Query cache

L1 cache

MyBatis enables the first level cache by default. If the same SqlSession object is used to execute the same query statement, it will only send the SQL statement to the database at the first query and put the query results into the SqlSession (existing as a cache). If the same query statement is executed again, the object will be queried directly from the cache. Reduces the number of database accesses and improves performance.

L1 cache lifecycle

  1. If SqlSession calls the close() method, the L1 cache object will be released and the L1 cache will not be available.
  2. sqlSession performs any update operation (update(), delete(), insert()) or session. commit() will clear the data in the PrepetualCache object, but the object can continue to be used.

mybatis L1 cache and L2 cache

L2 cache

Use of L2 cache

MyBatis does not enable L2 cache by default and needs to be opened manually.

  1. First, in the configuration file config. Of MyBatis Declare cache enabled in XML:
	<setting name="cacheEnabled"  value="true" />
  1. Then in the specific mapper Declare to open L2 cache in XML
  1. Class serialization
    The object to be cached must implement the serialization interface. If the Person class is serialized, the cascading property, and parent class of Person must be serialized.

The sqlSession object executes session Close() or query its own session Write object to L2 cache is triggered only when commit()

Mybatis has its own L2 cache. mapper objects generated by the same namespace can share the same L2 cache.

Disable and cleanup


Add useCache = "false" in the select tag


  1. The same as the method of cleaning up the L1 cache, execute the added, deleted and modified session When commit(), the L1 and L2 caches will be cleaned up. The reason why adding, deleting and changing is to clean up the cache is to prevent dirty data.
  2. Add the attribute flushCache = "true" in the select tag

Use the second level cache Ehcache provided by the third party

To integrate the L2 cache provided by the three parties (or customize the L2 cache), you must implement org apache. ibatis. cache. Cache interface. The default implementation class of this interface is perpetual cache

Consolidate ehcache L2 cache:

  1. Introduce the following jar under the classpath

  2. Write ehcache configuration file ehcache xml

<ehcache xmlns:xsi="" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
  <!--When the objects in L2 cache exceed the memory limit (number of cache objects)>maxElementsInMemory),Saved hard disk file  -->
 <diskStore path="D:\Ehcache"/>
 	maxElementsInMemory:Sets the number of objects cached in memory
    maxElementsOnDisk: Sets the number of cached objects in the hard disk
    eternal: Sets whether the cache never expires
    overflowToDisk: When the number of objects cached in memory exceeds maxElementsInMemory Is it transferred to the hard disk
    timeToIdleSeconds: When two accesses exceed this value, the cache object will be invalidated 
    timeToLiveSeconds: Maximum storage time of a cache object (life cycle)
    diskExpiryThreadIntervalSeconds: Set how often to clean up the cache in the hard disk through a thread
    memoryStoreEvictionPolicy: When the maximum value of the cache object is exceeded, the processing strategy; LRU,FIFO,LFU
  1. Enable EhCache L2 cache

In xxmapper Open in XML

<cache  type="org.mybatis.caches.ehcache.EhcacheCache">
<!-- adopt property cover Ehcache.xml Value in -->
<property name="maxElementsInMemory" value="2000"/>
<property name="maxElementsOnDisk" value="3000"/>

reverse engineering

Table, class, interface, mapper XML is closely related to the four, so when you know one, the other three should be generated automatically.
Table - > other three

Implementation steps:
a. mybatis-generator-core.jar,mybatis.jar,ojdbc.jar
b. Reverse engineered configuration file generator xml
c. Execute

Keywords: Mybatis

Added by ganesan on Thu, 17 Feb 2022 16:26:28 +0200