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

java org.springframework.boot 對(duì)redis操作方法

 更新時(shí)間:2025年04月21日 11:11:46   作者:shenzhipeng1023  
在Spring Boot項(xiàng)目中操作Redis,你可以使用Spring Data Redis,Spring Data Redis是Spring提供的一個(gè)用于簡(jiǎn)化Redis數(shù)據(jù)訪問(wèn)的模塊,它提供了一個(gè)易于使用的編程模型來(lái)與Redis交互,本文給大家介紹java org.springframework.boot 對(duì)redis操作方法,感興趣的朋友一起看看吧

java org.springframework.boot 對(duì)redis操作

在Spring Boot項(xiàng)目中操作Redis,你可以使用Spring Data Redis。Spring Data Redis是Spring提供的一個(gè)用于簡(jiǎn)化Redis數(shù)據(jù)訪問(wèn)的模塊,它提供了一個(gè)易于使用的編程模型來(lái)與Redis交互。

1. 添加依賴(lài)

首先,你需要在你的pom.xml文件中添加Spring Boot的Redis starter依賴(lài):

<dependencies>
    <!-- Spring Boot Redis Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <!-- 連接池管理 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
    </dependency>
</dependencies>

2. 配置Redis

application.propertiesapplication.yml中配置Redis的連接信息:

# application.properties 示例
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=yourpassword  # 如果你的Redis設(shè)置了密碼

或者使用YAML格式:

# application.yml 示例
spring:
  redis:
    host: localhost
    port: 6379
    password: yourpassword  # 如果你的Redis設(shè)置了密碼

3. 使用RedisTemplate操作Redis

RedisTemplate是Spring Data Redis中用于操作Redis的一個(gè)模板類(lèi),它提供了豐富的API來(lái)操作Redis。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
@Service
public class RedisService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate; // 用于操作String類(lèi)型的數(shù)據(jù)
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate; // 用于操作Object類(lèi)型的數(shù)據(jù),可以存儲(chǔ)任何類(lèi)型的數(shù)據(jù),需要手動(dòng)轉(zhuǎn)換類(lèi)型等操作。
    public void setKeyValue(String key, String value) {
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        opsForValue.set(key, value);
    }
    public String getKeyValue(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }
}

增刪改查操作

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class RedisService {
   @Autowired
   private RedisTemplate<String, Object> redisTemplate;
   // 存儲(chǔ)數(shù)據(jù)
   public void set(String key, Object value) {
       redisTemplate.opsForValue().set(key, value);
   }
   // 存儲(chǔ)數(shù)據(jù)并設(shè)置過(guò)期時(shí)間
   public void setWithExpire(String key, Object value, long timeout, TimeUnit unit) {
       redisTemplate.opsForValue().set(key, value, timeout, unit);
   }
   // 獲取數(shù)據(jù)
   public Object get(String key) {
       return redisTemplate.opsForValue().get(key);
   }
   // 刪除數(shù)據(jù)
   public void delete(String key) {
       redisTemplate.delete(key);
   }
}

創(chuàng)建臨時(shí)數(shù)據(jù)并自動(dòng)刪除,通過(guò)設(shè)置過(guò)期時(shí)間,可以創(chuàng)建臨時(shí)數(shù)據(jù)。當(dāng)數(shù)據(jù)過(guò)期時(shí),Redis 會(huì)自動(dòng)刪除它們。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class RedisService {
   @Autowired
   private RedisTemplate<String, Object> redisTemplate;
   // 創(chuàng)建臨時(shí)數(shù)據(jù)并設(shè)置過(guò)期時(shí)間
   public void createTemporaryData(String key, Object value, long timeout, TimeUnit unit) {
       redisTemplate.opsForValue().set(key, value, timeout, unit);
   }
}

例如

redisTemplate.opsForValue().set("exampleKey", "exampleValue", 10, TimeUnit.SECONDS);

在這個(gè)示例中,“exampleKey"將被設(shè)置為"exampleValue”,并且這個(gè)鍵值對(duì)將在10秒后過(guò)期。

4. 使用Jackson2JsonRedisSerializer(可選)自定義序列化方式(存儲(chǔ)對(duì)象)

如果你需要存儲(chǔ)Java對(duì)象而不是簡(jiǎn)單的字符串,你可以使用Jackson2JsonRedisSerializer來(lái)序列化和反序列化對(duì)象。

import com.fasterxml.jackson.databind.ObjectMapper;
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.StringRedisSerializer;
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(com.fasterxml.jackson.annotation.PropertyAccessor.ALL, com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY);  // 所有字段全部可見(jiàn),包括私有字段等。根據(jù)實(shí)際情況調(diào)整。
        serializer.setObjectMapper(om);  // 設(shè)置序列化工具類(lèi)。也可以自定義序列化工具類(lèi)。例如:new CustomObjectMapper()。 具體根據(jù)需要選擇合適的序列化工具類(lèi)。例如:new CustomObjectMapper()。具體根據(jù)需要選擇合適的序列化工具類(lèi)。例如:new CustomObjectMapper()。具體根據(jù)需要選擇合適的序列化工具類(lèi)。例如:new CustomObjectMapper()。具體根據(jù)需要選擇合適的序列化工具類(lèi)。例如:new CustomObjectMapper()。具體根據(jù)需要選擇合適的序列化工具類(lèi)。例如:new CustomObjectMapper()。具體根據(jù)需要選擇合適的序列化工具類(lèi)。例如:new CustomObjectMapper()。具體根據(jù)需要選擇合適的序列化工具類(lèi)

