需要的依赖库:
<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: 0host: 192.168.56.201port: 6379username: xxxxxxpassword: 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 {@Resourceprivate RedisTemplate redisTemplate;@PostMapping("/setStudent")public String setStudent(@RequestBody Student student){redisTemplate.opsForValue().set("student1", student);return "SUCCESS";}
}