idea spring boot configures MongoDB, idea spring boot integrates MongoDB pringboot integrates swagger

1. First you need to install the MongoDB database

table of Contents

1. First you have to install MongoDB or have MongoDB server

You can also download the configured scaffolding here

2. First click file --"new --"project in the idea, as shown in the next step

3. Here we first configure swagger as shown in the figure

3.1 Configure here swagger jar

3.2 Modify the application.applicat file to a yml file here

3.3 Create access interface


1. First you have to install MongoDB or have MongoDB server

You can also download the configured scaffolding here

https://gitee.com/bxhbc/spring-boot-space/tree/master

2. First click file --"new --"project in the idea, as shown in the next step

At this time your project has been created

3. Here we first configure swagger as shown in the figure

3.1 Configure here swagger jar

<!--   swagger2 与  swagger ui     -->        <dependency>            <groupId>io.springfox</groupId>            <artifactId>springfox-swagger2</artifactId>            <version>2.9.2</version>        </dependency>        <dependency>            <groupId>io.springfox</groupId>            <artifactId>springfox-swagger-ui</artifactId>            <version>2.9.2</version>        </dependency>
@[email protected] class SwaggerConfig {    @Bean    public Docket createRestApi() {        return new Docket(DocumentationType.SWAGGER_2)                .apiInfo(apiInfo())                .select()                .apis(RequestHandlerSelectors.basePackage("com.space."))                //这里是controller所处的包                .paths(PathSelectors.any())                .build();     }    /**     * 构建api文档的详细信息函数     */    private ApiInfo apiInfo() {        return new ApiInfoBuilder()                //页面标题                .title("APP")                //描述                .description("api查询测试接口")                .termsOfServiceUrl("API terms of service")                .licenseUrl("http://192.168.3.17:8080/")                .version("1.0")                .build();    }}

3.2 Modify the application.applicat file to a yml file here

application.yml

spring:  profiles:    active: dev

application-dev.yml

server:  port: 8081 # 该配置的名称是固定的,不可以改变,否则将不能自动加载到数据源中spring:  datasource:    # 使用druid数据源    type: com.alibaba.druid.pool.DruidDataSource    driver-class-name: com.mysql.jdbc.Driver    url: jdbc:mysql://localhost:3306/test?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8    username: root    password: root    filters: stat    maxActive: 20    initialSize: 1    maxWait: 60000    minIdle: 1    timeBetweenEvictionRunsMillis: 60000    minEvictableIdleTimeMillis: 300000    validationQuery: select 1 FROM DUAL    testWhileIdle: true    testOnBorrow: false    testOnReturn: false    poolPreparedStatements: true    maxOpenPreparedStatements: 20  data:    mongodb:      port: 27017      host: localhost      database: test#      uri:      option:        socket-keep-alive: true        max-connection-idle-time: 60000#        connect-timeout: 36000#        min-connection-per-host: 5#        threads-allowed-to-block-for-connection-multiplier: 5#        max-wait-time: 120000#        socket-timeout: 0#        max-connection-life-time: 0#        heartbeat-socket-timeout: 36000#        heartbeat-connect-timeout: 36000#        min-heartbeat-frequency: 5#        heartbeat-frequency: 10  redis:    host: localhost    password: 123456    port: 6379    jedis:      pool:        max-active: 8        max-wait: -1        max-idle: 8        min-idle: 0    timeout: 30000    database: 0#mybatis 配置mybatis-plus:  type-aliases-package: com.space.mapper  mapper-locations: mapper/*.xml

3.3 Create access interface

controller interface

@Api(tags = "测试接口描述",value = "口描述")@[email protected]("/test")public class TestController {    @Autowired    TUserService tUserService;      @GetMapping("/mo")    @ApiOperation(value = "测试接口MongoDB")    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "整数",required = true)})    public String testMongodb(@RequestParam("id")int id){        User1 us = new User1();        us.setId(id+"");        us.setName("huahua");        us.setAge(id+6);        System.out.println(us.toString());        User1 usr = tUserService.insertUser(us);        System.out.println("------------------");        System.out.println(usr.toString());        System.out.println("********************");        long n = tUserService.getCount();        System.out.println(n);        System.out.println("+++++++++++++");        return "success";    }   }

service

@Servicepublic class TUserService {    @Autowired    MongoTemplate mongoTemplate;    static int c = 0;     public long getCount(){        Query query = new Query();        long l = mongoTemplate.count(query,User1.class);        return l;    }     public User1 getUser(){        User1 user1 = mongoTemplate.findById(1,User1.class);        return user1;    }     public User1 insertUser(User1 us){        return mongoTemplate.insert(us);    } }

entity

@[email protected](collection = "t_user")public class User1 {    @Field("id")    private String id;     @Field("name")    private String name;     @Field("age")    private int age;}

The @Data above uses lombok

<!-- lombok 记得idea安装lombok插件-->        <dependency>            <groupId>org.projectlombok</groupId>            <artifactId>lombok</artifactId>            <version>1.16.16</version>        </dependency>

idea install lombok plugin file - "settings

4. You can use swagger by visiting swagger, the address of swagger: http://localhost:8081/swagger-ui.html

Console printing

swagger returns results