1.导入依赖
<!-https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.3.4.RELEASE</version> </dependency>
2.添加配置项
spring.redis.host=8.136.203.12 spring.redis.port=6379 spring.redis.password=
3.写配置类 RedisConfig
package com.kuang.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.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; @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate1(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate(); template.setConnectionFactory(redisConnectionFactory); //序列化 Jackson2JsonRedisSerializer objectJackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); objectJackson2JsonRedisSerializer.setObjectMapper(om); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); //key用String序列化 template.setKeySerializer(stringRedisSerializer); template.setHashKeySerializer(stringRedisSerializer); //value用jackson序列化 template.setValueSerializer(objectJackson2JsonRedisSerializer); template.setHashValueSerializer(objectJackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } }
4.写User类
package com.kuang.controller; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.stereotype.Component; @Component @Data @AllArgsConstructor @NoArgsConstructor //这里不用jdk的序列化,采用配置类里的自定义序列化,因此这里不用实现Serializable接口 public class User { private String name; private int age; }
5.写测试类
package com.kuang; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.kuang.controller.User; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; @SpringBootTest class SpringbootSpringsecurityApplicationTests { @Autowired @Qualifier("redisTemplate1") RedisTemplate redisTemplate; @Test void contextLoads() { redisTemplate.opsForValue().set("wcc1","魏灿灿"); System.out.println(redisTemplate.opsForValue().get("wcc")); } @Test public void test() throws JsonProcessingException { User wcc = new User("wcc", 20); //利用objectMapper序列化user对象,因为在配置类里自定义了序列化,因此这里可以注释掉 // String jsonUser = new ObjectMapper().writeValueAsString(wcc); redisTemplate.opsForValue().set("user",wcc); System.out.println(redisTemplate.opsForValue().get("user")); } }
测试结果:
原文:https://www.cnblogs.com/wcc-blog/p/14486236.html