RedisTemplate中boundHashOps()的使用

       首先要定义一个BoundHashOperations

BoundHashOperations<String, String, Object> boundHashOperations = redisTemplate.boundHashOps("li");

 1、put(HK key, HV value)

   新增元素到指定键中

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));

 

 5、get(Object member)

  获取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());

}

 

13、delete(Object... keys)

  批量删除map值

long delSize = boundHashOperations.delete("m3","m2");

System.out.println("删除的键的个数:" + delSize);

boundHashOperations.entries().forEach((m,n) -> System.out.println("删除后剩余map键值对:" + m + "-" + n));

 

 

 

 

posted @ 2020-11-27 15:46  昶·Cc  阅读(3237)  评论(1编辑  收藏  举报