Springboot整合Redis最簡(jiǎn)單例子分享
1. 編寫目的
最簡(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: 6379host: 39.106.198.74### 如果有密碼需要添加password
編寫一個(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;@RestControllerpublic 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è)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. JavaEE SpringMyBatis是什么? 它和Hibernate的區(qū)別及如何配置MyBatis2. Python 忽略文件名編碼的方法3. python 基于Appium控制多設(shè)備并行執(zhí)行4. android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器(無(wú)bug)5. Java Media Framework 基礎(chǔ)教程6. 解決vue頁(yè)面刷新,數(shù)據(jù)丟失的問(wèn)題7. python 實(shí)現(xiàn)圍棋游戲(純tkinter gui)8. python 讀txt文件,按‘,’分割每行數(shù)據(jù)操作9. 在Mac中配置Python虛擬環(huán)境過(guò)程解析10. Python趣味挑戰(zhàn)之用pygame實(shí)現(xiàn)簡(jiǎn)單的金幣旋轉(zhuǎn)效果
