Springboot整合Redis最简单例子分享
作者:smile-yan 时间:2021-12-18 17:53:48
1. 编写目的
最简单的例子,Springboot整合Redis。
2. 详细过程
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
编写一个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";
}
}
测试
运行项目,然后用浏览器打开localhost:8080/这个地址。
看到浏览器输出SUCCESS后,使用Redis Desktop查看是否set成功。
3. 总结
非常简单的例子,但还是记录一下,在这个基础上可以做很多事情,但是对于刚刚接触的人来说,可能还是有用的。
来源:https://blog.csdn.net/smileyan9/article/details/89135701
标签:Springboot,Redis
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java synchronized锁升级jol过程详解
2023-04-15 04:58:51
![](https://img.aspxhome.com/file/2023/4/61864_0s.jpg)
java不可逆加密算法之md5加密算法使用示例
2023-07-25 09:43:36
Spring Boot应用的极速部署脚本示例代码
2021-07-06 21:22:28
c#关于JWT跨域身份验证的实现代码
2023-09-12 18:26:02
![](https://img.aspxhome.com/file/2023/7/108927_0s.png)
详解三种C#实现数组反转方式
2023-10-09 20:29:23
spring boot多数据源动态切换代码实例
2022-03-11 00:37:09
![](https://img.aspxhome.com/file/2023/7/71867_0s.png)
java、android可用的rtp封包解包h264案例
2021-11-27 01:53:18
spring bean.xml文件p标签使用报错的解决
2022-03-12 23:16:16
图文详解Java中的字节输入与输出流
2022-12-23 23:14:48
![](https://img.aspxhome.com/file/2023/4/81884_0s.jpg)
Java笛卡尔积算法原理与实现方法详解
2023-11-18 22:08:17
![](https://img.aspxhome.com/file/2023/1/101981_0s.jpg)
C++中的数组详情
2022-02-23 19:06:11
![](https://img.aspxhome.com/file/2023/6/124036_0s.jpg)
java模拟TCP通信实现客户端上传文件到服务器端
2023-11-26 10:14:49
Android实现ListView异步加载的方法(改进版)
2023-11-24 12:08:49
提权函数之RtlAdjustPrivilege()使用说明
2021-08-27 04:16:49
Java EE项目中的异常处理总结(一篇不得不看的文章)
2023-06-16 23:35:26
Mybatis对mapper的加载流程深入讲解
2022-06-01 12:33:04
![](https://img.aspxhome.com/file/2023/7/62207_0s.png)
java多线程详细总结
2023-01-10 14:16:04
C# 实现dataGridView选中一行右键出现菜单的示例代码
2022-05-12 05:15:55
![](https://img.aspxhome.com/file/2023/9/94539_0s.png)
浅谈基于SpringBoot实现一个简单的权限控制注解
2022-01-23 16:42:47
java编程题之顺时针打印矩阵
2022-02-22 04:05:36