spring boot 集成redis 一、config配置
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate, Object> redisTemplate(LettuceConnectionFactory connectionFactory){
//重新设置Redis的数据模板,让其可以操作Java中的对象
RedisTemplate, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
}
二、service设置
public interface RedisService {
void set(String key, Object value);
Object get(String key);
Boolean delete(String key);
}
【idea|spring boot 集成redis】实现接口
@Service
public class RedisServiceImpl implements RedisService {
@Resource
private RedisTemplate redisTemplate;
public void set(String key, Object value) {
//更改在redis里面查看key编码问题
RedisSerializer redisSerializer =new StringRedisSerializer();
redisTemplate.setKeySerializer(redisSerializer);
ValueOperations vo = redisTemplate.opsForValue();
vo.set(key, value);
}public Object get(String key) {
ValueOperations vo = redisTemplate.opsForValue();
return vo.get(key);
}public Boolean delete(String key) {
return redisTemplate.delete(key);
}
}
推荐阅读
- spring|spring boot 返回图片流
- spring|Spring Boot的特点
- postman|postman工具的使用
- java|java mock verify_使用Mockito进行单元测试【1】——mock and verify[转]
- java|记一次Spring boot 和Vue前后端分离的入门培训
- 前端基础|众多mock工具,这一次我选对了
- ServiceLoader(Java 的 SPI 加载方案)
- spring|springboot+layui简单增删改查整合
- 数据库|MySQL 45讲 | 06 全局锁和表锁 (给表加个字段怎么有这么多阻碍())