WEBFLUX04 Springbootwebflux Integrated MongoDB version of Windows

Source: Internet
Author: User
Tags install mongodb mongodb mongodb version

1 Download and install MongoDB 1.1 MongoDB website

    

1.2 Downloads

Solutions-Download Center

    

1.3 Installation

Double click to enter the installation can

1.3.1 Common BUG01 during installation

      

1.3.2 BUG01 Solutions

      

1.4 Start MongoDB

Tip # 01: You need to create a data directory at the same level as the installation directory to hold it

Tip 02: Save the following command as a bat file and double-click it the next time you start

C:\tool\mongoDB\bin\mongod--dbpath C:\tool\data--smallfiles
1.5 MongoDB Normal Startup console information

    

2 Springbootwebflux Integrated MongoDB 2.1 Create a project

Introducing dependencies: 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_dem O</name> <description>demo Project for Spring boot</description> <parent> <groupId>org.springframework.boot</groupId> &        Lt;artifactid>spring-boot-starter-parent</artifactid> <version>2.0.3.RELEASE</version> <relativePath/> <!--lookup parent from repository to </parent> <properties> <pro Ject.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding >UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties&    Gt            <dependencies> <dependency> <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-devtools</ar            Tifactid><!--<scope>runtime</scope>--> <optional> true</optional> </dependency> <dependency> <groupid>org.projectlombok</gro Upid> <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&gt            ;spring-boot-starter-data-mongodb-reactive</artifactid> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-test</arti factid> <scope>test</scope> </dependency> <dependency> < Groupid>io.projectreactor</groupid> <artifactId>reactor-test</artifactId> <s cope>test</scope> </dependency> <dependency> <groupid>org.junit.jupite r</groupid> <artifactId>junit-jupiter-api</artifactId> </dependency> &LT;/DEP endencies> <build> <plugins> <plugin> &LT;GROUPID&GT;ORG.SPRINGFR Amework.boot</groupid> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> &LT;/BUILD&GT;&L T;/project>
Pom.xml2.2 Startup Class

Add @EnableReactiveMongoRepositories annotations on the startup class to turn on MongoDB-related configuration

 PackageCn.xiangxu.webflux_demo;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.data.mongodb.repository.config.enablereactivemongorepositories;@ Springbootapplication@enablereactivemongorepositories Public classwebfluxdemoapplication { Public Static voidMain (string[] args) {Springapplication.run (webfluxdemoapplication.class, args); }}
View Code2.3 Entity Classes

    @Document (collection = "user") for the purpose of defining table names in MongoDB, equivalent to @table annotations in JPA

Tip 01: Primary keys in MongoDB are generally string-type

 PackageCn.xiangxu.webflux_demo.domain;ImportLombok. Data;Importorg.springframework.data.annotation.Id;Importorg.springframework.data.mongodb.core.mapping.Document;/** * @authorWang Yangji * @create 2018-06-27 8:42 * @desc **/@Document (Collection= "User") @Data Public classUser {@IdPrivateString ID; PrivateString name; Private intAge ;}
User.java2.4 Persistence Layer

You just have to inherit the Reactivemongorepository interface, like JPA.

 Packagecn.xiangxu.webflux_demo.repository;ImportCn.xiangxu.webflux_demo.domain.User;Importorg.springframework.data.mongodb.repository.ReactiveMongoRepository;Importorg.springframework.stereotype.Repository;/** * @authorWang Yangji * @create 2018-06-27 8:44 * @desc **/@Repository Public InterfaceUserrepositoryextendsReactivemongorepository<user, string> {}
Userrepository.java2.5 Control Layer

Tip 01: There are two ways to return, one is to return all data at once, and the other is to return like a stream

 PackageCn.xiangxu.webflux_demo.web;ImportCn.xiangxu.webflux_demo.domain.User;Importcn.xiangxu.webflux_demo.repository.UserRepository;ImportOrg.springframework.http.MediaType;Importorg.springframework.web.bind.annotation.GetMapping;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController;ImportReactor.core.publisher.Flux;/** * @authorWang Yangji * @create 2018-06-27 8:45 * @desc **/@RestController @requestmapping (value= "/user") Public classUsercontroller {Private Finaluserrepository userrepository; /*** Using constructors to inject persistent layer objects *@paramuserrepository*/     PublicUsercontroller (userrepository userrepository) { This. Userrepository =userrepository; }    /*** return at once *@return     */@GetMapping (Value= "/")     PublicFlux<user>GetAll () {returnUserrepository.findall (); }    /*** Flow back *@return     */@GetMapping (Value= "/stream/all", produces =mediatype.text_event_stream_value) PublicFlux<user>Streamgetall () {returnUserrepository.findall (); }}
Usercontroller.java2.6 Configuration Files

Prerequisite: Install MongoDB and start

SPRING.DATA.MONGODB.URI=MONGODB://Localhost:27017/webflux
2.7 Start and test

Tip 01: Test with Postman

    

WEBFLUX04 Springbootwebflux Integrated MongoDB version of Windows

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.