使用SpringBoot中整合Redis
SpringBoot中整合Redis
本次,我們以IDEA + SpringBoot作為 Java中整合Redis的使用 的測試環(huán)境,如果對創(chuàng)建SpringBoot項目有不清楚的地方,可以參考這篇文章:使用Idea創(chuàng)建我的第一個SpringBoot項目
首先,我們需要導(dǎo)入Redis的maven依賴
<!-- Redis的maven依賴包 --> ?? ??? ?<dependency> ?? ??? ??? ?<groupId>org.springframework.boot</groupId> ?? ??? ??? ?<artifactId>spring-boot-starter-data-redis</artifactId> ?? ??? ?</dependency>
其次,我們需要在配置文件中配置你的Redis配置信息,我使用的是 .yml文件格式
# redis配置 spring: redis: # r服務(wù)器地址 host: 127.0.0.1 # 服務(wù)器端口 port: 6379 # 數(shù)據(jù)庫索引(默認(rèn)0) database: 0 jedis: pool: # 連接池最大連接數(shù)(使用負(fù)值表示沒有限制) max-active: 50 # 連接池最大阻塞等待時間(使用負(fù)值表示沒有限制) max-wait: 3000ms # 連接池中的最大空閑連接數(shù) max-idle: 20 # 連接池中的最小空閑連接數(shù) min-idle: 2 # 連接超時時間(毫秒) timeout: 5000ms
然后,我們需要創(chuàng)建一個RedisUtil來對Redis數(shù)據(jù)庫進(jìn)行操作
package com.zyxx.test.utils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; /** * @ClassName RedisUtil * @Author Lizhou * @Date 2019-08-03 17:29:29 * @Version 1.0 **/ @Component public class RedisUtil { @Autowired private RedisTemplate<String, String> template; /** * 讀取數(shù)據(jù) * * @param key * @return */ public String get(final String key) { return template.opsForValue().get(key); } /** * 寫入數(shù)據(jù) */ public boolean set(final String key, String value) { boolean res = false; try { template.opsForValue().set(key, value); res = true; } catch (Exception e) { e.printStackTrace(); } return res; } /** * 根據(jù)key更新數(shù)據(jù) */ public boolean update(final String key, String value) { boolean res = false; try { template.opsForValue().getAndSet(key, value); res = true; } catch (Exception e) { e.printStackTrace(); } return res; } /** * 根據(jù)key刪除數(shù)據(jù) */ public boolean del(final String key) { boolean res = false; try { template.delete(key); res = true; } catch (Exception e) { e.printStackTrace(); } return res; } /** * 是否存在key */ public boolean hasKey(final String key) { boolean res = false; try { res = template.hasKey(key); } catch (Exception e) { e.printStackTrace(); } return res; } /** * 給指定的key設(shè)置存活時間 * 默認(rèn)為-1,表示永久不失效 */ public boolean setExpire(final String key, long seconds) { boolean res = false; try { if (0 < seconds) { res = template.expire(key, seconds, TimeUnit.SECONDS); } } catch (Exception e) { e.printStackTrace(); } return res; } /** * 獲取指定key的剩余存活時間 * 默認(rèn)為-1,表示永久不失效,-2表示該key不存在 */ public long getExpire(final String key) { long res = 0; try { res = template.getExpire(key, TimeUnit.SECONDS); } catch (Exception e) { e.printStackTrace(); } return res; } /** * 移除指定key的有效時間 * 當(dāng)key的有效時間為-1即永久不失效和當(dāng)key不存在時返回false,否則返回true */ public boolean persist(final String key) { boolean res = false; try { res = template.persist(key); } catch (Exception e) { e.printStackTrace(); } return res; } }
最后,我們可以使用單元測試來檢測我們在RedisUtil中寫的操作Redis數(shù)據(jù)庫的方法
package com.zyxx.test; import com.zyxx.test.utils.RedisUtil; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; @RunWith(SpringRunner.class) @SpringBootTest public class TestApplicationTest { @Resource private RedisUtil redisUtil; @Test public void setRedis() { boolean res = redisUtil.set("jay", "周杰倫 - 《以父之名》"); System.out.println(res); } @Test public void getRedis() { String res = redisUtil.get("jay"); System.out.println(res); } @Test public void updateRedis() { boolean res = redisUtil.update("jay", "周杰倫 - 《夜的第七章》"); System.out.println(res); } @Test public void delRedis() { boolean res = redisUtil.del("jay"); System.out.println(res); } @Test public void hasKey() { boolean res = redisUtil.hasKey("jay"); System.out.println(res); } @Test public void expire() { boolean res = redisUtil.setExpire("jay", 100); System.out.println(res); } @Test public void getExpire() { long res = redisUtil.getExpire("jay"); System.out.println(res); } @Test public void persist() { boolean res = redisUtil.persist("jay"); System.out.println(res); } }
推薦使用Redis客戶端(redis-desktop-manager)來查看Redis數(shù)據(jù)庫中的數(shù)據(jù)
至此,我們在日常項目中整合Redis的基本使用操作就完成了,但在實際項目中,可能會涉及到更復(fù)雜的用法,可以根據(jù)你的業(yè)務(wù)需求調(diào)整Redis的使用即可。
SpringBoot整合Redis改不了database問題
關(guān)于學(xué)習(xí)redis寫的yaml文件,里面的
redis: ? database: 15 ? host: 127.0.0.1 ? port: 6379 ? password: ? timeout: 3000ms # 連接超時時間(毫秒)
但是springboot自動裝配一直是database為0
最后發(fā)現(xiàn)是沒有找到我配置的redis,默認(rèn)是用的自動裝配的默認(rèn)地址(所以我一直以為我的yaml寫對了,但是沒用寫對,因為一直沒有取到我的值)
自動裝配的就是
database=0,port=6379;無密碼,host=localhost,
就是因為這個原因,我寫的數(shù)據(jù)能進(jìn)入我的本地redis中,所以我一直沒找到問題,因為我也使用的本地的redis測試的,一直找不到為什么改不了database。
最后發(fā)現(xiàn)是我的yaml文件的redis寫錯了,把redis寫到log下面,并且歸屬于log。
最后該成歸屬于spring下面就可以了。
注意:一定要注意yaml的書寫規(guī)范和上下級的關(guān)系。
深刻的教訓(xùn),我在網(wǎng)上查了很多,也沒找到對應(yīng)的辦法,幸好沒有,不然的話,我的代碼會被改的亂七八糟的,花了一下午才發(fā)現(xiàn)是自己yaml寫錯了。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis配置之properties和settings標(biāo)簽的用法
這篇文章主要介紹了Mybatis配置之properties和settings標(biāo)簽的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07詳解Java8新特性之interface中的static方法和default方法
這篇文章主要介紹了Java8新特性之interface中的static方法和default方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08SpringBoot與SpringMVC中參數(shù)傳遞的原理解析
這篇文章主要介紹了SpringBoot與SpringMVC中參數(shù)傳遞的原理,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07IDEA 2021版新建Maven、TomCat工程的詳細(xì)教程
這篇文章主要介紹了IDEA 2021版新建Maven、TomCat工程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04Java并發(fā)編程ThreadLocalRandom類詳解
這篇文章主要介紹了Java并發(fā)編程ThreadLocalRandom類詳解,通過提出問題為什么需要ThreadLocalRandom展開詳情,感興趣的朋友可以參考一下2022-06-06SpringBoot事件發(fā)布與監(jiān)聽超詳細(xì)講解
今天去官網(wǎng)查看spring boot資料時,在特性中看見了系統(tǒng)的事件及監(jiān)聽章節(jié),所以下面這篇文章主要給大家介紹了關(guān)于SpringBoot事件發(fā)布和監(jiān)聽的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11SpringBoot中使用Jsoup爬取網(wǎng)站數(shù)據(jù)的方法
這篇文章主要介紹了SpringBoot中使用Jsoup爬取網(wǎng)站數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06