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>

? ? <!-- 要加上版本限制,父工程默認(rèn)是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)表示當(dāng)前屬性不是數(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")//文檔標(biāo)題

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

? ? ? ? ? ? ? ? ? ? ? ? .version("1.0")//文檔版本

? ? ? ? ? ? ? ? ? ? ? ? .build());

? ? }

}

訪問:

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末吕粗,一起剝皮案震驚了整個濱河市淌喻,隨后出現(xiàn)的幾起案子俘枫,更是在濱河造成了極大的恐慌译断,老刑警劉巖谦炒,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件挫剑,死亡現(xiàn)場離奇詭異去扣,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)樊破,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進(jìn)店門愉棱,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人哲戚,你說我怎么就攤上這事奔滑。” “怎么了惫恼?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵档押,是天一觀的道長。 經(jīng)常有香客問我祈纯,道長令宿,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任腕窥,我火速辦了婚禮粒没,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘簇爆。我一直安慰自己癞松,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布入蛆。 她就那樣靜靜地躺著响蓉,像睡著了一般。 火紅的嫁衣襯著肌膚如雪哨毁。 梳的紋絲不亂的頭發(fā)上枫甲,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天,我揣著相機(jī)與錄音扼褪,去河邊找鬼想幻。 笑死,一個胖子當(dāng)著我的面吹牛话浇,可吹牛的內(nèi)容都是我干的脏毯。 我是一名探鬼主播,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼幔崖,長吁一口氣:“原來是場噩夢啊……” “哼食店!你這毒婦竟也來了渣淤?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤叛买,失蹤者是張志新(化名)和其女友劉穎砂代,沒想到半個月后蹋订,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體率挣,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年露戒,在試婚紗的時候發(fā)現(xiàn)自己被綠了椒功。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡智什,死狀恐怖动漾,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情荠锭,我是刑警寧澤旱眯,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站证九,受9級特大地震影響删豺,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜愧怜,卻給世界環(huán)境...
    茶點故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一呀页、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧拥坛,春花似錦蓬蝶、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽楼肪。三九已至袄膏,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間凤藏,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留雹洗,地道東北人。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓卧波,卻偏偏與公主長得像时肿,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子港粱,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,678評論 2 354

推薦閱讀更多精彩內(nèi)容