整合步骤
由于springboot开箱即用的特点,所以可以预知,springboot整合redis将会非常简单,如下步骤:
1、导入依赖
该依赖是springboot整合Redis的starter依赖
<!--springboot-redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、配置Redis连接属性
在springboot主配置文件中配置如下属性:
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
#Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
#连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
3,注入RedisTemplate类
RedisTemplate 是spring封装的操作redis的模板对象,可以方便的操作redis各种数据结构,并且经过上述的两个整合步骤后,springboot将自动为我们创建RedisTemplate对象,并给spring容器管理,所以我们只需要从spring容器获取该对象即可
@Autowired
RedisTemplate redisTemplata;
使用Redis作为mybatis缓存
在分布式应用中,如果需要使用mybatis的二级缓存就不能用内嵌式的缓存了,比如ehcache这些,因为会出现缓存数据不同步的问题,所以就需要使用像Redis这些分布式缓存,而通过上面的整合步骤后,我们就可以获取spring容器内的RedisTemplate对象了,有了这个RedisTemplate对象,我们就可以开启Mybatis二级缓存,并且用RedisTemplate对象来操作Redis,具体使用步骤如下:
1、创建一个用于获取spring环境上下文对象的工具类
为什么要有这个工具类呢?因为mybatis的二级缓存类是需要由mybatis创建,每个mapper对象都会创建出一个缓存对象实例,该二级缓存类不能由spring创建,而RedisTemplate对象又是spring创建,所以,在非spring容器的对象获取spring容器对象是不能直接用@Autowired注解注入进来的,只能用spring环境上下文来获取
/**
* 获取spring环境上下文工具类,该类实现了spring环境上下文感知接口,
* 实现了该接口的类,在spring初始化完成后会调用该类的setApplicationContext方法
* 并传入spring环境上下文ApplicationContext对象,注意:该工具类要给spring容器管理
* 这样spring才能调用我们该类的setApplicationContext方法。
*/
@Component
public class ApplicationContextHolder implements ApplicationContextAware{
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextHolder.applicationContext = applicationContext;
}
public static <T>T getBean(String beanName){
return (T) applicationContext.getBean(beanName);
}
}
2、创建mybatis二级缓存类
任何需要使用mybatis二级缓存功能的工具类都需要实现mybatis的缓存接口:org.apache.ibatis.cache.Cache
,所以我们自定义一个MybatisRedisCache类,实现mybatis的Cache接口,并实现该接口上的抽象方法,以下为完整代码:
public class MybatisRedisCache implements Cache{
private ReadWriteLock readWriteLock = new ReentrantReadWriteLock(true);
//获取spring容器中的RedisTemplate对象
RedisTemplate<Object,Object> redisTemplate = ApplicationContextHolder.getBean("redisTemplate");
//该id是mybatis的mapper文件命名空间
private String id;
public MybatisRedisCache(String id){
this.id = id;
}
@Override
public String getId() {
return this.id;
}
/**
* @param key 查询语句和查询条件
* @param value 查询的结果集
*/
@Override
public void putObject(Object key, Object value) {
Map<Object,Object> map = (Map<Object, Object>) redisTemplate.opsForValue().get(id);
if(map == null){
map = new HashMap<Object, Object>();
}
map.put(key,value);
redisTemplate.opsForValue().set(id,map);
}
@Override
public Object getObject(Object key) {
Map<Object,Object> map = (Map<Object, Object>) redisTemplate.opsForValue().get(id);
if(map != null){
if(map.containsKey(key)){
return map.get(key);
}
}
return null;
}
@Override
public Object removeObject(Object key) {
Map<Object,Object> map = (Map<Object, Object>) redisTemplate.opsForValue().get(id);
if(map != null){
if(map.containsKey(key)){
return map.remove(key);
}
}
return null;
}
@Override
public void clear() {
redisTemplate.delete(id);
}
@Override
public int getSize() {
Map<Object,Object> map = (Map<Object, Object>) redisTemplate.opsForValue().get(id);
if(map != null){
return map.size();
}
return 0;
}
@Override
public ReadWriteLock getReadWriteLock() {
return readWriteLock;
}
}
3、使用二级缓存
在需要使用二级缓存的mapper文件添加<cache>标签
<cache type="xx.xx.MybatisRedisCache"></cache>