欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot中RedisTemplate的使用示例詳解

 更新時(shí)間:2023年10月24日 15:22:24   作者:y_bccl27  
RedisTemplate.opsForHash()是RedisTemplate類提供的用于操作Hash類型的方法,它可以用于對(duì)Redis中的Hash數(shù)據(jù)結(jié)構(gòu)進(jìn)行各種操作,如設(shè)置字段值、獲取字段值、刪除字段值等,本文介紹Spring Boot中RedisTemplate的使用,感興趣的朋友一起看看吧

當(dāng)前Spring Boot的版本為2.7.6,在使用RedisTemplate之前我們需要在pom.xml中引入下述依賴:

<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類提供的用于操作Hash類型的方法,它可以用于對(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", "蘋果");
    }
}

在上述代碼能正常運(yùn)行的情況下,我們?cè)诮K端中執(zhí)行 redis-cli 命令進(jìn)入到redis的控制臺(tái)中,然后執(zhí)行 keys * 命令查看所有的key,結(jié)果發(fā)現(xiàn)存儲(chǔ)在redis中的key不是設(shè)置的string值,前面還多出了許多類似 \xac\xed\x00\x05t\x00 這種字符串,如下圖所示:

這是因?yàn)镾pring-Data-Redis的RedisTemplate<K, V>模板類在操作redis時(shí)默認(rèn)使用JdkSerializationRedisSerializer來進(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;
    }
}

需要說明的是這種配置只是針對(duì)所有的數(shù)據(jù)都是String類型,如果是其它類型,則根據(jù)需求修改一下序列化方式。 

使用 flushdb 命令清除完所有的數(shù)據(jù)以后,再次執(zhí)行上述測(cè)試案例,接著我們?cè)俅稳ゲ榭此械膋ey,這個(gè)看到數(shù)據(jù)已經(jīng)正常:

接著使用 hget fruit:list 1 命令去查詢剛剛存儲(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","蘋果");
        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ù)量,如果字段不存在的話,則返回的是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 將指定字段的值增加指定步長

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è)置為指定步長,并且返回該字段當(dāng)前的值;如果字段存在,則在該字段原有值的基礎(chǔ)上增加指定步長,返回該字段當(dāng)前的最新值。 該方法只適用于字段值為int類型的數(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(即字符串),使用語法上面完全一致。 

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類提供的用于操作字符串值類型的方法。它可以用于對(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)文章

  • Java Lambda表達(dá)式超詳細(xì)介紹

    Java Lambda表達(dá)式超詳細(xì)介紹

    這篇文章主要介紹了Java Lambda表達(dá)式,Lambda表達(dá)式是Java SE 8中一個(gè)重要的新特性, Lambda 表達(dá)式(Lambda expression)可以看作是一個(gè)匿名函數(shù),基于數(shù)學(xué)中的λ演算得名,也可稱為閉包(Closure),下面來看看文章具體的詳細(xì)介紹吧
    2022-02-02
  • Java并發(fā) 結(jié)合源碼分析AQS原理

    Java并發(fā) 結(jié)合源碼分析AQS原理

    這篇文章主要介紹了Java并發(fā) 結(jié)合源碼分析AQS原理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • Spring Security系列教程之會(huì)話管理處理會(huì)話過期問題

    Spring Security系列教程之會(huì)話管理處理會(huì)話過期問題

    會(huì)話過期,是指當(dāng)用戶登錄網(wǎng)站后,較長一段時(shí)間沒有與服務(wù)器進(jìn)行交互,將會(huì)導(dǎo)致服務(wù)器上的用戶會(huì)話數(shù)據(jù)(即session)被銷毀。這篇文章主要介紹了Spring Security系列教程之會(huì)話管理處理會(huì)話過期問題,需要的朋友可以參考下
    2021-10-10
  • 詳解基于Spring Cloud幾行配置完成單點(diǎn)登錄開發(fā)

    詳解基于Spring Cloud幾行配置完成單點(diǎn)登錄開發(fā)

    這篇文章主要介紹了詳解基于Spring Cloud幾行配置完成單點(diǎn)登錄開發(fā),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-02-02
  • java poi導(dǎo)出excel時(shí)如何設(shè)置手動(dòng)換行

    java poi導(dǎo)出excel時(shí)如何設(shè)置手動(dòng)換行

    這篇文章主要介紹了java poi導(dǎo)出excel時(shí)如何設(shè)置手動(dòng)換行,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 詳解Spring MVC CORS 跨域

    詳解Spring MVC CORS 跨域

    本篇文章主要介紹了詳解Spring MVC CORS 跨域 ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • 解決springboot運(yùn)行出現(xiàn)錯(cuò)誤:找不到或無法加載主類com.xxxx.xxxx.Application問題

    解決springboot運(yùn)行出現(xiàn)錯(cuò)誤:找不到或無法加載主類com.xxxx.xxxx.Application問題

    文章介紹了在服務(wù)器上運(yùn)行一個(gè)未使用的Java項(xiàng)目時(shí)遇到的“找不到或無法加載主類”錯(cuò)誤,并提供了兩種解決方法:通過Maven install或build …、Goals輸入install并跳過測(cè)試來重新構(gòu)建項(xiàng)目
    2024-11-11
  • linux系統(tǒng)下java項(xiàng)目在后臺(tái)啟動(dòng)的4種方式總結(jié)

    linux系統(tǒng)下java項(xiàng)目在后臺(tái)啟動(dòng)的4種方式總結(jié)

    Linux是集多種功能于一身的操作系統(tǒng),它可以讓用戶查看和管理當(dāng)下正在運(yùn)行的進(jìn)程,包括Java程序,這篇文章主要給大家總結(jié)介紹了關(guān)于linux系統(tǒng)下java項(xiàng)目在后臺(tái)啟動(dòng)的4種方式,需要的朋友可以參考下
    2023-10-10
  • SpringBoot中application.properties、application.yaml、application.yml區(qū)別

    SpringBoot中application.properties、application.yaml、applicati

    本文主要介紹了SpringBoot中application.properties、application.yaml、application.yml區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-04-04
  • java使用lambda表達(dá)式對(duì)List集合進(jìn)行操作技巧(JDK1.8)

    java使用lambda表達(dá)式對(duì)List集合進(jìn)行操作技巧(JDK1.8)

    這篇文章主要介紹了java使用lambda表達(dá)式對(duì)List集合進(jìn)行操作技巧適用jdk1.8,感興趣的朋友跟著小編一起看看實(shí)現(xiàn)代碼吧
    2018-06-06

最新評(píng)論