当前位置:网站首页>三 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一样就可以获取了解决了上面的问题。边栏推荐
猜你喜欢

代码越写越乱?那是因为你没用责任链

进程间通信 --- system V三种通信方式(图文案例讲解)

Network connection optimization for instant messaging mobile terminal development

面试官:大量请求 Redis 不存在的数据,从而影响数据库,该如何解决?
![验证二叉树的前序序列化[抽象前序遍历]](/img/14/461409ce34369db69e569215f91880.png)
验证二叉树的前序序列化[抽象前序遍历]

Violence recursion to dynamic programming 02 (very clever game of CARDS)

有关包装类的一道经典面试题

How to merge the code when there is a code conflict in the collaborative development of multiple people?

日志打印不规范,被CTO骂了一顿~

关于知识付费的一些思考
随机推荐
About inner classes
R错误:缺少值不允许写在下面的作业的数据帧
Shared memory - shmget filling holes
C#线程操作UI控件
基于降阶扩张状态观测器的逆变系统重复控制设计
全球级的分布式数据库 Google Spanner原理 热:报错
马尔可夫跳变线性系统最优控制的研究现状与进展
The reason for Apple's official price reduction has been found, and it is also facing declining sales and even inventory problems
Pinia状态持久化
打卡广汽本田喜悦安全驾驶中心,体验最刁钻的场地训练
通过二维顺序表实现杨辉三角
还在开发短信验证码登录?试试(本机号码一键登录)
解决:Parameter 0 of method ribbonServerList in com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfigu
1192. 奖金
手摸手写一个互联网黑话生成器
中国互联网科技企业群狼围攻,谷歌终于遭受重挫导致利润大跌,它为推动鸿蒙的发展而后悔...
Nine kinds of way, teach you to read the resources files in the directory path
The Location object of BOM series
进程间通信 --- system V三种通信方式(图文案例讲解)
[10:00 Open Class]: Application Exploration of Kuaishou GPU/FPGA/ASIC Heterogeneous Platform