WebFlux系列(十三)MySql应用新增、修改、查询、删除

Java#Spring#SpringBoot#MySql#reactor#webflux#数据库#新增#修改#查询#删除

Spring WebFlux Mysql 数据库新增、删除、查询、修改

视频讲解 : https://www.bilibili.com/vide...

Employee.java

package com.example.springwebfluxmysql;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;

@Table("employee")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
    @Id
    private Long id;
    private String name;
}

EmployeeController.java

package com.example.springwebfluxmysql;

import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
@AllArgsConstructor
@RequestMapping("/employee")
public class EmployeeController {

    private final EmployeeRep employeeRep;

    @DeleteMapping("/{id}")
    public Mono delete(@PathVariable Long id){
        return employeeRep.deleteById(id);
    }

    @GetMapping("/{id}")
    public Mono findById(@PathVariable Long id){
        return employeeRep.findById(id);
    }
    @PutMapping
    public Mono update(@RequestBody Employee employee){
        return employeeRep.save(employee);
    }

    @PostMapping
    public Mono save(@RequestBody Employee employee){
        return employeeRep.save(employee);
    }
    @GetMapping
    public Flux findAll(){
        return employeeRep.findAll();
    }

}

EmployeeRep.java

package com.example.springwebfluxmysql;

import org.springframework.data.repository.reactive.ReactiveCrudRepository;

public interface EmployeeRep extends ReactiveCrudRepository {
}

MysqlConfigurtion.java

package com.example.springwebfluxmysql;

import com.github.jasync.r2dbc.mysql.JasyncConnectionFactory;
import com.github.jasync.sql.db.mysql.pool.MySQLConnectionFactory;
import com.github.jasync.sql.db.mysql.util.URLParser;
import io.r2dbc.spi.ConnectionFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;

import java.nio.charset.StandardCharsets;

@Configuration
public class MysqlConfigurtion extends AbstractR2dbcConfiguration {
    @Override
    public ConnectionFactory connectionFactory() {
        String url ="mysql://root:[email protected]:3306/test";
        return new JasyncConnectionFactory(new MySQLConnectionFactory(URLParser.INSTANCE.parseOrDie(url, StandardCharsets.UTF_8)));
    }

}

SpringWebfluxMysqlApplication.java

package com.example.springwebfluxmysql;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
@SpringBootApplication
@EnableR2dbcRepositories
public class SpringWebfluxMysqlApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringWebfluxMysqlApplication.class, args);
    }
}

公众号,坚持每天3分钟视频学习

你可能感兴趣的:(java,springboot,reactor,mysql)