文章目录
- 1、创建项目,添加Redis依赖
- 2、创建实体类Student
- 3、创建Controller
- 4、配置application.yml
- 5、整合完成
Redis
(
Remote Dictionary Server
)是一个开源的内存数据库,遵守 BSD 协议,它提供了一个高性能的键值(
key - value
)存储系统,常用于缓存、消息队列、会话存储等应用场景。
SpringBoot
整合
Redis
流程如下:
1、创建项目,添加Redis依赖
创建Springboot
项目,添加Redis
依赖
添加依赖后,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>3.3.3</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com</groupId><artifactId>redis</artifactId><version>0.0.1-SNAPSHOT</version><name>redis</name><description>redis</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>22</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><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><!-- redis --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- 连接池 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></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>
2、创建实体类Student
创建包com.redis.entity
,创建实体类Student
package com.redis.entity;import lombok.Data;import java.io.Serializable;
import java.util.Date;@Data
public class Student implements Serializable {private Long id;private String name;private Double score;private Date birthday;
}
3、创建Controller
创建包com.redis.controller
,创建类StudentController
package com.redis.controller;import com.redis.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;@RestController
public class StudentController {@Autowiredprivate RedisTemplate redisTemplate;@PostMapping("/set")public void set(@RequestBody Student student){redisTemplate.opsForValue().set("stu",student);}@GetMapping("/get/{key}")public Student get(@PathVariable("key") String key){return (Student) redisTemplate.opsForValue().get(key);}@DeleteMapping("/delete/{key}")public boolean delete(@PathVariable("key") String key){redisTemplate.delete(key);return redisTemplate.hasKey(key);}
}
4、配置application.yml
application.yml
内容如下:
spring:data:redis:database: 0host: localhostport: 6379
5、整合完成
整合完成,快来试试吧。