1、pom 添加依赖
<!-- redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2、Redis 配置类
package com.exam.config; import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer; 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.StringRedisSerializer; @Configuration public class RedisConfig { @Bean // @SuppressWarnings(value = {"sf","sdf"}) // SuppressWarnings注解的作用是用于抑制编译器产生某些类型的警告 public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){ RedisTemplate<Object, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); FastJsonRedisSerializer serializer = new FastJsonRedisSerializer(Object.class); // 使用 StringRedisSerializer 来序列化与发序列化 redis 的 key 值 template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(serializer); // Hash 的 key 也采用 StringRedisSerializer 的序列方式 template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(serializer); template.afterPropertiesSet(); return template; } }
标签:配置,redis,不全,springframework,StringRedisSerializer,template,org,import From: https://www.cnblogs.com/wangdch/p/18339285