1.
<!--redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2.
spring: redis: host: 127.0.0.1 port: 6379 jedis: pool: max-active: 8 max-wait: -1 max-idle: 500 min-idle: 0 lettuce: shutdown-timeout: 0
3.
@Configuration public class RedisConfig{ @Bean public RedisTemplate<String, Object>redisTemplate(RedisConnectionFactory factory){ RedisTemplate<String,Object>template=new RedisTemplate<>(); //关联 template.setConnectionFactory(factory); //设置key的序列化器 template.setKeySerializer(new StringRedisSerializer()); //设置value的序列化器 template.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class)); return template; } }
4.
@Autowired private RedisTemplate<String,Object> redisTemplate; @Test public void testSet(){ JSONObject jsonObject = new JSONObject(); jsonObject.put("name","xiao"); jsonObject.put("age",11); this.redisTemplate.opsForValue().set("user",jsonObject); System.out.println((redisTemplate.opsForValue().get("user"))); }