springboot学习(三十七) springboot使用webflux并实现mysql的CRUD

Spring WebFlux 是一个异步非阻塞式的 Web 框架, 可以运行在支持 Servlet 3.1 非阻塞IO的Servlet 容器上,或者其他异步运行环境,如 Netty、Undertow。它可以充分利用多核 CPU 资源去处理大量的并发请求,非常适合低延迟、高吞吐量的应用场景。

使用过程如下:
1、引入依赖
这里使用springboot版本是2.4.2,与搭建普通springboot-starter-web工程一样,去除spring-boot-starter-webflux依赖,并替换为如下依赖
如果构建工具使用maven:

		......
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-r2dbc</artifactId>
        </dependency>
        <!--  r2dbc 连接池 -->
        <dependency>
            <groupId>io.r2dbc</groupId>
            <artifactId>r2dbc-pool</artifactId>
        </dependency>
        <!--r2dbc mysql 库-->
        <dependency>
            <groupId>dev.miku</groupId>
            <artifactId>r2dbc-mysql</artifactId>
        </dependency>
        ......

如果构建工具使用gradle

...
dependencies {
	......
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-webflux'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-r2dbc'
    compile group: 'io.r2dbc', name: 'r2dbc-pool'
    compile group: 'dev.miku', name: 'r2dbc-mysql'
    ......
}
...

2、domain层

package com.iscas.webflux.test.domain;

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

/**
 *
 * @author zhuquanwen
 * @vesion 1.0
 * @date 2021/1/23 12:03
 * @since jdk1.8
 */
@Table("city")
@Data
public class City {
    @Id
    private Integer id;

    private String name;
    private String countrycode;
    private String district;
    private Integer population;
}

3、repository层

package com.iscas.webflux.test.repository;

import com.iscas.webflux.test.domain.City;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import reactor.core.publisher.Flux;

/**
 *
 * @author zhuquanwen
 * @vesion 1.0
 * @date 2021/1/23 12:07
 * @since jdk1.8
 */
public interface CityRepository extends ReactiveCrudRepository<City, Integer> {
    Flux<City> findByNameLike(String name);
}

4、service层

package com.iscas.webflux.test.service;

import com.iscas.webflux.test.domain.City;
import com.iscas.webflux.test.repository.CityRepository;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 *
 * @author zhuquanwen
 * @vesion 1.0
 * @date 2021/1/23 12:09
 * @since jdk1.8
 */
@Service
public class CityService {
    @Autowired
    private CityRepository cityRepository;

    public Mono<City> add(City city) {
        return cityRepository.save(city);
    }

    public Mono<City> edit(City city) {
        return cityRepository.save(city);
    }

    public Mono<Void> delete(City city) {
        return cityRepository.delete(city);
    }

    public Flux<City> search(String name) {
        return cityRepository.findByNameLike(name);
    }

}

5、controller层

package com.iscas.webflux.test.controller;

import com.iscas.webflux.test.domain.City;
import com.iscas.webflux.test.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 *
 * @author zhuquanwen
 * @vesion 1.0
 * @date 2021/1/23 12:14
 * @since jdk1.8
 */
@RestController
@RequestMapping("/city")
public class CityController {
    @Autowired
    private CityService cityService;

    @PostMapping
    public Mono<City> save(@RequestBody City city) {
        return cityService.add(city);
    }

    @DeleteMapping
    public Mono<Void> delete(Integer id) {
        City city = new City();
        city.setId(id);
        return cityService.delete(city);
    }

    @PutMapping
    public Mono<City> edit(@RequestBody City city) {
        return cityService.edit(city);
    }

    @GetMapping("/{name}")
    public Flux<City> search(@PathVariable String name) {
        return cityService.search(name);
    }
}

6、启动类

package com.iscas.webflux.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 *
 * @author zhuquanwen
 * @vesion 1.0
 * @date 2021/1/23 11:58
 * @since jdk1.8
 */
@SpringBootApplication
public class WebfluxApp {
    public static void main(String[] args) {
        SpringApplication.run(WebfluxApp.class, args);
    }
}

7、配置文件

spring.r2dbc.url= r2dbcs:mysql://127.0.0.1:3306/world
spring.r2dbc.username=root
spring.r2dbc.password=root
server.port=7701
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值