1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
   |  package com.example.springdataredistext;
  import com.example.springdataredistext.entity.User; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
  import java.util.Map;
  @SpringBootTest class SpringDataRedisTextApplicationTests {     @Autowired     private RedisTemplate<String,Object> redisTemplate;     @Autowired     private StringRedisTemplate stringRedisTemplate;     private static final ObjectMapper mapper = new ObjectMapper();     @Test     void contextLoads() {         redisTemplate.opsForValue().set("spring","helloRedis");         Object o = redisTemplate.opsForValue().get("spring");         System.out.println(o);     }     @Test     void text2(){       redisTemplate.opsForValue().set("spring:user:2",new User(2,"冯越"));       User user =(User) redisTemplate.opsForValue().get("spring:user:2");         System.out.println(user);     }     @Test     void text3() throws JsonProcessingException {         User user = new User(3,"我");         String json = mapper.writeValueAsString(user);         stringRedisTemplate.opsForValue().set("spring:user:3",json);         String re = stringRedisTemplate.opsForValue().get("spring:user:3");         User result = mapper.readValue(re,User.class);         System.out.println(result);     }     @Test     public void text4(){         stringRedisTemplate.opsForHash().put("spring:user:400","id","400");         stringRedisTemplate.opsForHash().put("spring:user:400","name","冯越");         Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries("spring:user:400");         System.out.println(entries);     }
 
  }
 
 
 
  |