SpringBoot+Mybatis plus+swagger2测试

包结构


数据库表


配置中设置主键auto数据库中要打开主键自增

创建


pom.xml


    mysql

    mysql-connector-java

   

    5.1.48

    runtime

    com.baomidou

    mybatis-plus-boot-starter

    3.2.0


    io.springfox

    springfox-swagger2

    2.9.2

    io.springfox

    springfox-swagger-ui

    2.9.2

application.yml


# 公共配置

spring:

datasource:

    url: jdbc:mysql://localhost:3306/demotest?useUnicode=true&characterEncoding=utf8

driver-class-name: com.mysql.jdbc.Driver

    username: root

password: 1234

# mybatis plus

mybatis-plus:

  mapper-locations: classpath:mapper/*.xml# 映射xml位置

  type-aliases-package: net.wanho.demoplus2.po# 别名

  configuration: # mybatis的原生配置

    map-underscore-to-camel-case: true # 开启驼峰命名

    cache-enabled: false # 不使用缓存

    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印日志

  global-config:

db-config:

      id-type: auto  # 主键生成方案

      field-strategy: not_empty# 列的策略

      db-type: mysql# 数据库类型

config/MyBatisPlusConfig.java


@Configuration

public class MyBatisPlusConfig {

/**

* mybatis-plus分页插件

*/

    @Bean

    public PaginationInterceptorpaginationInterceptor() {

PaginationInterceptor page =new PaginationInterceptor();

        page.setDialectType("mysql");

        return page;

    }

}

po/Student.java

@ApiParam(required = “是否必须参数”, name = “参数名称”, value = “参数具体描述”)

@TableField(exist=false)表示当前属性不是数据库字段,项目中要用



mapper/StudentMapper.java


vo/AjaxResult


@Data

@NoArgsConstructor

@AllArgsConstructor

public class AjaxResult {

private int status;

    private Stringmsg;

    private Objectdata;

}

vo/PageInfo


controller/BaseController


public class BaseController {

public AjaxResultsuccess(String msg,Object data){

return new AjaxResult(200,msg,data);

    }

public AjaxResultsuccess(String msg){

return new AjaxResult(200,msg,null);

    }

public AjaxResultsuccess(Object data){

return new AjaxResult(200,null,data);

    }

public AjaxResultsuccess(){

return new AjaxResult(200,null,null);

    }

public AjaxResultfail(String msg,Object data){

return new AjaxResult(500,msg,data);

    }

public AjaxResultfail(String msg){

return new AjaxResult(500,msg,null);

    }

public AjaxResultfail(Object data){

return new AjaxResult(500,null,data);

    }

public AjaxResultfail(){

return new AjaxResult(500,null,null);

    }

}

controller/StuControllerApi

@ApiOperation(value = “接口说明”, httpMethod = “接口请求方式”, response = “接口返回参数类型”, notes = “接口发布说明”)

Api参数无需用@PathVariable说明



service/StudentService.java


分页


controller/StudentController.java



注解配置

config/SwaggerConfig.java


@Configuration

@EnableSwagger2

public class SwaggerConfig {

@Bean

    public DocketcreateRestApi() {

return new Docket(DocumentationType.SWAGGER_2)

.pathMapping("/")

.select()

.apis(RequestHandlerSelectors.basePackage("net.wanho.demoplus2.controller"))//controller所在包

                .paths(PathSelectors.any())

.build().apiInfo(new ApiInfoBuilder()

.title("SpringBoot整合Swagger")//文档标题

                        .description("SpringBoot整合Mybatis的CRUD操作")//文档描述

                        .version("1.0")//文档版本

                        .build());

    }

}

访问:

http://localhost:8080/swagger-ui.html

你可能感兴趣的:(SpringBoot+Mybatis plus+swagger2测试)