P6Spy是一個可以用來在應用程序中攔截和修改數據操作語句的開源框架。
通過P6Spy可以對SQL語句進行攔截,相當於一個SQL語句的記錄器,這樣我們可以用它來作相關的分析,比如性能分析。
springboot集成P6Spy
1.添加依賴
<dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifactId> <version>3.8.7</version> </dependency>
2.修改配置
原來的
spring.jpa.database=h2 spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=none spring.jpa.open-in-view=false spring.datasource.driverClassName=org.h2.Driver spring.datasource.url=jdbc:h2:./data/test spring.datasource.username=sa spring.datasource.password=123456
修改為
spring.jpa.database=h2 spring.jpa.hibernate.ddl-auto=none spring.jpa.open-in-view=false spring.datasource.driverClassName=com.p6spy.engine.spy.P6SpyDriver spring.datasource.url=jdbc:p6spy:h2:./data/test spring.datasource.username=sa spring.datasource.password=123456
3.P6Spy的配置
添加配置文件 spy.properties
# 單行日志 logMessageFormat=com.p6spy.engine.spy.appender.SingleLineFormat # 使用Slf4J記錄sql appender=com.p6spy.engine.spy.appender.Slf4JLogger # 是否開啟慢SQL記錄 outagedetection=true # 慢SQL記錄標准,單位秒 outagedetectioninterval=2 #日期格式 dateformat=yyyy-MM-dd HH:mm:ss
更多配置查看 https://p6spy.readthedocs.io/en/latest/configandusage.html
schema.sql
drop table users if exists; drop table goods if exists; create table users ( id bigint auto_increment, name varchar(255), create_time timestamp, primary key (id) ); create table goods ( id bigint auto_increment, name varchar(255), price bigint, create_time timestamp, update_time timestamp, primary key (id) );
data.sql
insert into users (name, create_time) values ('Lili', now()); insert into users (name, create_time) values ('Fiona', now()); insert into goods (name, price, create_time, update_time) values ('bag', 2000, now(), now()); insert into goods (name, price, create_time, update_time) values ('bottole', 2500, now(), now());
啟動項目
控制台輸出p6spy的部分
2020-04-01 10:05:53.193 INFO 1064 --- [ main] p6spy : 2020-04-01 10:05:53|4|statement|connection 0|url jdbc:p6spy:h2:./data/test|drop table users if exists|drop table users if exists
2020-04-01 10:05:53.194 INFO 1064 --- [ main] p6spy : 2020-04-01 10:05:53|0|statement|connection 0|url jdbc:p6spy:h2:./data/test|drop table goods if exists|drop table goods if exists
2020-04-01 10:05:53.198 INFO 1064 --- [ main] p6spy : 2020-04-01 10:05:53|3|statement|connection 0|url jdbc:p6spy:h2:./data/test|create table users ( id bigint auto_increment, name varchar(255), create_time timestamp, primary key (id) )|create table users ( id bigint auto_increment, name varchar(255), create_time timestamp, primary key (id) )
2020-04-01 10:05:53.199 INFO 1064 --- [ main] p6spy : 2020-04-01 10:05:53|0|statement|connection 0|url jdbc:p6spy:h2:./data/test|create table goods ( id bigint auto_increment, name varchar(255), price bigint, create_time timestamp, update_time timestamp, primary key (id) )|create table goods ( id bigint auto_increment, name varchar(255), price bigint, create_time timestamp, update_time timestamp, primary key (id) )
2020-04-01 10:05:53.216 INFO 1064 --- [ main] p6spy : 2020-04-01 10:05:53|8|statement|connection 0|url jdbc:p6spy:h2:./data/test|insert into users (name, create_time) values ('Lili', now())|insert into users (name, create_time) values ('Lili', now())
2020-04-01 10:05:53.216 INFO 1064 --- [ main] p6spy : 2020-04-01 10:05:53|0|statement|connection 0|url jdbc:p6spy:h2:./data/test|insert into users (name, create_time) values ('Fiona', now())|insert into users (name, create_time) values ('Fiona', now())
2020-04-01 10:05:53.217 INFO 1064 --- [ main] p6spy : 2020-04-01 10:05:53|0|statement|connection 0|url jdbc:p6spy:h2:./data/test|insert into goods (name, price, create_time, update_time) values ('bag', 2000, now(), now())|insert into goods (name, price, create_time, update_time) values ('bag', 2000, now(), now())
2020-04-01 10:05:53.217 INFO 1064 --- [ main] p6spy : 2020-04-01 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())
這樣在運行項目的時就可以在控制台中看到具體的sql語句了,從而檢查沒有錯誤
注:
p6spy配合slf4j+log4j使用,項目中需要整合logback