redis中通过redisTemplate存放后再修改map值(遍历Map),同时得到map里面value对象的属性值

redis中通过redisTemplate存放后再修改map值,遍历Map,同时得到map里面value对象的属性值

代码(分为三个测试):

@Autowired
private RedisTemplate redisTemplate;
@Test
    public void test03(){
        Map<String, Object> map = new HashMap();
        map.put("123","商品1");
        map.put("124","商品2");
        map.put("125","商品3");
        Store store = new Store();
        store.setBusinessLicense("测试商品1");
        map.put("126",store);
        redisTemplate.opsForValue().set("1",map);

        Map<String, Object> o = (Map)redisTemplate.opsForValue().get("1");

        for (Map.Entry<String, Object> entry : o.entrySet()) {
            String mapKey = entry.getKey();
            Object mapValue = entry.getValue();
            System.out.println("key= "+mapKey+" ,value= "+mapValue);
        }

        System.out.println("~~~~~~~~~~~~~~~~~~~~~~测试一结束(遍历redis存放的map)~~~~~~~~~~~~~~~~~~~~~~~~");

        Map<String, Object> map1 = new HashMap();
        map1.put("123","商品1");
        map1.put("124","商品2");
        map1.put("125","商品4");
        map1.put("126","商品5");
        Store store1 = new Store();
        store1.setBusinessLicense("测试商品2");
        map1.put("136",store1);
        redisTemplate.opsForValue().set("1",map1,10, TimeUnit.MINUTES);

        Map<String, Object> k = (Map)redisTemplate.opsForValue().get("1");

        for (Map.Entry<String, Object> entry : k.entrySet()) {
            String mapKey = entry.getKey();
            Object mapValue = entry.getValue();
            System.out.println("key= "+mapKey+" ,value= "+mapValue);
        }

        System.out.println("~~~~~~~~~~~~~~~~~~~~~~测试二结束(修改redis里面的map)~~~~~~~~~~~~~~~~~~~~~~~~");

        Map<String, Object> m = (Map)redisTemplate.opsForValue().get("1");
        JSONObject jsonObject =  (JSONObject)m.get("136");
        Store s = JSONObject.toJavaObject(jsonObject, Store.class);
        System.out.println(s.getBusinessLicense());

        System.out.println("~~~~~~~~~~~~~~~~~~~~~~测试三结束(取map里面存放的map对象的一个属性值)~~~~~~~~~~~~~~~~~~~~~~~~");
    }

你可能感兴趣的:(redis)