首先要定義一個BoundHashOperations
BoundHashOperations<String, String, Object> boundHashOperations = redisTemplate.boundHashOps("li");
新增元素到指定鍵中
boundHashOperations.put("ww","i"); boundHashOperations.put("w1","i1"); boundHashOperations.put("w2","i2");
2、getKey()
獲取指定鍵中的值
//獲取設置的綁定key值 System.out.println("獲取設置的綁定key值:" + boundHashOperations.getKey());
3、values()
獲取map中的值jdk要求1.8及以上
//獲取map中的value值 boundHashOperations.values().forEach(v -> System.out.println("獲取map中的value值" + v));
4、entries()
獲取map中的鍵值對
//獲取map鍵值對 boundHashOperations.entries().forEach((m,n) -> System.out.println("獲取map鍵值對:" + m + "-" + n));
獲取map鍵中的值
//獲取map鍵的值 System.out.println("獲取map建的值:" + boundHashOperations.get("w1"));
6、keys()
獲取map的鍵
//獲取map的鍵 boundHashOperations.keys().forEach(v -> System.out.println("獲取map的鍵:" + v));
7、multiGet(Collection<HK> keys)
根據map鍵批量獲取map值
//根據map鍵批量獲取map值 List list = new ArrayList<>(Arrays.asList("ww","w1")); boundHashOperations.multiGet(list).forEach(v -> System.out.println("根據map鍵批量獲取map值:" + v));
8、putAll(Map<? extends HK,? extends HV> m)
批量添加鍵值對
//批量添加鍵值對 Map map = new HashMap<>(); map.put("m1","n1"); map.put("m2","n2"); boundHashOperations.putAll(map); boundHashOperations.entries().forEach((m,n) -> System.out.println("批量添加鍵值對:" + m + "-" + n));
9、increment(HK key, long delta)
自增長map鍵的值
//自增長map鍵的值 boundHashOperations.increment("c",1); System.out.println("自增長map鍵的值:" + boundHashOperations.get("c"));
10、putIfAbsent(HK key, HV value)
添加不存在的map鍵
//如果map鍵不存在,則新增,存在,則不變 boundHashOperations.putIfAbsent("m2","n2-1"); boundHashOperations.putIfAbsent("m3","n3"); boundHashOperations.entries().forEach((m,n) -> System.out.println("新增不存在的鍵值對:" + m + "-" + n));
11、size()
獲取特定鍵對應的map大小
//查看綁定建的map大小 System.out.println("查看綁定建的map大小:" + boundHashOperations.size());
12、scan(ScanOptions options)
掃描特定鍵所有值
//遍歷綁定鍵獲取所有值 Cursor<Map.Entry<String, Object>> cursor = boundHashOperations.scan(ScanOptions.NONE); while (cursor.hasNext()){ Map.Entry<String, Object> entry = cursor.next(); System.out.println("遍歷綁定鍵獲取所有值:" + entry.getKey() + "---" + entry.getValue()); }
批量刪除map值
long delSize = boundHashOperations.delete("m3","m2"); System.out.println("刪除的鍵的個數:" + delSize); boundHashOperations.entries().forEach((m,n) -> System.out.println("刪除后剩余map鍵值對:" + m + "-" + n));