solutions -> download center
双击进入安装即可
技巧01:需要在同安装目录同一级别创建一个data目录来存放数据
技巧02:将下下面的命令存储成一个 bat 文件,下次启动时双击即可
C:\tool\mongoDB\bin\mongod --dbpath C:\tool\data --smallfiles
引入相关依赖:webflux、mongodb、devtool、lombok
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.xiangxu</groupId> <artifactId>webflux_demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>webflux_demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <!--<scope>runtime</scope>--> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb-reactive --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId> </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.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build> </project>
在启动类上添加 @EnableReactiveMongoRepositories 注解来开启mongodb相关的配置
package cn.xiangxu.webflux_demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories; @SpringBootApplication @EnableReactiveMongoRepositories public class WebfluxDemoApplication { public static void main(String[] args) { SpringApplication.run(WebfluxDemoApplication.class, args); } }
@Document(collection = "user") 目的时定义在mongodb中的表名,相当于JPA中的@Table注解
技巧01:在mongodb中的主键一般都是String类型的
package cn.xiangxu.webflux_demo.domain; import lombok.Data; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; /** * @author 王杨帅 * @create 2018-06-27 8:42 * @desc **/ @Document(collection = "user") @Data public class User { @Id private String id; private String name; private int age; }
只需要继承 ReactiveMongoRepository 接口就行啦,和JPA差不多
package cn.xiangxu.webflux_demo.repository; import cn.xiangxu.webflux_demo.domain.User; import org.springframework.data.mongodb.repository.ReactiveMongoRepository; import org.springframework.stereotype.Repository; /** * @author 王杨帅 * @create 2018-06-27 8:44 * @desc **/ @Repository public interface UserRepository extends ReactiveMongoRepository<User, String> { }
技巧01:有两种返回方式,一种是把所有数据一次性返回,另一种是像流一样的进行返回
package cn.xiangxu.webflux_demo.web; import cn.xiangxu.webflux_demo.domain.User; import cn.xiangxu.webflux_demo.repository.UserRepository; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Flux; /** * @author 王杨帅 * @create 2018-06-27 8:45 * @desc **/ @RestController @RequestMapping(value = "/user") public class UserController { private final UserRepository userRepository; /** * 利用构造器注入持久层对象 * @param userRepository */ public UserController(UserRepository userRepository) { this.userRepository = userRepository; } /** * 一次性返回 * @return */ @GetMapping(value = "/") public Flux<User> getAll() { return userRepository.findAll(); } /** * 流式返回 * @return */ @GetMapping(value = "/stream/all", produces = MediaType.TEXT_EVENT_STREAM_VALUE) public Flux<User> streamGetAll() { return userRepository.findAll(); } }
前提:安装好mongodb并启动
spring.data.mongodb.uri=mongodb://localhost:27017/webflux
技巧01:利用postman进行测试
WebFlux04 SpringBootWebFlux集成MongoDB之Windows版本
原文:https://www.cnblogs.com/NeverCtrl-C/p/9241138.html