Springboot如何实现Redis最基础整合示例?

2026-05-16 03:551阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计361个文字,预计阅读时间需要2分钟。

Springboot如何实现Redis最基础整合示例?

1. 编写项目的最简单示例,集成Spring Boot和Redis。

2.详细步骤:在pom.xml文件中添加依赖。`

org.springframework.boot spring-boot-starter-data-redis`

1. 编写目的

最简单的例子,Springboot整合Redis。

Springboot如何实现Redis最基础整合示例?

2. 详细过程


pom 文件添加依赖

<!-- 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. 总结

非常简单的例子,但还是记录一下,在这个基础上可以做很多事情,但是对于刚刚接触的人来说,可能还是有用的。

以上这篇Springboot整合Redis最简单例子分享就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

本文共计361个文字,预计阅读时间需要2分钟。

Springboot如何实现Redis最基础整合示例?

1. 编写项目的最简单示例,集成Spring Boot和Redis。

2.详细步骤:在pom.xml文件中添加依赖。`

org.springframework.boot spring-boot-starter-data-redis`

1. 编写目的

最简单的例子,Springboot整合Redis。

Springboot如何实现Redis最基础整合示例?

2. 详细过程


pom 文件添加依赖

<!-- 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. 总结

非常简单的例子,但还是记录一下,在这个基础上可以做很多事情,但是对于刚刚接触的人来说,可能还是有用的。

以上这篇Springboot整合Redis最简单例子分享就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。