Spring-webflux访问关系型数据库实战
作者:右耳菌
这篇文章主要为大家介绍了Spring-webflux访问关系型数据库实战详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
1. pom.xml引入
主要是支持在响应式的环境下对关系型数据库进行访问
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-r2dbc</artifactId> </dependency> <dependency> <groupId>com.github.jasync-sql</groupId> <artifactId>jasync-r2dbc-mysql</artifactId> <version>1.1.3</version> </dependency>
注意: 这里对于SpringBoot的版本似乎是有一些要求的,为了避免遇到无法正常启动的情况,建议使用版本2.3.0-RELEASE版本
完整的xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cn.lazyfennec</groupId> <artifactId>webflux-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>webflux-demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.3.0.RELEASE</spring-boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-r2dbc</artifactId> </dependency> <dependency> <groupId>com.github.jasync-sql</groupId> <artifactId>jasync-r2dbc-mysql</artifactId> <version>1.1.3</version> </dependency> </dependencies> <!-- 这里很重要,限定了SpringBoot的依赖版本 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
2. 修改application.yml
spring: r2dbc: url: r2dbcs:mysql://localhost:3306/test username: root password: 123456
3. 创建实体类User
package cn.lazyfennec.webfluxdemo.model; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.data.annotation.Id; import org.springframework.data.relational.core.mapping.Table; /** * @Author: Neco * @Description: * @Date: create in 2022/7/25 23:15 */ @Table("t_user") @Data @AllArgsConstructor @NoArgsConstructor public class User { @Id private Long id; private String name; }
4. 修改UserController
package cn.lazyfennec.webfluxdemo.controller; import cn.lazyfennec.webfluxdemo.dao.UserDao; import cn.lazyfennec.webfluxdemo.model.User; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.util.stream.IntStream; /** * @Author: Neco * @Description: * @Date: create in 2022/7/25 22:13 */ @RestController @Slf4j @AllArgsConstructor public class UserController { private final UserDao userDao; @GetMapping("user") public Flux<User> findAll(){ return userDao.findAll(); } @PostMapping("save") public Mono save(@RequestBody User user){ return userDao.save(user); } @DeleteMapping("user/{id}") public Mono delete(@PathVariable Long id){ return userDao.deleteById(id); } @GetMapping("user/{id}") public Mono findById(@PathVariable Long id){ return userDao.findById(id); } }
5. 启动类上方增加 @EnableR2dbcRepositories
package cn.lazyfennec.webfluxdemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories; @SpringBootApplication @EnableR2dbcRepositories // 必须新增这个 public class WebfluxDemoApplication { public static void main(String[] args) { SpringApplication.run(WebfluxDemoApplication.class, args); } }
6. 启动然后访问相关网址测试
其实除了以上的方式之外,还有另外的方式,即handler 和 router (路由的)方式
新增以下两个类
- UserHandler
package cn.lazyfennec.webfluxdemo.handler; import cn.lazyfennec.webfluxdemo.dao.UserDao; import cn.lazyfennec.webfluxdemo.model.User; import lombok.AllArgsConstructor; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerResponse; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; /** * @Author: Neco * @Description: * @Date: create in 2022/7/26 0:25 */ @Component @AllArgsConstructor public class UserHandler { private final UserDao userDao; //http://localhost:8080/save public Mono<ServerResponse> saveUser(ServerRequest request) { Mono<User> mono = request.bodyToMono(User.class); User user = mono.block(); return ServerResponse.ok().build(userDao.save(user).then()); } public Mono<ServerResponse> deleteUser(ServerRequest request) { Long id = Long.valueOf(request.pathVariable("id")); return ServerResponse.ok().build(userDao.deleteById(id).then()); } public Mono<ServerResponse> getUserbyId(ServerRequest request) { Long id = Long.valueOf(request.pathVariable("id")); Mono<User> mono = userDao.findById(id); return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(mono, User.class); } public Mono<ServerResponse> listUser(ServerRequest serverRequest) { Flux<User> userFlux = userDao.findAll(); return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(userFlux, User.class); } }
- UserRouter
package cn.lazyfennec.webfluxdemo.config; import cn.lazyfennec.webfluxdemo.handler.UserHandler; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.web.reactive.function.server.RequestPredicates; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerResponse; /** * @Author: Neco * @Description: * @Date: create in 2022/7/26 0:27 */ @Configuration public class UserRouter { @Bean public RouterFunction<ServerResponse> routUser(UserHandler userHandler) { return RouterFunctions .route(RequestPredicates.GET("/user") .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), userHandler::listUser) .andRoute(RequestPredicates.GET("/user/{id}") .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), userHandler::getUserbyId) .andRoute(RequestPredicates.DELETE("/user/{id}") .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), userHandler::deleteUser); } }
- 注释掉UserController
访问同样的内容
正常
Tips
官方并不建议使用mysql和webflux相结合,主要是事务方面会产生一定的问题。
关于webflux,在国内其实会使用到的几率并不是很大,所以大致了解一些简单的使用基本就可以了。
以上就是Spring-webflux访问关系型数据库实战的详细内容,更多关于Spring-webflux访问数据库的资料请关注脚本之家其它相关文章!