Springboot整合Redis最簡(jiǎn)單例子分享
1. 編寫(xiě)目的
最簡(jiǎn)單的例子,Springboot整合Redis。
2. 詳細(xì)過(guò)程
pom 文件添加依賴
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置redis
編輯application.yml文件。
spring:
redis:
port: 6379
host: 39.106.198.74
### 如果有密碼需要添加password
編寫(xiě)一個(gè)controller類
package cn.smileyan.shirodemo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RedisController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@RequestMapping("/")
private String hello() {
stringRedisTemplate.opsForValue().set("hello","world");
return "SUCCESS";
}
}
測(cè)試
運(yùn)行項(xiàng)目,然后用瀏覽器打開(kāi)localhost:8080/這個(gè)地址。
看到瀏覽器輸出SUCCESS后,使用Redis Desktop查看是否set成功。

3. 總結(jié)
非常簡(jiǎn)單的例子,但還是記錄一下,在這個(gè)基礎(chǔ)上可以做很多事情,但是對(duì)于剛剛接觸的人來(lái)說(shuō),可能還是有用的。
以上這篇Springboot整合Redis最簡(jiǎn)單例子分享就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決springmvc項(xiàng)目中使用過(guò)濾器來(lái)解決請(qǐng)求方式為post時(shí)出現(xiàn)亂碼的問(wèn)題
這篇文章主要介紹了springmvc項(xiàng)目中使用過(guò)濾器來(lái)解決請(qǐng)求方式為post時(shí)出現(xiàn)亂碼的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
java多線程并發(fā)executorservice(任務(wù)調(diào)度)類
這篇文章主要介紹了線程并發(fā)ScheduledExecutorService類,設(shè)置 ScheduledExecutorService ,2秒后,在 1 分鐘內(nèi)每 10 秒鐘蜂鳴一次2014-01-01
對(duì)象轉(zhuǎn)Json字符串時(shí)如何忽略指定屬性
這篇文章主要介紹了對(duì)象轉(zhuǎn)Json字符串時(shí)如何忽略指定屬性,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Spring項(xiàng)目中swagger用法與swagger-ui使用
這篇文章主要介紹了Spring項(xiàng)目中swagger用法與swagger-ui使用,通過(guò)圖文并茂的形式給大家介紹了編寫(xiě)springboot項(xiàng)目的方法及導(dǎo)入spring-fox依賴的代碼詳解,需要的朋友可以參考下2021-05-05
史上最佳springboot Locale 國(guó)際化方案
今天給大家分享史上最佳springboot Locale 國(guó)際化方案,本文通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-08-08

