Spring Boot中RedisTemplate的使用示例詳解
當(dāng)前Spring Boot的版本為2.7.6,在使用RedisTemplate之前我們需要在pom.xml中引入下述依賴(lài):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>3.1.4</version> </dependency>
同時(shí)在application.yml文件中添加下述配置:
spring
redis:
host: 127.0.0.1
port: 6379
一、opsForHash
RedisTemplate.opsForHash()是RedisTemplate類(lèi)提供的用于操作Hash類(lèi)型的方法,它可以用于對(duì)Redis中的Hash數(shù)據(jù)結(jié)構(gòu)進(jìn)行各種操作,如設(shè)置字段值、獲取字段值、刪除字段值等。
1.1 設(shè)置哈希字段的值
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void test(){
redisTemplate.opsForHash().put("fruit:list", "1", "蘋(píng)果");
}
}
在上述代碼能正常運(yùn)行的情況下,我們?cè)诮K端中執(zhí)行 redis-cli 命令進(jìn)入到redis的控制臺(tái)中,然后執(zhí)行 keys * 命令查看所有的key,結(jié)果發(fā)現(xiàn)存儲(chǔ)在redis中的key不是設(shè)置的string值,前面還多出了許多類(lèi)似 \xac\xed\x00\x05t\x00 這種字符串,如下圖所示:

這是因?yàn)镾pring-Data-Redis的RedisTemplate<K, V>模板類(lèi)在操作redis時(shí)默認(rèn)使用JdkSerializationRedisSerializer來(lái)進(jìn)行序列化,因此我們要更改其序列化方式:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisTemplateConfig {
@Bean
public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object,Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
RedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setValueSerializer(stringRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
redisTemplate.setHashValueSerializer(stringRedisSerializer);
return redisTemplate;
}
}
需要說(shuō)明的是這種配置只是針對(duì)所有的數(shù)據(jù)都是String類(lèi)型,如果是其它類(lèi)型,則根據(jù)需求修改一下序列化方式。
使用 flushdb 命令清除完所有的數(shù)據(jù)以后,再次執(zhí)行上述測(cè)試案例,接著我們?cè)俅稳ゲ榭此械膋ey,這個(gè)看到數(shù)據(jù)已經(jīng)正常:

接著使用 hget fruit:list 1 命令去查詢(xún)剛剛存儲(chǔ)的數(shù)據(jù),這時(shí)又發(fā)現(xiàn)對(duì)應(yīng)字段的值中文顯示亂碼:
\xe8\x8b\xb9\xe6\x9e\x9c

這個(gè)時(shí)候需要我們?cè)谶M(jìn)入redis控制臺(tái)前,添加 --raw 參數(shù):
redis-cli --raw

