WebFlux 完成增删改查功能

WebFlux是一个基于Reactor异步、非阻塞的web框架。WebFlux可以运行在Netty, UndertowServlet 3.1以上的容器中

WebFlux并不能使接口的响应时间缩短,它仅仅能够提升吞吐量和伸缩性

WebFlux提供了两种使用方式:注解式(Annotated Controllers)和 函数式(Functional Endpoints

  • 注解式:和SpringMvc的注解一致,使用RestControllerGetMappingPostMapping等注解
  • 函数式:基于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("删除成功");
         }
     
     }
上一篇:一起谈.NET技术,Silverlight 2.5D RPG游戏技巧与特效处理:(九)粒子系统


下一篇:使用C4C ABSL获得当前登录用户所在的organization unit信息