◉◡◉ 您好,欢迎到访伊成个人站!

Springboot2.x整合p6spy

介绍p6spy

P6Spy 是针对数据库访问操作的动态监测框架,它使得数据库数据可无缝截取和操纵,而不必对现有应用程序的代码作任何修改。

P6Spy 分发包包括P6Log,它是一个可记录任何 Java 应用程序的所有JDBC事务的应用程序。其配置完成使用时,可以进行数据访问性能的监测。

P6Spy是一个开源项目,项目首页 www.p6spy.com [项目首页网站不可访问了!]

为何选择p6spy

在很多项目中我们都会用mybatis打印必要的sql语句,但是对应的参数都是用 ? 展示。而开发过程中如果我需要看完整的一条sql语句和具体参数值这就需要p6spy了。

1
2
==> Preparing: select order_id, order_code, org_remark, add_payment, activity_id, evaluate_score FROM tc_order WHERE 1=1 and category =? and order_status = ? and start_date <=? and third_order_id = ? 
2017-06-07 14:30:02,679 [executor-1] DEBUG com.xx.xx.dop2c.order.ext.TcOrderExtDao.queryForQueue - 47 - ==> Parameters: 253(Long), 30(Integer), 2017-06-07 14:30:02(String), 0(String)

使用p6spy打印的sql如下:

1
2
2021-08-05 10:05:53.217 INFO 1064 --- [ main] p6spy : 2021-08-05 10:05:53|0|statement|connection 0|url 
jdbc:p6spy:h2:./data/test|insert into goods (name, price, create_time, update_time) values ('bottole', 2500, now(), now())|insert into goods (name, price, create_time, update_time) values ('bottole', 2500, now(), now())

Springboot2整合p6spy

spring boot 版本:2.4.3

1.添加对应依赖

1
2
3
4
5
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>3.9.1</version>
</dependency>

2.修改数据库连接URL中加入p6spy

“伊成博客”

3.配置spy.properties
将spy.properties复制到项目的resources文件夹中。

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
#3.2.1以上使用
modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory
#modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory
#3.2.1以下使用或者不配置
#modulelist=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
# 自定义日志打印
logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger
#日志输出到控制台
appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger
# 使用日志系统记录 sql
#appender=com.p6spy.engine.spy.appender.Slf4JLogger
# 设置 p6spy driver 代理
deregisterdrivers=true
# 取消JDBC URL前缀
useprefix=true
# 配置记录 Log 例外,可去掉的结果集有error,info,batch,debug,statement,commit,rollback,result,resultset.
excludecategories=info,debug,result,commit,resultset
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 实际驱动可多个
#driverlist=org.h2.Driver
# 是否开启慢SQL记录
outagedetection=true
# 慢SQL记录标准 2 秒
outagedetectioninterval=2

这样就大功告成了,我们在运行项目的时候就可以在控制台中看到具体的sql语句了。

1
2
Consume Time:0 ms 2021-08-05 11:16:24
Execute SQL:SELECT * FROM tb_data_info WHERE datasource_id = 74

The end…

支付宝打赏 微信打赏