1. 案例需求
基于Spring Boot+Redis实现学生信息按学号存储和取出功能,数据存储至Redis。
2. 创建Spring Boot项目
创建Spring Boot项目,项目名称为springboot-redis01。
3. 选择依赖
pom.xml文件内容如下所示:
<?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.0</version>
<relativePath/>
</parent>
<groupId>com.wfit</groupId>
<artifactId>Spring Boot-redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring Boot-redis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--spring-boot-starter-web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
4. 创建配置文件
resources目录下创建application.yml。
server:
port: 8090
spring:
redis:
host: localhost
port: 6379
database: 0
5. 创建Constants常量类
com.wfit.boot.commons目录下创建Constants.java。
public class Constants {
// 默认成功码
public static final int SUCCESS_CODE = 200;
public static final String SUCCESS_MSG = "操作成功";
// 默认失败码
public static final int ERROR_CODE = 500;
public static final String ERROR_MSG = "系统异常";
}
6. 创建Result类
com.wfit.boot.commons目录下创建Result.java。
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
//响应码
private int code;
//响应消息
private String msg;
//响应结果
private T data;
public static <T> Result<T> success(T data){
return new Result<>(Constants.SUCCESS_CODE,Constants.SUCCESS_MSG,data);
}
public static <T> Result<T> error(T data){
return new Result<>(Constants.ERROR_CODE,Constants.ERROR_MSG,data);
}
}
7. 创建Student实体类
com.wfit.boot.model目录下创建Student.java。
@Data
public class Student implements Serializable {
private String id;
private String name;
private int age;
}
8
8. 创建StudentController类
com.wfit.boot.controller目录下创建StudentController.java。
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
Private StudentService studentService;
/**
* 新增学生信息
* @param student
* @return
*/
@PostMapping("/add")
public Result addStudent(@RequestBody Student student){
studentService.addStudent(student);
return Result.success("新增学生信息成功!");
}
/**
* 查询单个学生信息
*/
@GetMapping("/query")
public Result query(String id){
Student student = studentService.queryStudent(id);
return Result.success(student);
}
}
9. 创建StudentService接口
com.wfit.boot.service目录下创建StudentService.java。
public interface StudentService {
public void addStudent(Student student);
public Student queryStudent(String id);
}
10. 创建StudentServiceImpl类
com.wfit.boot.service.impl目录下创建StudentServiceImpl.java。
@Service
public class StudentServiceImpl implements StudentService {
@Resource
private RedisTemplate<String,Object> redisTemplate;
@Override
public void addStudent(Student student) {
redisTemplate.opsForValue().set(student.getId(),student);
}
@Override
public Student queryStudent(String id) {
return (Student)redisTemplate.opsForValue().get(id);
}
}
11. 案例测试
-
测试新增学生信息
-
测试查询学生信息