Mybatis注解开发之@CacheNamespace:实现注解二级缓存的使用

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Mybatis注解开发之@CacheNamespace:实现注解⼆级缓存的
使⽤
MyBatis⼆级缓存使⽤:
官⽅建议在service使⽤缓存,但是你也可以直接在mapper层缓存,这⾥的⼆级缓存就是直接在Mapper层进⾏缓存操作
Mybatis的⼆级缓存实现也⼗分简单,只要在
springboot的配置⽂件打开⼆级缓存,即:mybatis-
plus.configuration.cache-enabled=true
在Dao接⼝上增加注解
@CacheNamespace(implementation= MybatisPlusCache.class,eviction=MybatisPlusCache.class)
public interface DictEntryDao extends BaseMapper<DictEntry> {
}
与controller同级新建configuration⽂件夹,在⽂件夹下添加MybatisPlusCache类,代码如下:
package com.ljxx.app.configuration;
import com.ljxx.app.utils.ApplicationContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cache.Cache;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* @author zhongyushi
* @date 2020/9/28 0028
* @dec MybatisPlus缓存配置,使⽤redis作为缓存服务器
*/
@Slf4j
public class MybatisPlusCache implements Cache {
// 读写锁
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(true);
//这⾥使⽤了redis缓存,使⽤springboot⾃动注⼊
private RedisTemplate<String, Object> redisTemplate;
private String id;
//是mybatis必须要求的,必写。

此id是xml中的namespace的值
public MybatisPlusCache(final String id) {
if (id == null) {
throw new IllegalArgumentException("未获取到缓存实例id");
}
this.id = id;
}
//返回cache的唯⼀名称
@Override
public String getId() {
return this.id;
}
//缓存存值
@Override
public void putObject(Object key, Object value) {
//id是namespace的值,key是⽅法名,value是查询的结果
getRedisTemplate().opsForHash().put(id, key.toString(), value);
}
//缓存取值
@Override
public Object getObject(Object key) {
return getRedisTemplate().opsForHash().get(id, key.toString());
}
//mybatis保留⽅法
@Override
public Object removeObject(Object key) {
return null;
}
//清空缓存,在增删改时会⾃动调⽤
@Override
public void clear() {
getRedisTemplate().delete(id);
}
@Override
public int getSize() {
return getRedisTemplate().opsForHash().size(id).intValue();
}
@Override
public ReadWriteLock getReadWriteLock() {
return this.readWriteLock;
}
//获取RedisTemplate,不能通过注⼊的⽅式,原因是此类是由mybatis实例化的
private RedisTemplate getRedisTemplate() {
//从上下⽂中获取redisTemplate
RedisTemplate redisTemplate = (RedisTemplate) ApplicationContextUtil.getBean("redisTemplate");
//设置key是string类型的序列
redisTemplate.setKeySerializer(new StringRedisSerializer());
//设置hashKey是string类型的序列
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
return redisTemplate;
}
}
如果是BaseMapper⾃带的⽅法,则配置@CacheNamespace(implementation=
MybatisPlusCache.class,eviction=MybatisPlusCache.class)即可,如果是⾃定义的⽅法,则要在Mapper.xml中添加如下配置:<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ljxx.app.dao.DictEntryDao">
<cache-ref namespace="com.ljxx.app.dao.DictEntryDao"/>
</mapper>。

相关文档
最新文档