最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot中RedisTemplate的使用示例詳解

 更新時(shí)間:2023年10月24日 15:22:24   作者:y_bccl27  
RedisTemplate.opsForHash()是RedisTemplate類(lèi)提供的用于操作Hash類(lèi)型的方法,它可以用于對(duì)Redis中的Hash數(shù)據(jù)結(jié)構(gòu)進(jìn)行各種操作,如設(shè)置字段值、獲取字段值、刪除字段值等,本文介紹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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java8中使用一行代碼讀取文件

    Java8中使用一行代碼讀取文件

    這篇文章主要介紹了Java8中使用一行代碼讀取文件,要注意,本文介紹的方法不適合讀取很大的文件,因?yàn)榭赡艽嬖趦?nèi)存空間不足的問(wèn)題,需要的朋友可以參考下
    2015-03-03
  • 深入理解Java設(shè)計(jì)模式之代理模式

    深入理解Java設(shè)計(jì)模式之代理模式

    這篇文章主要介紹了Java設(shè)計(jì)模式之代理模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下
    2021-11-11
  • Java實(shí)現(xiàn)PNG圖片格式轉(zhuǎn)BMP圖片格式

    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
  • MyBatis通過(guò)BATCH批量提交的方法

    MyBatis通過(guò)BATCH批量提交的方法

    今天小編就為大家分享一篇關(guān)于MyBatis通過(guò)BATCH批量提交的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-12-12
  • 簡(jiǎn)單聊聊Java中驗(yàn)證碼功能的實(shí)現(xiàn)

    簡(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通過(guò)itext生成pdf的干貨教程

    java通過(guò)itext生成pdf的干貨教程

    這篇文章主要介紹了java通過(guò)itext生成pdf的相關(guān)資料,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定幫助,需要的可以參考一下
    2022-06-06
  • Java設(shè)計(jì)模式之迭代器模式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    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í)例

    這篇文章主要介紹了JAVA實(shí)現(xiàn)二維碼生成加背景圖代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Java中BorderLayout布局管理器的兩種排列方式

    Java中BorderLayout布局管理器的兩種排列方式

    這篇文章主要介紹了Java中BorderLayout布局管理器的兩種排列方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Jenkins一鍵打包部署SpringBoot應(yīng)用

    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

最新評(píng)論

时尚| 巢湖市| 泽库县| 巴彦县| 扎囊县| 沙田区| 德化县| 文水县| 卢龙县| 柳州市| 肇东市| 永川市| 潞城市| 渑池县| 富源县| 思南县| 阆中市| 本溪市| 视频| 南汇区| 贡觉县| 固原市| 永仁县| 凭祥市| 天台县| 昌江| 梁平县| 泰安市| 神池县| 达尔| 博罗县| 论坛| 海阳市| 宁安市| 湛江市| 全椒县| 客服| 青岛市| 虹口区| 澎湖县| 沙田区|