SpringBoot2.7.14整合redis7

需要的依赖库:


            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        

        
            org.apache.commons
            commons-pool2
        

单节点整合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";
    }
}

你可能感兴趣的:(springboot,redis,spring,boot,后端,java)