天天看點

WebFlux 完成增删改查功能

WebFlux

是一個基于

Reactor

異步、非阻塞

的web架構。

WebFlux

可以運作在

Netty

,

Undertow

Servlet 3.1以上

的容器中

WebFlux

并不能使接口的響應時間縮短,它僅僅能夠提升吞吐量和伸縮性

WebFlux

提供了兩種使用方式:注解式(

Annotated Controllers

)和 函數式(

Functional Endpoints

  • 注解式:和

    SpringMvc

    的注解一緻,使用

    RestController

    GetMapping

    PostMapping

    等注解
  • 函數式:基于Lambda表達式,使用Function描述請求端點

本文示範基于注解的使用方式

示例

  1. 建立資料表
    CREATE TABLE `user` (
      `id` int(64) NOT NULL AUTO_INCREMENT,
      `name` varchar(255) DEFAULT NULL,
      `age` int(4) DEFAULT NULL,
      `sex` varchar(2) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;           
  2. 引入依賴
    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-webflux</artifactId>
            </dependency>
            <dependency>
                <groupId>dev.miku</groupId>
                <artifactId>r2dbc-mysql</artifactId>
                <version>0.8.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-r2dbc</artifactId>
            </dependency>           
  3. 編寫配置檔案
    server:
      port: 8099
    spring:
      data:
        r2dbc:
          repositories:
            enabled: true
    
      r2dbc:
        url: r2dbc:mysql://**********:****/user?useUnicode=true&characterEncoding=UTF-8&useSSL=false
        username: ****
        password: ****
        pool:
          enabled: true
          max-size: 10
          initial-size: 10
          validation-query: select 1           
  4. 建立實體類
    @Data
    @Table(value = "user")
    public class User {
        @Id
        private Integer id;
    
        private String name;
    
        private Integer age;
    
        private String sex;
    }           
  5. 建立Repository
    public interface UserRepository extends R2dbcRepository<User, Integer> {
    }           
  6. 建立Controller
    @RestController
     public class UserController {
     
         @Resource
         private UserRepository userRepository;
     
         @PostMapping("user")
         public Mono<Integer> createUser(@RequestBody User user){
     
             Mono<User> userMono = userRepository.save(user);
             return userMono.map(User::getId);
         }
     
         @GetMapping("user/{id}")
         public Mono<User> getUserById(@PathVariable("id") Integer id){
     
             Mono<User> userMono = userRepository.findById(id);
             return userMono;
         }
     
         @PutMapping("user")
         public Mono<String> updateUser(@RequestBody User user){
     
             userRepository.save(user);
             return Mono.just("修改成功");
         }
     
         @DeleteMapping("user/{id}")
         public Mono<String> delUser(@PathVariable("id") Integer id){
     
             userRepository.deleteById(id);
             return Mono.just("删除成功");
         }
     
     }