StringRedisTemplate操作hash實(shí)現(xiàn)過程
更新時(shí)間:2026年03月12日 10:37:12 作者:小石潭記丶
文章總結(jié)了使用StringRedisTemplate操作Redis哈希(hash)的基本示例,分享了個人經(jīng)驗(yàn),旨在為讀者提供參考,并鼓勵大家支持腳本之家
StringRedisTemplate操作hash
示例如下
package com.frank.redis.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
/**
* @author 小石潭記
* @date 2021/6/8 20:34
* @Description: ${todo}
*/
@RestController
@RequestMapping("/redis-test")
public class RedisTestController {
@Autowired
private StringRedisTemplate redisTemplate;
@GetMapping("/demo1")
public void demo1() {
//put方法
redisTemplate.opsForHash().put("k1", "name", "frank");
redisTemplate.opsForHash().put("k1", "age", "22");
redisTemplate.opsForHash().put("k1", "height", "176");
//hashKey不存在時(shí),才設(shè)值
//redisTemplate.opsForHash().putIfAbsent(key, hashKey, value)
}
@GetMapping("/demo2")
public void demo2() {
//putAll方法
Map<String, String> data = new HashMap<>();
data.put("name", "jack ma");
data.put("company", "alibaba");
data.put("age", "500");
redisTemplate.opsForHash().putAll("k2", data);
}
@GetMapping("/demo3")
public void demo3() {
//delete方法,刪除key對應(yīng)的hash的hashkey及其value
redisTemplate.opsForHash().delete("k2", "name");
}
@GetMapping("/demo4")
public void demo4() {
//hasKey方法,確定hashkey是否存在
System.out.println(redisTemplate.opsForHash().hasKey("k2", "name"));
}
@GetMapping("/demo5")
public void demo5() {
//get方法,根據(jù)key和hashkey找出對應(yīng)的值
System.out.println(redisTemplate.opsForHash().get("k1", "name"));
}
@GetMapping("/demo6")
public void demo6() {
//multiGet方法,根據(jù)key和多個hashkey找出對應(yīng)的多個值
Collection<Object> keys = new ArrayList<>();
keys.add("name");
keys.add("age");
System.out.println(redisTemplate.opsForHash().multiGet("k1", keys));
}
@GetMapping("/demo7")
public void demo7() {
//increment方法,對key和hashkey對應(yīng)的值進(jìn)行增加操作
//增加長整形(無法對浮點(diǎn)數(shù)據(jù)使用本方法)
System.out.println(redisTemplate.opsForHash().increment("k1", "age", 1));
//增加浮點(diǎn)型(可以對整形數(shù)據(jù)使用本方法)
System.out.println(redisTemplate.opsForHash().increment("k1", "age", 1.0));
}
@GetMapping("/demo8")
public void demo8() {
//keys方法,獲取key對應(yīng)的hash表的所有key
Set<Object> keys = redisTemplate.opsForHash().keys("k1");
System.out.println(keys);
//values方法,獲取key對應(yīng)的hash表的所有value
List<Object> values = redisTemplate.opsForHash().values("k1");
System.out.println(values);
}
@GetMapping("/demo9")
public void demo9() {
//keys方法,獲取key對應(yīng)的hash表的大小
long size = redisTemplate.opsForHash().size("k1");
System.out.println(size);
}
@GetMapping("/demo10")
public void demo10() {
//keys方法,獲取key對應(yīng)的hash表的所有鍵值對
Map<Object, Object> entries = redisTemplate.opsForHash().entries("k1");
System.out.println(entries);
}
}
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- SpringBoot3.4.0無法找到StringRedisTemplate?bean的問題Consider?defining?a?bean?of?type?‘org.springframework
- SpringBoot混合使用StringRedisTemplate和RedisTemplate的坑及解決
- 使用StringRedisTemplate操作Redis方法詳解
- Java中StringRedisTemplate和RedisTemplate的區(qū)別及使用方法
- SpringBoot整合Redis使用RedisTemplate和StringRedisTemplate
- 淺談RedisTemplate和StringRedisTemplate的區(qū)別
相關(guān)文章
springboot項(xiàng)目整合mybatis并配置mybatis中間件的實(shí)現(xiàn)
這篇文章主要介紹了springboot項(xiàng)目整合mybatis并配置mybatis中間件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
idea找不到創(chuàng)建package包的選項(xiàng)問題及解決方案
在IntelliJ IDEA中找不到創(chuàng)建package包選項(xiàng)?按照步驟操作即可顯示:右鍵選擇Mark Directory as -> Source Root2026-03-03
springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes
這篇文章主要介紹了springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
Spring學(xué)習(xí)之開發(fā)環(huán)境搭建的詳細(xì)步驟
本篇文章主要介紹了Spring學(xué)習(xí)之開發(fā)環(huán)境搭建的詳細(xì)步驟,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
Mybatis 一對多和多對一關(guān)聯(lián)查詢問題
這篇文章主要介紹了Mybatis 一對多和多對一關(guān)聯(lián)查詢問題,需要的朋友可以參考下2017-04-04
一文分享Java日期解析的完整方案(覆蓋50+種格式自動識別)
寫后端接口,日期解析是個躲不過去的坎,本文主要和大家分享一下Java中日期解析的完整方案,一個方法可以覆蓋50+種格式自動識別,希望對大家有所幫助2026-06-06
深入解析Java實(shí)現(xiàn)文件寫入磁盤的全鏈路過程
寫一行簡單的 Java 文件操作代碼,數(shù)據(jù)就能順利保存到磁盤,這背后到底經(jīng)歷了什么,本文將從源碼到硬件,全方位拆解這個過程,有需要的可以了解下2025-05-05

