1. 添加Swagger2来在线自动生成接口的文档+测试功能
1.1 什么是Swagger2
Swagger2是一款通过我们添加的注解来对方法进行说明,来自动生成项目的在线api接口文档的web服务。
1.2 添加Swagger2依赖
在build.gradle中添加1
2compile("io.springfox:springfox-swagger2:2.6.1")
compile("io.springfox:springfox-swagger-ui:2.6.1")
1.3 创建Swagger2配置文件
新建文件SwaggerConfigurer1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24@Configuration
@EnableSwagger2
public class SwaggerConfigurer {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("cn.guluwa"))//注意替换包名
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Demo接口文档")
.description("个人主页:https://guluwa.github.io")
.termsOfServiceUrl("https://guluwa.github.io")
.contact(new Contact("咕噜娃", "https://guluwa.github.io", null))
.version("1.0")
.build();
}
}
1.4 修改Controller,添加API注解
1 | @RestController |
1.5 修改WebConfigurer
1 | @Override |
1.6 接口测试
浏览器输入localhost:8080/swagger-ui.html我们可以看到如下页面
/img1.png)
1.7 修改语言
在resourece目录下创建\META-INF\resourece目录,创建swagger-ui.html
1 | <!DOCTYPE html> |
2. 添加PageHelper分页查询功能
2.1 什么是PageHelper
PageHelper是一款好用的开源免费的Mybatis第三方物理分页插件
2.2 添加Swagger2依赖
compile("com.github.pagehelper:pagehelper-spring-boot-starter:1.2.5")
2.3 添加PageHelper配置
在application.properties中添加
1 | logging.level.com.example.demo.dao=DEBUG |
2.4 修改文件
UserInfoMapper.xml
1 | <select id="selectAll" resultMap="BaseResultMap"> |
UserInfoMapper1
List<UserInfo> selectAll();
UserInfoService
1 | PageInfo<UserInfo> selectAll(Integer page,Integer size); |
UserInfoServiceImpl
1 | @Override |
UserInfoController
1 | @ApiOperation(value = "查询用户", notes = "分页查询用户所有") |
2.5 功能测试,结果如下图所示
/img2.png)