1、put(H var1, HK var2, HV var3)
新增hashMap值
- var1 为Redis的key
- var2 为key对应的map值的key
- var3 为key对应的map值的值
- var2相同替换var3
redisTemplate.opsForHash().put("hashValue","map1","value1");
redisTemplate.opsForHash().put("hashValue","map2","value2");
2、get(H var1, Object var2)
获取key对应的map中,key为var2的map的对应的值
Object o = redisTemplate.opsForHash().get("hashValue", "map1");
System.out.println("o = " + o);
3、entries(H key)
获取key对应的所有map键值对
Map hashValue = redisTemplate.opsForHash().entries("hashValue");
System.out.println("hashValue = " + hashValue);
4、keys(H key)
获取key对应的map中所有的键
Set hashValue = redisTemplate.opsForHash().keys("hashValue");
System.out.println("hashValue = " + hashValue);
5、values(H key)
获取key对应的map中所有的值
6、hasKey(H key, Object var2)
判断key对应的map中是否有指定的键
Boolean aBoolean = redisTemplate.opsForHash().hasKey("hashValue", "map1");
System.out.println("aBoolean = " + aBoolean);
7、size(H key)
获取key对应的map的长度
Long hashValue = redisTemplate.opsForHash().size("hashValue");
System.out.println("hashValue = " + hashValue);
8、putIfAbsent(H key, HK var2, HV var3)
如何key对应的map不存在,则新增到map中,存在则不新增也不覆盖
redisTemplate.opsForHash().putIfAbsent("hashValue", "map3", "value3");
9、putAll(H key, Map<? extends HK, ? extends HV> map)
直接以map集合的方式添加key对应的值
- map中key已经存在,覆盖替换
- map中key不存在,新增
Map newMap = new HashMap();
newMap.put("map4","map4");
newMap.put("map5","map5");
redisTemplate.opsForHash().putAll("hashValue",newMap);
10、multiGet(H key, Collection var2)
以集合的方式获取这些键对应的map
List list = new ArrayList<>();
list.add("map1");
list.add("map2");
List hashValue = redisTemplate.opsForHash().multiGet("hashValue", list);
System.out.println("hashValue = " + hashValue);
11、lengthOfValue(H key, HK var2)
获取指定key对应的map集合中,指定键对应的值的长度
Long aLong = redisTemplate.opsForHash().lengthOfValue("hashValue", "map1");
System.out.println("aLong = " + aLong);
12、increment(H key, HK var2, long long1)
使key对应的map中,键var2对应的值以long1自增
Long increment = redisTemplate.opsForHash().increment("hashValue", "map7", 1);
System.out.println("increment = " + increment);
13、increment(H key, HK var2, double d1)
使key对应的map中,键var2对应的值以double类型d1自增
Double increment = redisTemplate.opsForHash().increment("hashValue", "map8", 1.2);
System.out.println("increment = " + increment);
14、scan(H var1, ScanOptions var2)
匹配获取键值对
- ScanOptions.NONE为获取全部键对
- ScanOptions.scanOptions().match(“map1”).build(),匹配获取键位map1的键值对,不能模糊匹配
Cursor<Map.Entry<Object,Object>> cursor = redisTemplate.opsForHash().scan("hashValue",ScanOptions.scanOptions().match("map1").build());
//Cursor<Map.Entry<Object,Object>> cursor = redisTemplate.opsForHash().scan("hashValue",ScanOptions.NONE);
while (cursor.hasNext()) {
Map.Entry<Object, Object> entry = cursor.next();
System.out.println("entry.getKey() = " + entry.getKey());
System.out.println("entry.getValue() = " + entry.getValue());
}
15、delete(H key, Object… var2)
删除key对应的map中的键值对
Long delete = redisTemplate.opsForHash().delete("hashValue", "map1", "map2");
System.out.println("delete = " + delete);
16、拓展
map中存储对象、对象集合时最好转为JSON字符串,容易解析
map中键值对都可以存为对象、对象集合JSON字符串,具体看实际应用存储
List<MPEntity> list = mpService.list();
redisTemplate.opsForHash().put("hashValue",JSON.toJSONString(list),JSON.toJSONString(list));
本文由博客一文多发平台 OpenWrite 发布!