SpringBoot+Mybatis plus+swagger2測試

包結(jié)構(gòu)


數(shù)據(jù)庫表


配置中設(shè)置主鍵auto數(shù)據(jù)庫中要打開主鍵自增

創(chuàng)建


pom.xml


<dependency>

? ? <groupId>mysql</groupId>

? ? <artifactId>mysql-connector-java</artifactId>

? ? <!-- 要加上版本限制,父工程默認是8的版本 -->

? ? <version>5.1.48</version>

? ? <scope>runtime</scope>

</dependency>

<!--? mybatis plus-->

<dependency>

? ? <groupId>com.baomidou</groupId>

? ? <artifactId>mybatis-plus-boot-starter</artifactId>

? ? <version>3.2.0</version>

</dependency>


<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>

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# 數(shù)據(jù)庫類型

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 = “是否必須參數(shù)”, name = “參數(shù)名稱”, value = “參數(shù)具體描述”)

@TableField(exist=false)表示當前屬性不是數(shù)據(jù)庫字段,項目中要用



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 = “接口返回參數(shù)類型”, notes = “接口發(fā)布說明”)

Api參數(shù)無需用@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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容