Mybatis learning: Mybatis cache configuration

**

MyBatis cache configuration

**
database cache, secondary cache, three-level cache
cache: When maintaining a session, query to get the data will be stored in a cache, the next use to obtain from the cache. (Transaction-level cache)
secondary cache: when the session is closed, the cache data is stored in the secondary cache. (Application-level cache)
three-level cache: can achieve cross-jvm, data synchronization via remote call way. (Query cache as redis)

Secondary cache usage scenarios:
1. query-based applications, as little as add, delete, change operation;
2 single business operation of the main table;
an association between the Table 3. Table as little as possible;

How to configure the secondary cache MyBatis

The first step: open the cache in mybatis-config.xml (on by default)

<settings>
<setting name="cacheEnable" value="true"/>
</settings>

Step 2: Perform disposed in the mapping file
because Mybatis selected individually in each policy object mapping cache configuration,
it is divided into two kinds of annotations or use Mapper.xml interface
Mapper.xml

<cache
eviction="FIFO"  #先进先出的收回策略
flushIntervak="6000"
size="512"
readOnly="true"   #返回对象只可读
/>

The case where the annotation is disposed on the interface

@CacheNamespace(
 eviction = FifoCahe.class, 
 flushInterval = 60000,
 size = 512,
 readWrite = false    #false只读 true读写
)

Integrated Redis Cache

Features: use of cache access sequence and deserializes it to the entity class implements Serializable
Step: Add Item dependent

<dependency>
<groupId>org.mybatis.caches</groudId>
<artifactId>mybatis-redis</artifactId>
<version>1.0.0-beta2</version>
</dependency>

Step Two: Configure redis parameters: redis.properties

host=localhost
port=6379
connectionTimeout=5000
soTimeout=5000
password=
database=0
clientName=

The third step is provided in the cache type mapping.xml

<mapper namespace="src.com.luo.MybatisMappings.UserMapper.xml" >
  <cache type="org.mybatis.caches.redis.RedisCache"/>
</mapper>

Guess you like

Origin blog.csdn.net/weixin_40990818/article/details/85651928