一、简介
起因
我们在日常的后端Java开发中,你是否碰到过以下这些场景:
(1)产品经理急匆匆过来,来帮我找下这个页面的XXX数据
(2)测试开发在测试环境,测试时,需要和后端频繁交流使用那些表,用来构造测试数据
(3)数据开发处理数据写入表中,页面访问与预期不符时,数据开发需要知道执行了那些SQL
(4)正式环境运行,接口发生SQL异常、访问缓慢,需要定位SQL
通常的做法是从前端页面获得接口地址,搜索接口地址对应后端项目的执行方法,找到对应Mybatis的Mapper.xml中SQL,如果参数处理复杂,还可能需要人脑过代码或测试模拟执行,这个过程重复而乏味,我们可以用代码让这个过程自动、简单、高效起来,slog-spring-boot的作用就是如此,让我们可以直接在接口响应中获得SQL数据。
概念
可支持SQL执行日志获取,基于Springboot项目可自动集成,简单快速定位SQL问题
特征
- 支持SpringMvc、Mybatis环境 — 通过SpringMvc过滤器、Mybaits拦截器获得SQL语句、SQL参数、执行时间
- 支持Http返回Json — 对返回格式是Json形式的接口,自动添加slog字段,返回执行SQL集合
- 安全性配置 — 支持不同环境不同模式,模式枚举:无密码模式、关闭、有密码模式
- Spring Boot项目可快速集成 — 通过@EnableSlog注解开启
二、使用
Maven Setting
SpringMvc、Mybatis环境
<dependency>
<groupId>com.v2hoping</groupId>
<artifactId>slog</artifactId>
<version>1.1.0</version>
</dependency>
Spring Boot环境
<dependency>
<groupId>com.v2hoping</groupId>
<artifactId>slog-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
配置
名称 | 类型 | 说明 |
---|---|---|
slog.active | true/false | 是否启用,默认不启动 |
slog.passwordActive | true/false | 是否密码模式,默认否 |
slog.password | 字符串 | 密码,为空则不使用密码 |
密码模式
密码模式必须通过请求头传递密码
请求头 | 类型 | 说明 |
---|---|---|
Slog-Pwd | 字符串 | 传递配置的密码 |
示例
第一步:确认环境
确认Maven已经引入SpringMvc、Mybatis、Springboot
第二步:Maven引入jar
<dependencies>
<dependency>
<groupId>com.v2hoping</groupId>
<artifactId>slog-spring-boot-starter</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
第三步:环境配置
建议测试环境开启,正式环境可以关闭或者选择密码模式开启。
@SpringBootApplication
@EnableSlog
public class ServletInitializer extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(ServletInitializer.class);
Map<String, Object> defaultProperties = new HashMap<>(1);
defaultProperties.put("druid.datasource.primary.base.package");
defaultProperties.put("registerErrorPageFilter",false);
System.setProperty("spring.devtools.restart.enabled", "false");
application.setDefaultProperties(defaultProperties);
application.setWebEnvironment(true);
application.run(args);
}
}
无密码模式:
slog.active=true
密码模式:
slog.active=true
slog.passwordActive=true
slog.password=123456
第四步:http请求返回
不添加slog组件时
{
"codeNum": 0,
"success": true,
"value": {
"rr": [
{
"name": "医疗健康",
"rank": 1
},
{
"name": "企业服务",
"rank": 2
},
{
"name": "金融",
"rank": 3
}
],
"content": "一季度医疗健康、企业服务、金融"
}
}
添加slog组件后
{
"codeNum": 0,
"success": true,
"slog": [{
"param": "",
"sql": "select element_type_id,element_type from ad_ver",
"time": 3
},
{
"param": "701001(Integer), 1(Integer), 2019(Integer)",
"sql": "select financing_amount_rank from ad_in where platform_id=? and date_id = ? and year = ?",
"time": 3
}
],
"value": {
"rr": [{
"name": "医疗健康",
"rank": 1
},
{
"name": "企业服务",
"rank": 2
},
{
"name": "金融",
"rank": 3
}
],
"content": "一季度医疗健康、企业服务、金融"
}
}
三、结语
下一章我们会介绍如何实现该工具,开发的思路,以及项目中使用的关键技术点也会简略介绍。
github地址源码:https://github.com/v2hoping/slog-spring-boot