当前位置:网站首页>三 RedisTemplate 序列化机制配置实战
三 RedisTemplate 序列化机制配置实战
2022-07-29 14:01:00 【橘子ꦿ.๓】
自定义序列化和反序列化机制配置实战
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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;
@Configuration
public class RedisTemplateConfiguration {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 使用Jackson2JsonRedisSerialize 替换默认序列化
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// 设置key和value的序列化规则
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
// 设置hashKey和hashValue的序列化规则
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
// 设置支持事物
//redisTemplate.setEnableTransactionSupport(true);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
当配置了序列化模板后,redisTemplate 和StringredisTemplate只要key一样就可以获取了解决了上面的问题。边栏推荐
- How to merge the code when there is a code conflict in the collaborative development of multiple people?
- Children's programming electronics (graphical programming Scratch secondary level exam parsing (choice) in June 2022
- 全面质量管理理论
- 即时通讯移动端开发之网络连接优化
- Shared memory - shmget filling holes
- 为什么 ThreadLocal 可以做到线程隔离?
- AI全流程开发难题破解之钥
- Google Cloud X Kyligence|如何从业务视角管理数据湖?
- 上个厕所的功夫,就把定时任务的三种调度策略说得明明白白
- kubernetes cks strace etcd
猜你喜欢

rosbag数据画图MATLAB

Project Manager: Not bad!The SSO single sign-on code is written, and the sequence diagram is also drawn?

Programmers are a group with a high incidence of occupational diseases. Don’t be naive to think that it’s just as simple as being bald.

即时通讯移动端开发之网络连接优化

国产手机将用户变成它们的广告肉鸡,难怪消费者都买iPhone了

【Postman】Download and installation (novice graphic tutorial)

Network connection optimization for instant messaging mobile terminal development

【Postman】下载与安装(新手图文教程)

搞直播啦,千视超高清4K NDI编解码器8月3日19:00准时开播

何为擦除机制,泛型的上界?
随机推荐
电视处理器a53和a55哪个厉害(cortexa55处理器好吗)
leetcode linked list topic
九种方式,教你读取 resources 目录下的文件路径
EA&UML日拱一卒-活动图::CallOperationAction(续)
Still developing SMS verification code login?Try it (one-click login with your phone number)
第4章_3——索引的使用
【pytorch】1.6 tensor 基本运算
Programmers are a group with a high incidence of occupational diseases. Don’t be naive to think that it’s just as simple as being bald.
请写出用Graphics2D类的新方法画一个图形的步骤(表格如何画斜线)
如何返回一个数字的所有质因数?
Shared memory - shmget filling holes
trivy如何从非关系型数据库查询数据
用TypeScript类型系统编程实现斐波那契数列
【10点公开课】:快手GPU/FPGA/ASIC异构平台的应用探索
Nacos基础教程
无线传感器网络定位综述
EA&UML日拱一卒-活动图::StartClassifierBehavior和StartObjectBehavior
Chinese Internet technology companies were besieged by wolves. Google finally suffered a severe setback and its profits fell sharply. It regretted promoting the development of Hongmeng...
潘多拉 IOT 开发板学习(RT-Thread)—— 实验19 MQTT 协议通信实验(学习笔记)
Vscode搭建ESP32-C3开发环境