Spring Boot CURD Redis integration of

1. Create the MAVEN project in IDEA
2. Introduction of dependence in pom.xml

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

Creating the Entity Classes
Here Insert Picture Description

package com.mj.entity;

import lombok.Data;
import java.io.Serializable;
import java.util.Date;

@Data
public class Student implements Serializable {
    private Integer id;
    private String name;
    private Double score;
    private Date birthday;
}

Create a controller
Here Insert Picture Description

package com.mj.entity.com.mj.controller;

import com.mj.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
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;

@RestController
public class StudentHandler {

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/set")
    public void set(@RequestBody Student student){

        redisTemplate.opsForValue().set("student",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);
        
    }
}

Create a profile
Here Insert Picture Description

spring:
  redis:
    database: 0
    host: localhost
    port: 6379

Create a startup class

package com.mj;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application{

    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}
Published 42 original articles · won praise 0 · Views 942

Guess you like

Origin blog.csdn.net/mojiezhao/article/details/104137474