Springboot以Repository方式整合Redis的方法
1 簡介
Redis是高性能的NoSQL數(shù)據(jù)庫,經(jīng)常作為緩存流行于各大互聯(lián)網(wǎng)架構(gòu)中。本文將介紹如何在Springboot中整合Spring Data Redis,使用Repository的方式操作。
代碼結(jié)構(gòu)如下:

2 整合過程
2.1 安裝Redis數(shù)據(jù)庫
為了節(jié)省時間,就直接通過Docker來安裝了,可以參考文章:Docker安裝Redis并介紹漂亮的可視化客戶端進(jìn)行操作,可以快速安裝并使用客戶端進(jìn)行查看和操作。
2.2 引入相關(guān)依賴
我們引入Springboot Web的依賴,以啟動REST服務(wù)。還需要引入Spring Data Redis相關(guān)的依賴。最后,還需要commons-pool2,不然會因為缺少類而無法啟動。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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>
2.3 配置連接信息
配置Redis的連接信息,這個信息跟你安裝時的配置有關(guān),同時配置了連接池,各項的配置及相關(guān)解釋如下:
# Redis數(shù)據(jù)庫索引,默認(rèn)為0 spring.redis.database=0 # Redis端口 spring.redis.port=6379 # Redis服務(wù)器主機(jī) spring.redis.host=localhost # 連接池最大連接數(shù) spring.redis.lettuce.pool.max-active=8 # 連接池最大空閑 spring.redis.lettuce.pool.max-idle=8 # 連接池最小空閑 spring.redis.lettuce.pool.min-idle=2 # 連接池最大阻塞等待時間 spring.redis.lettuce.pool.max-wait=1ms # 超時時間 spring.redis.lettuce.shutdown-timeout=100ms
2.4 創(chuàng)建實(shí)體類
存入Redis中的數(shù)據(jù)類型,可以是自定義的一個類,注意需要加上注解@RedisHash和@Id。存入Redis的數(shù)據(jù)為Set類型。
具體代碼如下:
package com.pkslow.redis.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;
import java.util.Date;
@RedisHash("User")
public class User {
@Id
private String userId;
private String name;
private Integer age;
private Date createTime = new Date();
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
2.5 數(shù)據(jù)庫訪問層UserRepository接口
直接繼承CrudRepository接口就行了,不用自己來實(shí)現(xiàn),需要注意CrudRepository<User, String>的泛型類型:
package com.pkslow.redis.dal;
import com.pkslow.redis.model.User;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<User, String> {
}
2.6 實(shí)現(xiàn)Controller
Controller實(shí)現(xiàn)了RESTful風(fēng)格的增刪改查功能,只要把UserRepository注入便可以使用它來操作:
package com.pkslow.redis.controller;
import com.pkslow.redis.dal.UserRepository;
import com.pkslow.redis.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private final UserRepository userRepository;
public UserController(UserRepository userRepository) {
this.userRepository = userRepository;
}
@GetMapping("")
public Iterable<User> getAllUsers() {
return userRepository.findAll();
}
@GetMapping("/{userId}")
public User getByUserId(@PathVariable String userId) {
return userRepository.findById(userId).orElse(new User());
}
@PostMapping("")
public User addNewUser(@RequestBody User user) {
return userRepository.save(user);
}
@DeleteMapping("/{userId}")
public String delete(@PathVariable String userId) {
User user = new User();
user.setUserId(userId);
userRepository.deleteById(userId);
return "deleted: " + userId;
}
@PutMapping("")
public User update(@RequestBody User user) {
return userRepository.save(user);
}
}
3 Postman接口測試
本文使用Postman進(jìn)行測試,結(jié)果顯示的時間為GMT時間,每個功能測試如下:
(1)新增User

(2)根據(jù)UserId查詢特定User

(3)修改User

(4)刪除一個User

(5)查詢所有User

在Redis中的數(shù)據(jù)如下所示:

4 總結(jié)
本文通過實(shí)例講解了如何整合Springboot和Redis,使用的是Repository的方式。
到此這篇關(guān)于Springboot以Repository方式整合Redis的文章就介紹到這了,更多相關(guān)Springboot以Repository方式整合Redis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?RestTemplate遠(yuǎn)程調(diào)用過程
這篇文章主要介紹了Spring?RestTemplate遠(yuǎn)程調(diào)用過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
解決springboot項目啟動報錯Field xxxMapper in com...xx
這篇文章主要介紹了解決springboot項目啟動報錯Field xxxMapper in com...xxxContr問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
詳解lombok @Getter @Setter 使用注意事項
這篇文章主要介紹了詳解lombok @Getter @Setter 使用注意事項,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
java構(gòu)造器 默認(rèn)構(gòu)造方法及參數(shù)化構(gòu)造方法
構(gòu)造器也叫構(gòu)造方法、構(gòu)造函數(shù),是一種特殊類型的方法,負(fù)責(zé)類中成員變量(域)的初始化。構(gòu)造器的用處是在創(chuàng)建對象時執(zhí)行初始化,當(dāng)創(chuàng)建一個對象時,系統(tǒng)會為這個對象的實(shí)例進(jìn)行默認(rèn)的初始化,下面文章將進(jìn)入講解,需要的朋友可以參考下2021-10-10
java虛擬機(jī)jvm方法區(qū)實(shí)例講解
在本篇文章里小編給大家整理的是一篇關(guān)于java虛擬機(jī)jvm方法區(qū)實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-02-02
SpringBoot消息國際化配置實(shí)現(xiàn)過程解析
這篇文章主要介紹了SpringBoot消息國際化配置實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
java隨機(jī)驗證碼生成實(shí)現(xiàn)實(shí)例代碼
這篇文章主要介紹了java隨機(jī)驗證碼生成實(shí)現(xiàn)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05

