首页 > 数据库 >springboot中重写RedisTemplate

springboot中重写RedisTemplate

时间:2022-09-22 19:14:50浏览次数:47  
标签:null springboot springframework RedisTemplate org import 序列化 重写 redisTemplate

1、引入jar包

<!-- 引入 redis 依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

 

2、重写配置类

package com.wisedu.common.config;
​
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.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
​
@Configuration
public class RedisConfig {
    /**
     * 默认是JDK的序列化策略,这里配置redisTemplate采用的是Jackson2JsonRedisSerializer的序列化策略
     */
    @Bean
    public RedisTemplate<String, Object> myRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
​
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        // 配置连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
​
        // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK序列化方式)
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
​
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String序列化
        redisTemplate.setKeySerializer(stringRedisSerializer);
        // hash的key采用String序列化
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        // value采用json序列化
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value采用json序列化
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
​
    /***
     * stringRedisTemplate默认采用的是String的序列化策略
     */
    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(redisConnectionFactory);
        return stringRedisTemplate;
    }
​
}

 

3、编写单元测试

@Autowired
@Qualifier("myRedisTemplate")
private RedisTemplate<String, Object> redisTemplate;
​
@Test
public void testUser() {
    UserDto userDto = new UserDto();
    userDto.setId("1001");
    userDto.setUserName("zhangsan");
    userDto.setMobileNo("13845630123");
    redisTemplate.opsForValue().set("name", "zhangsan");
    redisTemplate.opsForValue().set("user:1001", userDto);
}

 

4、测试结果

127.0.0.1:6379[1]> keys *
1) "name"
2) "user:1001"
127.0.0.1:6379[1]> get user:1001
"[\"com.wisedu.user.dto.UserDto\",{\"id\":\"1001\",\"userName\":\"zhangsan\",\"userNo\":null,\"password\":null,\"mobileNo\":\"13845630123\",\"customMobileNo\":null,\"officePhone\":null,\"receiveSmsFlag\":null,\"publicMobileNoFlag\":null,\"publicOfficePhoneFlag\":null,\"deptId\":null,\"deptCode\":null,\"deptName\":null,\"fullPathDeptName\":null,\"sex\":null,\"status\":null,\"deleteFlag\":null,\"teacherFlag\":null,\"duty\":null,\"lastLoginTime\":null,\"createTime\":null,\"schoolCode\":null,\"schoolName\":null,\"accessToken\":null,\"groupDtoList\":null,\"permissionDtoList\":null,\"needHandleGroup\":false,\"openId\":null,\"source\":null,\"timeout\":null}]"
127.0.0.1:6379[1]> get name
"\"zhangsan\""

 

标签:null,springboot,springframework,RedisTemplate,org,import,序列化,重写,redisTemplate
From: https://www.cnblogs.com/liu-dong/p/16720555.html

相关文章