SpringBoot整合MongoDb

Posted 天宇轩-王

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot整合MongoDb相关的知识,希望对你有一定的参考价值。

构建项目及配置

pom.xml

 <dependencies>
        <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-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </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>
    </dependencies>

application.yml

spring:
  application:
    name: spring-boot-mongodb
  data:
    mongodb:
      uri: mongodb://test:123456@192.168.180.113:27017/test

创建实体及具体实现

package com.topcheer.mongodb2.entity;

import org.springframework.data.annotation.Id;


public class UserInfo {

    @Id
    private Long id;

    private String username;

    private String password;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public UserInfo(Long id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public UserInfo() {
    }
}
@Component
public class UserHandler {

    private final UserRepository repository;

    public UserHandler(UserRepository repository) {
        this.repository = repository;
    }

    public Mono<ServerResponse> saveUser(ServerRequest request) {
        Mono<UserInfo> user = request.bodyToMono(UserInfo.class);
        return ServerResponse.ok().build(repository.insert(user).then());
    }

    public Mono<ServerResponse> deleteUser(ServerRequest request) {
        Long userId = Long.valueOf(request.pathVariable("id"));
        return ServerResponse.ok().build(repository.deleteById(userId).then());
    }

    public Mono<ServerResponse> getUser(ServerRequest request) {
        Long userId = Long.valueOf(request.pathVariable("id"));
        Mono<UserInfo> userInfo = repository.findById(userId);
        return ServerResponse.ok().contentType(APPLICATION_JSON).body(userInfo, UserInfo.class);
    }

    public Mono<ServerResponse> listUser(ServerRequest request) {
        Flux<UserInfo> userList = repository.findAll();
        return ServerResponse.ok().contentType(APPLICATION_JSON).body(userList, UserInfo.class);
    }


}
public interface UserRepository extends ReactiveMongoRepository<UserInfo,Long> {
}
package com.topcheer.mongodb2.router;

import com.topcheer.mongodb2.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;


@Configuration
public class UserRouter {
    @Bean
    public RouterFunction<ServerResponse> routeCity(UserHandler userHandler) {
        return RouterFunctions
                .route(RequestPredicates.GET("/listUser")
                                .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
                        userHandler::listUser)
                .andRoute(RequestPredicates.GET("/user/{id}")
                                .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
                        userHandler::getUser)
                .andRoute(RequestPredicates.GET("/deleteUser/{id}")
                                .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
                        userHandler::deleteUser)
                .andRoute(RequestPredicates.POST("/saveUser")
                                .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
                        userHandler::saveUser);
    }
}

测试:

 

 

以上是关于SpringBoot整合MongoDb的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot整合MongoDB教程

全栈编程系列SpringBoot整合Shiro(含KickoutSessionControlFilter并发在线人数控制以及不生效问题配置启动异常No SecurityManager...)(代码片段

Springboot整合MongoDB的Docker开发,其它应用也类似

SpringBoot整合MongoDB

SpringBoot整合MongoDB

《SpringBoot篇》15.SpringBoot整合MongoDB超详细教程(包括安装教程)