SpringBoot整合Redis將對象寫入redis的實現(xiàn)
1、環(huán)境搭建
創(chuàng)建一個SpringBoot項目,普通的web項目就可以了,我這里使用的是start.aliyun

引入依賴:
(1)老演員了不多說。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>(2)整合redis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>(3) 實體類用到了@Data注解
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>(4)將對象轉(zhuǎn)為json存入redis,取出來時將json轉(zhuǎn)為對象
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.30</version>
</dependency>2、代碼編寫
(1)在Application啟動類的同級目錄下創(chuàng)建對應(yīng)的包

(2)寫redis工具類
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtils {
/**
* 獲取redis模板
*/
@Autowired
private StringRedisTemplate stringRedisTemplate;
/**
* 存入String類型
* @param key
* @param value
* @param timeOut
*/
public void setString(String key, String value, Long timeOut){
stringRedisTemplate.opsForValue().set(key, value);
if (timeOut != null){
//設(shè)置Redis的key的有效期
stringRedisTemplate.expire(key, timeOut, TimeUnit.SECONDS);
}
}
/**
* 獲取String類型
* @param key
* @return
*/
public String getString(String key){
return stringRedisTemplate.opsForValue().get(key);
}
}實體類:
import lombok.Data;
@Data
public class User {
private String name;
private Integer age;
}控制層:
import com.alibaba.fastjson.JSONObject;
import com.example.redis.redistudy.pojo.User;
import com.example.redis.redistudy.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RedisController {
@Autowired
private RedisUtils redisUtils;
@GetMapping("/addUser")
public String addUser(){
User user = new User();
user.setName("zhangsan");
user.setAge(18);
String userString = JSONObject.toJSONString(user);
redisUtils.setString("userString",userString, null);
return "存入成功";
}
@GetMapping("/getUser")
public User getUser(String key){
String userString= redisUtils.getString(key);
User user = JSONObject.parseObject(userString, User.class);
return user;
}
}(3)yml文件配置
spring:
redis:
host: 服務(wù)器公網(wǎng)ip
password: root //密碼
port: 6379 //端口號
database: 0 //指定存入哪一個庫3、測試
啟動程序 ,訪問地址:http://localhost:8080/addUser

看一下redis,存入成功

再獲取一下,獲取成功
地址:http://localhost:8080/getUser?key=userString

到此這篇關(guān)于SpringBoot整合Redis將對象寫入redis的實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot將對象寫入redis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實現(xiàn)網(wǎng)絡(luò)文件下載以及下載到指定目錄
在Spring框架中,StreamUtils和FileCopyUtils兩個工具類提供了方便的文件下載功能,它們都屬于org.springframework.util包,可以通過簡單的方法調(diào)用實現(xiàn)文件流的復制和下載,這些工具類支持多種參數(shù)傳遞,涵蓋了文件下載的多種場景2024-09-09
Spring MVC學習筆記之Controller查找(基于Spring4.0.3)
這篇文章主要給大家介紹了關(guān)于Spring MVC學習筆記之Controller查找(基于Spring4.0.3)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-03-03
5分鐘快速搭建SpringBoot3?+?MyBatis-Plus工程/項目的實現(xiàn)示例
本文主要介紹了使用IntelliJ?IDEA創(chuàng)建Spring?Boot工程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-01-01
完美解決MybatisPlus插件分頁查詢不起作用總是查詢?nèi)繑?shù)據(jù)問題
這篇文章主要介紹了解決MybatisPlus插件分頁查詢不起作用總是查詢?nèi)繑?shù)據(jù)問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08

