这一篇文章介绍SpringBoot使用WebFlux响应式编程操作MongoDb数据库。
前言
在之前一篇简单介绍了WebFlux响应式编程的操作,我们在来看一下下图,可以看到,在目前的Spring WebFlux还没有支持类似Mysql这样的关系型数据库,所以本文以MongoDb数据库为例。
SpringBoot使用WebFlux响应式编程操作数据库
接下来介绍SpringBoot使用WebFlux响应式编程操作MongoDb数据库。
新建项目
pom文件
新建项目,在项目中加入webflux依赖和mongodb-reactive依赖,完整pom代码如下:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.dalaoyang
springboot_mongodb2
0.0.1-SNAPSHOT
jar
springboot_mongodb2
springboot_mongodb2
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-data-mongodb-reactive
org.springframework.boot
spring-boot-starter-webflux
org.springframework.boot
spring-boot-devtools
runtime
org.springframework.boot
spring-boot-starter-test
test
io.projectreactor
reactor-test
test
org.springframework.boot
spring-boot-maven-plugin
配置文件
配置文件中配置mongodb数据库信息,之前文章有介绍,这里就不介绍了,配置文件代码如下:
##端口号
server.port=8888
##mongo配置
spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.port=27017
spring.data.mongodb.database=test
启动类
启动类是默认生成的,没有做任何修改,