package cn.kgc.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @Author: ldj
* @Date: 2021/10/19/15:44
* @Description:
*/
@Configuration
public class RedisConfig {
/**
* 注意!
* 1. 方法名不可以随便改,就叫redisTemplate,要不然配置不生效
* 2. RedisConnectionFactory spring boot在容器创建好了
*/
@Bean
public RedisTemplate<Object,Object> redisTemlpate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<Object,Object> redisTemplate=new RedisTemplate<>();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
/**
* 让bean对象知道操作那个Redis,好比application.yml配置文件
* url: jdbc:mysql://localhost:3306/dbtest?serverTimezone=GMT%2B8
*/
redisTemplate.setConnectionFactory(redisConnectionFactory);
/**
* 修改序列化方式
*/
//String类型
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
//Hash类型
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
//String类型
redisTemplate.setStringSerializer(new StringRedisSerializer());
return redisTemplate;
}
}
配置Redis序列化方式不生效问题,因方法名不一致!
最新推荐文章于 2023-06-30 14:56:58 发布