# spring-boot-redis **Repository Path**: mbigger/spring-boot-redis ## Basic Information - **Project Name**: spring-boot-redis - **Description**: No description available - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-04-17 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## `spring-boot-starter-data-redis` 使用 ### 配置如下 ```java 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; import org.springframework.data.redis.serializer.RedisSerializer; @Configuration public class RedisConfig { @Bean public RedisTemplate coffeeRedisTemplate(RedisConnectionFactory factory) { RedisTemplate template = new RedisTemplate<>(); template.setConnectionFactory(factory); configTemplate(template,Coffee.class); return template; } @Bean public RedisTemplate rtValueTemplate(RedisConnectionFactory factory) { RedisTemplate template = new RedisTemplate<>(); template.setConnectionFactory(factory); configTemplate(template, RtValue.class); return template; } @Bean public RedisTemplate redisTemplate(RedisConnectionFactory factory) { RedisTemplate template = new RedisTemplate<>(); template.setConnectionFactory(factory); configTemplate(template, Object.class); return template; } private void configTemplate(RedisTemplate template,Class clazz){ Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(clazz); ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(mapper); RedisSerializer stringSerializer = new StringRedisSerializer(); // 如果key是String 需要配置一下StringSerializer,不然key会乱码 template.setKeySerializer(stringSerializer);// key序列化 template.setValueSerializer(jackson2JsonRedisSerializer);// value序列化 template.setHashKeySerializer(stringSerializer);// Hash key序列化 template.setHashValueSerializer(jackson2JsonRedisSerializer);// Hash value序列化 template.afterPropertiesSet(); } } ``` ### 使用方法 [参考文档](https://docs.spring.io/spring-data/redis/docs/2.0.2.RELEASE/reference/html/#redis:serializer) `JSON` 序列化方法: - `Jackson2JsonRedisSerializer` - ` GenericJackson2JsonRedisSerializer` ```java @RunWith(SpringRunner.class) @SpringBootTest @Slf4j @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class RedisApplicationTests { @Autowired private RedisTemplate coffeeRedisTemplate; @Autowired private RedisTemplate rtValueRedisTemplate; @Autowired private RedisTemplate redisTemplate; private Coffee coffee; private RtValue rtValue; @Before public void contextLoads() { this.coffee = new Coffee(); this.coffee.setId("1111"); this.coffee.setName("name"); this.rtValue = new RtValue(); this.rtValue.setId(1); this.rtValue.setQuality(2); this.rtValue.setTagName("c1.d1.yc.yc.1"); this.rtValue.setTimestamp(new Date()); this.rtValue.setValue(123.2344); } @Test public void aTestSet(){ log.info("set: {}", coffee); redisTemplate.opsForValue().set(coffee.getId(), coffee); } @Test public void bTestGet(){ Coffee returnCoffee = coffeeRedisTemplate.opsForValue().get(coffee.getId()); log.info("get: {}", returnCoffee); Assert.assertEquals(coffee.getId(), returnCoffee.getId()); } @Test public void cTestSet(){ log.info("set: {}", rtValue); rtValueRedisTemplate.opsForValue().set(rtValue.getTagName(), rtValue); } @Test public void dTestGet(){ RtValue returnRtValue = rtValueRedisTemplate.opsForValue().get(rtValue.getTagName()); log.info("get: {}", returnRtValue); Assert.assertEquals(rtValue.getId(), returnRtValue.getId()); } @Test public void eTestSet(){ log.info("set: {}", rtValue); redisTemplate.opsForValue().set(rtValue.getTagName(), rtValue); } @Test public void fTestGet(){ RtValue returnRtValue = (RtValue)redisTemplate.opsForValue().get(rtValue.getTagName()); log.info("get: {}", returnRtValue); Assert.assertEquals(rtValue.getId(), returnRtValue.getId()); } } ```