传送门:
网上找了一些springboot操作Redis的文章,
这篇文章关于序列化有大概的讲解:https://blog.csdn.net/neosmith/article/details/46800235
redisTemplate 的基本使用:https://blog.csdn.net/ruby_one/article/details/79141940
GenericJackson2JsonRedisSerializer :https://blog.csdn.net/asdfsadfasdfsa/article/details/79962056 (这个没有测试成功!)
看到的文章中对于json方式序列化的使用基本是2中方式:
方式1.每种类型写一个Bean,比如:
@Bean
public RedisTemplate<String,Person> redisTemplate(RedisConnectionFactory factory){
...
}
@Bean
public RedisTemplate<String,School> redisTemplate(RedisConnectionFactory factory){
...
}
方式2.
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
...
}
或者
@Bean
public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory factory){
...
}
每个get***方法得到的结果需要自己做一次强制类型转换,将Object转换为我们需要的类型.
以下这种写法实现了泛型的方式,解决了上述2个问题:
application.properties中增加redis的配置信息
spring.redis.host=10.9.36.120
spring.redis.password=****
spring.redis.port=6379
spring.redis.database=1
redis配置类
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
/*//缓存管理器
@Bean
public CacheManager cacheManager(@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
//设置缓存过期时间
cacheManager.setDefaultExpiration(10000);
return cacheManager;
}*/
@Bean
@ConditionalOnMissingBean(StringRedisTemplate.class)
public StringRedisTemplate stringRedisTemplate(
RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
//因为我的场景中Key都是普通的String,所以这里写死了String
@Bean
public <String,V>RedisTemplate<String, V> redisTemplate(RedisConnectionFactory factory){
RedisTemplate<String, V> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
setSerializer(template);
template.afterPropertiesSet();
return template;
}
private void setSerializer(RedisTemplate template){
//key的序列化器为string
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//value的序列化为json格式,使用Jackson2JsonRedisSerializer
@SuppressWarnings({ "rawtypes", "unchecked" })
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setDefaultSerializer(jackson2JsonRedisSerializer);//先 全部为json序列化
template.setKeySerializer(stringRedisSerializer);//key为String序列化
template.setHashKeySerializer(stringRedisSerializer);//hashKey为String序列化
}
}
测试:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest( classes = MainApplication.class)
public class TestRedis {
@Resource
RedisTemplate<String,Student> redisTemplateStudent;
@Resource
RedisTemplate<String,Person> redisTemplatePerson;
@Test
public void TestReidsStudent(){
ValueOperations<String, Student> valueOperations = redisTemplateStudent.opsForValue();
Student student = new Student();
student.setId(1L);
student.setName("zhansan");
student.setAge(12);
valueOperations.set(student.getName(),student);
Student result = valueOperations.get(student.getName());
System.out.println(result);
}
@Test
public void TestReidsPerson(){
ValueOperations<String, Person> valueOperations = redisTemplatePerson.opsForValue();
Person person = new Person();
person.setName("zhansan");
person.setAge(12);
valueOperations.set(person.getName(),person);
Person result = valueOperations.get(person.getName());
System.out.println(result);
}
}
查看结果: