Springboot2.0从零开始搭建脚手架(三)-集成swagger2+lombok+fastjosn+MP分页

本文已被阅读过 Posted by Liao Can on 2019-04-20

Springboot2.0从零开始搭建脚手架(三)-集成swagger2+lombok+fastjosn+MybatisPlus分页插件+sqlj执行性能监控

添加swagger2+lombok+fastjosn的依赖

依赖的添加可以到对应组件的官方找,也可以直接去maven仓库直接搜。如果对应的依赖没有可以换一个仓库地址把依赖下到本地即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
      <!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
<!-- <scope>test</scope> -->
</dependency>
<!-- TypeBuilder -->
<dependency>
<groupId>com.github.ikidou</groupId>
<artifactId>TypeBuilder</artifactId>
<version>1.0</version>
<!-- <scope>test</scope> -->
</dependency>
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>

添加MP分页插件和sql执行监控配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.nqmysb.scaffold.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;

/**
* @author liaocan
* @since 2018-08-10
*/
@Configuration
@MapperScan("com.nqmysb.scaffold.mapper.*.*")
public class MybatisPlusConfig {

/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}


/**
* SQL执行效率插件
* 性能分析拦截器,用于输出每条 SQL 语句及其执行时间
*/
@Bean
// @Profile({"dev","pro"})// 设置 dev pro 环境开启
public PerformanceInterceptor performanceInterceptor() {
return new PerformanceInterceptor();
}
}

主配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
3.添加mapper地址
#服务端口
server:
port: 8080

# druid配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:oracle:thin:@//192.168.6.6:1521/orclpdb
username: nqmysb
password: nqmysb
druid:
initial-size: 2
max-active: 30
min-idle: 2
max-wait: 1234
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 5
filter:
stat:
enabled: true
filters: stat,wall,log4j
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
web-stat-filter:
enabled: true
stat-view-servlet:
enabled: true
reset-enable: false
aop-patterns: com.nqmysb.scaffold.user.service.*.*,com.nqmysb.scaffold.user.controller.*.*


mybatis-plus:
mapper-locations: classpath:/mapper/*/*Mapper.xml

swagger配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.nqmysb.scaffold.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2 // 启用Swagger2
public class Swagger2 {

@Bean
public Docket createRestApi() {// 创建API基本信息
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.nqmysb.scaffold.controller"))// 扫描该包下的所有需要在Swagger中展示的API,@ApiIgnore注解标注的除外
.paths(PathSelectors.any())
.build();
}

private ApiInfo apiInfo() {// 创建API的基本信息,这些信息会在Swagger UI中进行显示
return new ApiInfoBuilder()
.title("微服务后台脚手架工程API")// API 标题
.description("swagger2API清单")// API描述
.version("1.0")// 版本号
.build();
}
}

接口添加文档描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Controller
@RequestMapping("/user")
@Api(value="用户资源", tags="用户管理")
public class UserinfoController {

@Autowired
UserinfoServiceImpl userinfoServiceImpl;

@ApiOperation(value="查询用户信息", notes="通过用户输入的条件,查询满足条件的用户信息列表", httpMethod = "GET")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "userId", dataType = "string", required = false, value = "用户编号"),
@ApiImplicitParam(paramType = "query", name = "userName", dataType = "string", required = false, value = "用户账号,可以模糊查找"),
@ApiImplicitParam(paramType = "query", name = "fullName", dataType = "string", required = false, value = "用户姓名,可以模糊查找"),
@ApiImplicitParam(paramType = "query", name = "email", dataType = "string", required = false, value = "电子邮件,可以模糊查找"),
@ApiImplicitParam(paramType = "query", name = "mobile", dataType = "string", required = false, value = "联系方式,可以模糊查找"),
@ApiImplicitParam(paramType = "query", name = "status", dataType = "string", required = false, value = "用户状态,0:停用、1:正常")
})
@RequestMapping("/getUsers")
@ResponseBody
public ArrayList<Userinfo> getUsers() {
Wrapper<Userinfo> queryWrapper = null;
ArrayList<Userinfo> data = (ArrayList<Userinfo>) userinfoServiceImpl.list(queryWrapper);
return data;
}

访问swagger页面

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

开启logback日志

添加配置文件 logback.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="1800 seconds" debug="true">

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="com.nqmysb.scaffold.dao" level="DEBUG" />
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>

工程源代码地址

https://github.com/nqmysb/springboot-scaffold


支付宝打赏 微信打赏

赞赏一下