开发环境
- JDK 1.8
- SpringBoot2.2.1
- MybatisPlus3.4.3.4
- Maven 3.2+
- Mysql5.7.36
- 开发工具
- IntelliJ IDEA
- smartGit
使用阿里的脚手架,链接:https://start.aliyun.com
选择mybatis plus framework
然后项目基础架构搭建好之后,加上redis配置
org.springframework.boot spring-boot-starter-data-redis
加上redis配置:
spring: redis: host: 127.0.0.1 port: 6379 password: database: 0
MyBatisPlus开启缓存支持
实现Cache接口,因为这个类不是Spring管理的,所以通过SpringContextHolder从ioc容器里获取redisTemplate类
package com.example.mybatisplus.common.cache; import com.example.mybatisplus.common.ioc.SpringContextHolder; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.cache.Cache; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.util.StringUtils; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; @Slf4j public class MyBatisRedisCache implements Cache { private static RedisTemplateredisTemplate; private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(true); private String id; public MyBatisRedisCache(String id) { if (StringUtils.isEmpty(id)) { throw new IllegalArgumentException("cache instances require an id."); } this.id = id; if (redisTemplate == null) { redisTemplate = SpringContextHolder.getBean("redisTemplate"); } } @Override public String getId() { return this.id; } @Override public void putObject(Object key, Object value) { if (!StringUtils.isEmpty(value)) { redisTemplate.opsForHash().put(id, key.toString() , value); log.info("mybatis缓存,{}:[{}]" , key , value ); } } @Override public Object getObject(Object key) { if (!StringUtils.isEmpty(key)) { Object object = redisTemplate.opsForHash().get(id , key.toString()); log.info("mybatis缓存读取,{}:[{}]", key , object); return object; } return null; } @Override public Object removeObject(Object key) { if (!StringUtils.isEmpty(key)) { redisTemplate.delete(key.toString()); } return null; } @Override public void clear() { redisTemplate.delete(id.toString()); } @Override public int getSize() { return redisTemplate.opsForHash().size(id.toString()).intValue(); } @Override public ReadWriteLock getReadWriteLock() { return readWriteLock; } }
修改CacheNamespace指定缓存类,implementation 属性:默认是PerpetualCache类,即hashMap实现
package com.example.mybatisplus.mapper; import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.mybatisplus.core.mapper.baseMapper; import com.example.mybatisplus.common.cache.MyBatisRedisCache; import com.example.mybatisplus.model.User; import org.apache.ibatis.annotations.CacheNamespace; @DS(value = "shop") @CacheNamespace(implementation = MyBatisRedisCache.class ,eviction = MyBatisRedisCache.class) public interface UserMapper extends baseMapper{ }
SpringContextHolder 类
package com.example.mybatisplus.common.ioc; import org.springframework.beans.BeansException; import org.springframework.beans.factory.DisposableBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.util.Assert; @Service @Lazy(false) public class SpringContextHolder implements ApplicationContextAware, DisposableBean { private static ApplicationContext applicationContext = null; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextHolder.applicationContext = applicationContext; } public static ApplicationContext getApplicationContext() { assertContextInjected(); return applicationContext; } public staticT getBean(String name) { assertContextInjected(); return (T) applicationContext.getBean(name); } public static T getBean(Class requiredType) { assertContextInjected(); return applicationContext.getBean(requiredType); } public static void clearHolder() { applicationContext = null; } @Override public void destroy() throws Exception { SpringContextHolder.clearHolder(); } private static void assertContextInjected() { Assert.state(applicationContext != null,"applicaitonContext属性未注入"); } }
测试,缓存里没数据,读取数据库
缓存有数据,不读取数据库
本博客代码例子可以在GitHub找到下载链接
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)