1.2 設(shè)置多個(gè)哈希字段的值
設(shè)置多個(gè)哈希字段的值一種很簡(jiǎn)單的粗暴的方法是多次執(zhí)行opsForHash().put()方法,另外一種更優(yōu)雅的方式如下:
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void test(){
Map<String,String> map = new HashMap<>();
map.put("1","蘋(píng)果");
map.put("2","橘子");
map.put("3","香蕉");
redisTemplate.opsForHash().putAll("fruit:list",map);
}
}
1.3 獲取哈希字段的值
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void test(){
String value = (String) redisTemplate.opsForHash().get("fruit:list","1");
System.out.println(value);
}
}
1.4 獲取多個(gè)哈希字段的值
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Arrays;
import java.util.List;
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void test(){
List<String> values = redisTemplate.opsForHash().multiGet("fruit:list", Arrays.asList("1", "2","3"));
System.out.println(values);
}
}
1.5 判斷哈希中是否存在指定的字段
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void test(){
Boolean hasKey = redisTemplate.opsForHash().hasKey("fruit:list", "1");
System.out.println(hasKey);
}
}
1.6 獲取哈希的所有字段
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Set;
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void test(){
Set<String> keys = redisTemplate.opsForHash().keys("fruit:list");
System.out.println(keys);
}
}
1.7 獲取哈希的所有字段的值
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void test(){
List<String> values = redisTemplate.opsForHash().values("fruit:list");
System.out.println(values);
}
}
1.8 獲取哈希的所有字段和對(duì)應(yīng)的值
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Map;
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void test(){
Map<String, String> entries = redisTemplate.opsForHash().entries("fruit:list");
System.out.println(entries);
}
}
1.9 刪除指定的字段
返回值返回的是刪除成功的字段的數(shù)量,如果字段不存在的話(huà),則返回的是0。
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void test(){
Long deletedFields = redisTemplate.opsForHash().delete("fruit:list", "4");
System.out.println(deletedFields);
}
}
1.10 如果哈希的字段存在則不會(huì)添加,不存在則添加
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void test(){
Boolean success = redisTemplate.opsForHash().putIfAbsent("fruit:list","4","西瓜");
System.out.println(success);
}
}
1.11 將指定字段的值增加指定步長(zhǎng)
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void test(){
Long incrementedValue = redisTemplate.opsForHash().increment("salary:list", "1", 5);
System.out.println(incrementedValue);
}
}
如果字段不存在,則將該字段的值設(shè)置為指定步長(zhǎng),并且返回該字段當(dāng)前的值;如果字段存在,則在該字段原有值的基礎(chǔ)上增加指定步長(zhǎng),返回該字段當(dāng)前的最新值。 該方法只適用于字段值為int類(lèi)型的數(shù)據(jù),因此關(guān)于哈希數(shù)據(jù)結(jié)構(gòu)的value值的序列化方式要有所改變:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisTemplateConfig {
@Bean
public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object,Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
RedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setValueSerializer(stringRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
return redisTemplate;
}
}
StringRedisTemplate的好處就是在RedisTemplate基礎(chǔ)上封裝了一層,指定了所有數(shù)據(jù)的序列化方式都是采用StringRedisSerializer(即字符串),使用語(yǔ)法上面完全一致。
public class StringRedisTemplate extends RedisTemplate<String, String> {
public StringRedisTemplate() {
this.setKeySerializer(RedisSerializer.string());
this.setValueSerializer(RedisSerializer.string());
this.setHashKeySerializer(RedisSerializer.string());
this.setHashValueSerializer(RedisSerializer.string());
}
}
二、opsForValue
RedisTemplate.opsForValue()是RedisTemplate類(lèi)提供的用于操作字符串值類(lèi)型的方法。它可以用于對(duì)Redis中的字符串值進(jìn)行各種操作,如設(shè)置值、獲取值、刪除值等。
2.1 設(shè)置一個(gè)鍵值對(duì)
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.concurrent.TimeUnit;
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void test(){
String key = "fruit";
String value = "apple";
redisTemplate.opsForValue().set(key,value,30,TimeUnit.SECONDS);
}
}
2.2 根據(jù)鍵獲取對(duì)應(yīng)的值
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void test(){
String value = (String) redisTemplate.opsForValue().get("fruit");
System.out.println(value);
}
}
到此這篇關(guān)于Spring Boot中RedisTemplate的使用的文章就介紹到這了,更多相關(guān)Spring Boot RedisTemplate使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java使用RedisTemplate如何根據(jù)前綴獲取key列表
- 使用redisTemplate從redis獲取所有數(shù)據(jù)
- SpringBoot整合Redis使用RedisTemplate和StringRedisTemplate
- Java中StringRedisTemplate和RedisTemplate的區(qū)別及使用方法
- 使用StringRedisTemplate操作Redis方法詳解
- Spring中RedisTemplate使用方法詳解
- Java使用RedisTemplate操作Redis遇到的坑
- Redis使用RedisTemplate導(dǎo)致key亂碼問(wèn)題解決
- RedisTemplate的使用與注意事項(xiàng)小結(jié)
相關(guān)文章
Java實(shí)現(xiàn)PNG圖片格式轉(zhuǎn)BMP圖片格式
在實(shí)際開(kāi)發(fā)中,有時(shí)需要在不同平臺(tái)、不同應(yīng)用場(chǎng)景中對(duì)圖片格式進(jìn)行轉(zhuǎn)換,本文主要介紹了如何使用 Java 語(yǔ)言實(shí)現(xiàn)將 PNG 格式的圖片轉(zhuǎn)換為 BMP 格式的圖片,需要的可以了解下2025-03-03
簡(jiǎn)單聊聊Java中驗(yàn)證碼功能的實(shí)現(xiàn)
相信大家都經(jīng)常接觸到驗(yàn)證碼的,畢竟平時(shí)上網(wǎng)也能遇到各種驗(yàn)證碼,需要我們輸入驗(yàn)證碼進(jìn)行驗(yàn)證我們是人類(lèi),本篇文章就從這幾個(gè)方面出發(fā)說(shuō)說(shuō)驗(yàn)證碼,廢話(huà)不多說(shuō),下面開(kāi)始正文2023-06-06
Java設(shè)計(jì)模式之迭代器模式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java設(shè)計(jì)模式之迭代器模式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理,需要的朋友可以參考下2017-08-08
JAVA實(shí)現(xiàn)二維碼生成加背景圖代碼實(shí)例
這篇文章主要介紹了JAVA實(shí)現(xiàn)二維碼生成加背景圖代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
Jenkins一鍵打包部署SpringBoot應(yīng)用
本文主要介紹了Jenkins一鍵打包部署SpringBoot應(yīng)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

