Mybatis cache Study Notes

Mybatis official definition
MyBatis is excellent persistence framework to support custom SQL, stored procedures and advanced mappings. MyBatis avoids almost all JDBC code and manual setting parameters and obtaining the result set. MyBatis can configure and native Map using simple XML or annotation, the interfaces and Java POJOs (Plain Old Java Objects, ordinary Java Objects) to database records in.
Cache
  Repeat query optimization problem;
  Each SqlSession are set up its own cache (Session level, all statements in a session share this cache; sta'tement level: Detachment of the current statement effective, in particular every SqlSession held in his Executor, a per Executor there is a Local cache. when a user initiates a query, Mybatis is generated according to MappedStatement currently executing a key, go to Local cache query, if the cache hit, then returned. If a cache miss, then write Local cache, and finally return The results to the user.
Configuration:
  
// Mybatis arranged in the configuration file (default level SESSION)
 < Setting name = "localCacheScope" value = "SESSION" />
  For the select statement, which generates a key (statement Id + Offset + Limmit + Sql + Params) to find the cache according to said small hit, miss is performed in the database, and returns the data cache.
  updata, delete, insert operation will empty the cache.
  problem:
   When a plurality of SqlSession prone dirty data.
Two-level cache
  More SqlSession share data.
  After opening second-level cache, use CachingExecutor decorative Executor, before entering the subsequent executions, carried out the first two cache queries CachingExecutor.
 
// Mybatis profile
 < Setting name = "cacheEnabled" value = "to true" />

// declare the namespace to use second-level cache mapping file
<cache/>   
//or
// This mapping file sharing life a secondary cache and xxxMapper in the mapping file
<cache-ref namespace="xxx.xxxMapper"/>

  Mybatis secondary cache with respect to a cache, a shared cache data is achieved between the SqlSession, while more fine particle size, it is possible to Mapper level, through the Cache interface class different combinations of controllable also Cache stronger.

  problem:

  Multi-table queries prone to dirty data.

  inevitable dirty cache data locally-based, distributed environment;

to sum up:

  Mybatis cache imperfect.

  

 

 

 
 

Guess you like

Origin www.cnblogs.com/ccqqcc/p/11074600.html