WebFlux系列(十三)MySql應用新增、修改、查詢、刪除

 

 

#Java#Spring#SpringBoot#MySql#reactor#webflux#數據庫#新增#修改#查詢#刪除#

Spring WebFlux Mysql 數據庫新增、刪除、查詢、修改

視頻講解 : https://www.bilibili.com/video/av84439890/

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<Employee> findAll(){
        return employeeRep.findAll();
    }

}

EmployeeRep.java
package com.example.springwebfluxmysql;

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

public interface EmployeeRep extends ReactiveCrudRepository<Employee,Long> {
}

 

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分鐘視頻學習

發佈了51 篇原創文章 · 獲贊 0 · 訪問量 8263
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章