登录
首页 >  数据库 >  MySQL

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

来源:SegmentFault

时间:2023-02-24 17:24:20 265浏览 收藏

大家好,今天本人给大家带来文章《WebFlux系列(十三)MySql应用新增、修改、查询、删除》,文中内容主要涉及到MySQL、Java、springboot、reactor,如果你对数据库方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!

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:123456@127.0.0.1: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分钟视频学习

理论要掌握,实操不能落!以上关于《WebFlux系列(十三)MySql应用新增、修改、查询、删除》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>