到此這篇關(guān)于java org.springframework.boot 對(duì)redis操作方法的文章就介紹到這了,更多相關(guān)java org.springframework.boot redis操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java?JVM方法分派模型靜態(tài)分派動(dòng)態(tài)分派全面講解

    java?JVM方法分派模型靜態(tài)分派動(dòng)態(tài)分派全面講解

    這篇文章主要為大家介紹了java?JVM方法分派模型靜態(tài)分派動(dòng)態(tài)分派全面講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • java虛擬機(jī)運(yùn)行時(shí)數(shù)據(jù)區(qū)分析

    java虛擬機(jī)運(yùn)行時(shí)數(shù)據(jù)區(qū)分析

    這篇文章主要介紹了java虛擬機(jī)運(yùn)行時(shí)數(shù)據(jù)區(qū)分析,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • maven打包加入本地jar包的實(shí)現(xiàn)

    maven打包加入本地jar包的實(shí)現(xiàn)

    在使用maven打包的過(guò)程中,有時(shí)候我們需要添加一些本地的jar包,本文主要介紹了maven打包加入本地jar包的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-08-08
  • Spring 整合 MyBatis的實(shí)現(xiàn)步驟

    Spring 整合 MyBatis的實(shí)現(xiàn)步驟

    SpringMVC 本來(lái)就是 Spring 框架的一部分,這兩者無(wú)須再做整合,所以 SSM 整合的關(guān)鍵就是Spring對(duì)MyBatis的整合,三大框架整合完成后,將以 Spring 為核心,調(diào)用有關(guān)資源,高效運(yùn)作,這篇文章主要介紹了 Spring 整合 MyBatis的實(shí)現(xiàn)步驟,需要的朋友可以參考下
    2023-02-02
  • Java二維數(shù)組實(shí)現(xiàn)數(shù)字拼圖效果

    Java二維數(shù)組實(shí)現(xiàn)數(shù)字拼圖效果

    這篇文章主要為大家詳細(xì)介紹了Java二維數(shù)組實(shí)現(xiàn)數(shù)字拼圖效果,控制臺(tái)可以對(duì)空格進(jìn)行移動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • 如何通過(guò)java將doc文件轉(zhuǎn)換為docx文件詳解

    如何通過(guò)java將doc文件轉(zhuǎn)換為docx文件詳解

    在數(shù)字化時(shí)代文檔處理成為了我們?nèi)粘9ぷ骱蛯W(xué)習(xí)中不可或缺的一部分,其中doc和docx作為兩種常見(jiàn)的文檔格式,各自具有不同的特點(diǎn)和優(yōu)勢(shì),這篇文章主要給大家介紹了關(guān)于如何通過(guò)java將doc文件轉(zhuǎn)換為docx文件的相關(guān)資料,需要的朋友可以參考下
    2024-07-07
  • Java初學(xué)者入門(mén)之繼承和多態(tài)

    Java初學(xué)者入門(mén)之繼承和多態(tài)

    Java 面向?qū)ο缶幊逃腥筇匦裕悍庋b、繼承、多態(tài),學(xué)好繼承和多態(tài)是面向?qū)ο箝_(kāi)發(fā)語(yǔ)言中非常重要的一個(gè)環(huán)節(jié),這篇文章主要給大家介紹了關(guān)于Java初學(xué)者入門(mén)之繼承和多態(tài)的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • Java LinkedList的實(shí)現(xiàn)原理圖文詳解

    Java LinkedList的實(shí)現(xiàn)原理圖文詳解

    今天小編就為大家分享一篇關(guān)于Java LinkedList的實(shí)現(xiàn)原理圖文詳解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • java內(nèi)存溢出示例(堆溢出、棧溢出)

    java內(nèi)存溢出示例(堆溢出、棧溢出)

    這篇文章主要介紹了java內(nèi)存溢出示例(堆溢出、棧溢出),需要的朋友可以參考下
    2014-04-04
  • SpringBoot整合Sharding-JDBC實(shí)現(xiàn)MySQL8讀寫(xiě)分離

    SpringBoot整合Sharding-JDBC實(shí)現(xiàn)MySQL8讀寫(xiě)分離

    本文是一個(gè)基于SpringBoot整合Sharding-JDBC實(shí)現(xiàn)讀寫(xiě)分離的極簡(jiǎn)教程,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的可以了解一下
    2021-07-07

最新評(píng)論