需要的依赖库:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<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>
单节点整合SpringBoot:
spring:
redis:
database: 0
host: 192.168.56.201
port: 6379
username: xxxxxx
password: XXXXXXXXX
Student.java:
package com.lscbaiotaigc.entiies;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Serializable {
private String id;
private String name;
private int age;
private Date birthday;
}
StudentController.java:
package com.lscbaiotaigc.controller;
import com.lscbaiotaigc.entities.Student;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class StudentController {
@Resource
private RedisTemplate redisTemplate;
@PostMapping("/setStudent")
public String setStudent(@RequestBody Student student){
redisTemplate.opsForValue().set("student1", student);
return "SUCCESS";
}
}