SpringBoot整合Redis将对象写入redis的实现
作者:明明就很安静 时间:2023-07-30 14:39:07
1、环境搭建
创建一个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)将对象转为json存入redis,取出来时将json转为对象
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.30</version>
</dependency>
2、代码编写
(1)在Application启动类的同级目录下创建对应的包
(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){
//设置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: 服务器公网ip
password: root //密码
port: 6379 //端口号
database: 0 //指定存入哪一个库
3、测试
启动程序 ,访问地址:http://localhost:8080/addUser
看一下redis,存入成功
再获取一下,获取成功
地址:http://localhost:8080/getUser?key=userString
来源:https://blog.csdn.net/qq_42251944/article/details/124586950
标签:SpringBoot,对象写入,redis
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C# 对象持久化详解
2023-06-24 10:21:47
![](https://img.aspxhome.com/file/2023/7/83357_0s.png)
java实现学籍管理系统
2023-04-03 00:32:19
WPF实现窗体中的悬浮按钮
2022-07-25 17:45:53
![](https://img.aspxhome.com/file/2023/2/94652_0s.jpg)
Spring 中 PageHelper 不生效问题及解决方法
2021-11-25 11:25:04
![](https://img.aspxhome.com/file/2023/6/101986_0s.jpg)
举例解析Java的图像缓冲技术的使用
2022-09-08 17:17:17
Java C++题解leetcode886可能的二分法并查集染色法
2023-08-29 01:12:52
![](https://img.aspxhome.com/file/2023/1/128931_0s.png)
spring aop action中验证用户登录状态的实例代码
2021-12-04 19:47:01
浅析Android 模拟键盘鼠标事件
2022-12-19 00:11:14
Android App如何防止抓包
2023-01-03 05:20:13
解决RestTemplate 请求接收自定义400+ 或500+错误
2023-12-12 00:25:20
![](https://img.aspxhome.com/file/2023/6/85426_0s.png)
Logback日志基础及自定义配置代码实例
2022-09-04 01:01:41
springboot自定义Starter过程解析
2023-07-24 22:24:55
C#实现异步编程的方法
2022-03-01 22:43:27
![](https://img.aspxhome.com/file/2023/9/108669_0s.jpg)
Java Mybatis框架多表操作与注解开发详解分析
2023-12-04 17:15:14
![](https://img.aspxhome.com/file/2023/6/88746_0s.png)
C#创建windows系统用户的方法
2022-07-16 20:30:12
IntelliJ IDEA 2020.1.2激活工具下载及破解方法免费可用至2089年(强烈推荐)
2023-07-29 09:22:11
![](https://img.aspxhome.com/file/2023/1/92701_0s.jpg)
Android性能优化以及数据优化方法
2021-09-02 03:46:18
![](https://img.aspxhome.com/file/2023/6/117166_0s.png)
解析Android开发优化之:对Bitmap的内存优化详解
2023-08-31 21:05:40
Android自定义TextView实现drawableLeft内容居中
2022-04-24 18:20:14
![](https://img.aspxhome.com/file/2023/2/122092_0s.png)
Springboot2.0处理自定义异常并返回json
2021-06-01 03:42:24
![](https://img.aspxhome.com/file/2023/0/62140_0s.png)