redis实现mybatis的二级缓存

步骤一:开启mybatis的二级缓存

#开启mybatis的二级缓存
mybatis.configuration.cache-enabled=true

redis实现mybatis的二级缓存_第1张图片

步骤二:实现mybatis的cache接口

package com.aaa.qy156.utils;
import lombok.extern.java.Log;
import org.apache.ibatis.cache.Cache;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Set;
/**
 * @author :Teacher陈([email protected])
 * @date :Created in 2022/10/22 16:20
 * @description:我自己的myabtis二级缓存实现类
 * 将我们的数据缓存到redis中,MyMybatisCacheImpl 不被spring所管理
 * @modified By:
 * @version:
 */
@Log
public class MyMybatisCacheImpl   implements Cache {
    private  String id;
    private RedisTemplate redisTemplate;
    //构造的时候id必须传
    public MyMybatisCacheImpl(String id) {
        this.id = id;
    }
    @Override
    public String getId() {
        return this.id;
    }
    @Override
    public void putObject(Object key, Object value) {
        log.info("设置缓存");
        if(null==redisTemplate){
            redisTemplate = (RedisTemplate) MySpringTool.getBean("redisTemplate");
        }
        redisTemplate.opsForValue().set(key.toString(),value);
    }
    @Override
    public Object getObject(Object key) {
        log.info("查询缓存");
        if(null==redisTemplate){
              redisTemplate = (RedisTemplate) MySpringTool.getBean("redisTemplate");
        }
        Object o = redisTemplate.opsForValue().get(key.toString());
        return o;
    }
    @Override
    public Object removeObject(Object key) {
        log.info("删除缓存");
        if(null==redisTemplate){
            redisTemplate = (RedisTemplate) MySpringTool.getBean("redisTemplate");
        }
        redisTemplate.delete(key.toString());
        return null;
    }
    @Override
    public void clear() {
        log.info("清除缓存");
        if(null==redisTemplate){
            redisTemplate = (RedisTemplate) MySpringTool.getBean("redisTemplate");
        }
        Set keys = redisTemplate.keys("*" + this.id + "*");
        redisTemplate.delete(keys);
    }
    @Override
    public int getSize() {
        if(null==redisTemplate){
            redisTemplate = (RedisTemplate) MySpringTool.getBean("redisTemplate");
        }
        Integer execute = redisTemplate.execute(new RedisCallback() {
            @Override
            public Integer doInRedis(RedisConnection connection) throws DataAccessException {
                return connection.dbSize().intValue();
            }
        });
        return execute;
    }
}


步骤三:获取bean的工具类


package com.aaa.qy156.utils;
import jdk.internal.dynalink.beans.StaticClass;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
/**
 * @author :Teacher陈([email protected])
 * @date :Created in 2022/10/22 16:34
 * @description:在运行时手动获取bean
 * @modified By:
 * @version:
 */
@Component
public class MySpringTool implements ApplicationContextAware {
    private static ApplicationContext myApplicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        myApplicationContext=applicationContext;
    }
/**
 * @create by: Teacher陈([email protected])
 * @description: 获取bean
 * @create time: 2022/10/22 16:37
 * @return
 */
    public static Object getBean(String beanId){
        RedisTemplate  bean = myApplicationContext.getBean(beanId,RedisTemplate.class);
        return bean;
    }
}

步骤四:在映射文件或者在接口上使用cache注解中引入缓存接口的实现类

        
        

redis实现mybatis的二级缓存_第2张图片

步骤五:测试

启动项目,请求数据发现第一次查询之后再次查询没有执行SQL语句,直接从缓存中查询,不是从数据库查询,结果如下图所示

查看日志要在配置文件中加入下面代码

logging.level.com.aaa.qy156.dao=debug

redis实现mybatis的二级缓存_第3张图片

 

redis实现mybatis的二级缓存_第4张图片

 redis实现mybatis的二级缓存_第5张图片

 

你可能感兴趣的:(mybatis,redis,java)