1. 案例需求
基于Spring Boot+Redis实现图书信息按书号存储和取出功能,数据存储至Redis。
2. 创建Spring Boot项目
创建Spring Boot项目,项目名称为springboot-redis02。
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: localhostport: 6379database: 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. 创建Book实体类
com.wfit.boot.model目录下创建Book.java。
@Data
public class Book implements Serializable {private String id;private String name;private String author;}
8. 创建BookController类
com.wfit.boot.controller目录下创建BookController.java。
@RestController
@RequestMapping("/book")
public class BookController {@Autowiredprivate BookService bookService;/*** 新增图书信息* @param book* @return*/@PostMapping("/add")public Result addBook(@RequestBody Book book){bookService.addBook(book);return Result.success("新增图书信息成功!");}/*** 查询图书信息*/@GetMapping("/query")public Result query(String id){Book book = bookService.queryBook(id);return Result.success(book);}
}
9. 创建BookService接口
com.wfit.boot.service目录下创建BookService.java。
public interface BookService {public void addBook(Book book);public Book queryBook(String id);}
10. 创建BookServiceImpl类
com.wfit.boot.service.impl目录下创建BookServiceImpl.java。
@Service
public class StudentServiceImpl implements StudentService {@Resourceprivate RedisTemplate<String,Object> redisTemplate;@Overridepublic void addStudent(Student student) {redisTemplate.opsForValue().set(student.getId(),student);}@Overridepublic Student queryStudent(String id) {return (Student)redisTemplate.opsForValue().get(id);}
}
11. 案例测试
-
测试新增图书信息
-
测试查询图书信息