包結(jié)構(gòu)
數(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ù)具體描述”)
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());
? ? }
}