当前位置:网站首页>编写一个自己的 RedisTemplate
编写一个自己的 RedisTemplate
2022-04-23 05:43:00 【林仔520】
package com.lzjtu.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
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;
/** * Created by DELL on 2021/3/17 */
@Configuration
public class RedisConfig {
// 编写自己的redisTemplate
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
// 我们为了自己开发方便,一般直接使用<String, Object>
RedisTemplate<String, Object> template = new RedisTemplate();
template.setConnectionFactory(factory);
// Json序列化配置
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);
// String 的序列化
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// 配置具体的序列化方式
// key 采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash 的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化的方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用Jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
版权声明
本文为[林仔520]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_37955704/article/details/114915806
边栏推荐
- POI generates excel and inserts pictures
- Navicate连接oracle(11g)时ORA:28547 Connection to server failed probable Oeacle Net admin error
- 多个一维数组拆分合并为二维数组
- AcWing 1096. Detailed notes of Dungeon Master (3D BFS) code
- Find the number of "blocks" in the matrix (BFS)
- PreparedStatement防止SQL注入
- mysql如何将存储的秒转换为日期
- Golang implements Ping connectivity detection case through exec module
- Redis经典面试题总结2022
- 橙单微服务之批量导入
猜你喜欢
Isosceles triangle - the 9th Lanqiao provincial competition - group C
C language - Spoof shutdown applet
Find the number of "blocks" in the matrix (BFS)
基于thymeleaf实现数据库图片展示到浏览器表格
Software architecture design - software architecture style
JVM family (4) -- memory overflow (OOM)
JVM系列(4)——内存溢出(OOM)
PyEMD安装及简单使用
2-軟件設計原則
Flutter 新一代图形渲染器 Impeller
随机推荐
xxl-job采坑指南xxl-rpc remoting error(connect timed out)
对象转map
Strategy for improving the conversion rate of independent stations | recovering abandoned users
解决报错:ImportError: IProgress not found. Please update jupyter and ipywidgets
Strategies to improve Facebook's touch rate and interaction rate | intelligent customer service helps you grasp users' hearts
Flutter nouvelle génération de rendu graphique Impeller
lambda表达式
ES6之解构函数
Map object map get(key)
PreparedStatement防止SQL注入
MySQL realizes master-slave replication / master-slave synchronization
Map对象 map.get(key)
SQL statement simple optimization
Date增加天数
Golang通过exec模块实现Ping连通性检测案例
Jiugong magic square - the 8th Lanqiao provincial competition - group C (DFS and comparison of all magic square types)
数据安全入门产品——数据库审计系统详解
Getting started with JDBC \ getting a database connection \ using Preparedstatement
Step on the pit: Nacos uses startup CMD - M standalone startup error
MySQL triggers, stored procedures, stored functions