MyBatis-plus基于注解使用二级缓存

开启二级缓存配置
application.yml
mybatis-plus:
  configuration:
    cache-enabled: true
自定义Cashe实现类
import cn.hutool.core.collection.CollectionUtil;
import com.dandandog.framework.common.utils.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cache.Cache;
import org.springframework.data.redis.connection.RedisServerCommands;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Optional;
import java.util.Set;

@Slf4j
public class MybatisRedisCache implements Cache {

    private final String id;

    private RedisTemplate<String, Object> redisTemplate;

    public MybatisRedisCache(final String id) {
        if (id == null) {
            throw new IllegalArgumentException("Cache instances require an ID");
        }
        this.id = id;

    }

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public void putObject(Object key, Object value) {
        log.debug("添加缓存");
        if (value != null) {
            getRedisTemplate().opsForValue().set(key.toString(), value);
        }
    }

    @Override
    public Object getObject(Object key) {
        log.debug("获取缓存");
        try {
            if (key != null) {
                return getRedisTemplate().opsForValue().get(key.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("缓存出错 ");
        }
        return null;
    }

    @Override
    public Boolean removeObject(Object key) {
        log.debug("删除缓存");
        if (key != null) {
            return getRedisTemplate().delete(key.toString());
        }
        return false;
    }

    @Override
    public void clear() {
        log.debug("清空缓存");
        Set<String> keys = getRedisTemplate().keys("*:" + this.id + "*");
        if (!CollectionUtil.isEmpty(keys)) {
            getRedisTemplate().delete(keys);
        }
    }

    @Override
    public int getSize() {
        Long size = getRedisTemplate().execute(RedisServerCommands::dbSize);
        return Optional.ofNullable(size).orElse(0L).intValue();
    }

    public RedisTemplate<String, Object> getRedisTemplate() {
        return Optional.ofNullable(redisTemplate).orElse(SpringContextUtil.getBean("redisTemplate", RedisTemplate.class));
    }
}

Mapper使用 @CacheNamespace
@CacheNamespace(implementation=MybatisRedisCache.class,eviction=MybatisRedisCache.class)
@Mapper
public interface IntegralUserLogDao extends BaseMapper<IntegralUserLog> {

}
分页问题
cause: java.lang.indexoutofboundsexception: index: 0, size: 0] with root cause
mybatis配置的时候加入
	@Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值