redisにredisTemplateで保存してからmap値(Mapを巡る)を変更し、map内のvalueオブジェクトの属性値を得る
14767 ワード
redisでredisTemplateで保存してからmap値を修正し、Mapを巡りながらmap内のvalueオブジェクトの属性値を得る
コード(3つのテストに分けられます):
コード(3つのテストに分けられます):
@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 )~~~~~~~~~~~~~~~~~~~~~~~~");
}