一、先创建springboot项目,然后添加相关POM依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.tedu</groupId>
<artifactId>redisdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>redisdemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--集成redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--lombok,来自动生成get,set等方法-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二、配置连接redis的application.yml文件?
spring:
redis:
host: 127.0.0.1
port: 6379
server:
port: 8083
三、启动redis并连接?
3.1 进入到安装redis的目录下
3.2 使用命令启动并去连接?
四、测试连接:做一个基本的连接测试?
package cn.tedu.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Classname RedisController
* @Description TODO
* @Date 2022/6/24 11:57
* @Author 朱文龙
*/
@RestController
@RequestMapping("/redis")
public class RedisController {
@Autowired
private StringRedisTemplate template;
@GetMapping("/hello")
public String hello(String name,String age){
template.opsForValue().set(name,age);
String value = template.opsForValue().get(name);
System.out.println(name+","+value);
return name+","+value;
}
}
在浏览器中访问该项目地址,运行效果如下图,说明redis已连接成功,并且获取数据也成功了!http://localhost:8083/redis/hello?name=%E5%B0%8F%E6%9D%8E%E5%A4%B4&age=23?
五、通过对象的方式来进行数据的保存与读取。在项目中新增一个User类
package cn.tedu.pojo;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* @Classname User
* @Description TODO
* @Date 2022/6/24 14:53
* @Author 朱文龙
*/
@Data
@Accessors(chain = true)
public class User {
private String name;
private Integer age;
private String Gender;
}
测试代码如下图:
@GetMapping("/user")
public String getUser(){
List<User> userList = new ArrayList<>();
userList.add(new User().setName("小李头").setAge(23).setGender("女"));
userList.add(new User().setName("小白").setAge(20).setGender("男"));
userList.add(new User().setName("张三").setAge(30).setGender("男"));
userList.add(new User().setName("小法师").setAge(15).setGender("女"));
template.opsForValue().set("user", String.valueOf(userList));
System.out.println(template.opsForValue().get("user"));
return template.opsForValue().get("user");
}
通过访问地址 http://localhost:8083/redis/user?,最后运行效果如下图 :
?
?六、以上内容,本人也是在学习整理,如果有错误或语法不当之处,欢迎批评斧正。感谢!!!
|