(2)SQL监控:P6Spy
参考:
Spring Boot + Mybatis Plus3.1 实现 SQL 打印
使用P6Spy监控你的Spring boot数据库操作
引言
最近换了号称最快的HikariDataSource,由于没有了SQL监控,加之于Mybaits默认输出日志之拙计。遂用此物,与之相仿还有log4jdbc,比较活跃度后选择了P6Spy。
步骤
具体步骤比较简单,这里说下简单原理。P6Spy通过劫持JDBC驱动,在调用实际JDBC驱动前拦截调用的目标语,达到SQL语句日志记录的目的。
它包括P6Log和P6Outage两个模块。
- P6Log 用来拦截和记录任务应用程序的 JDBC 语句
- P6Outage 专门用来检测和记录超过配置条件里时间的 SQL 语句
应用P6Spy只需要 三步
1、添加依赖
2.替换你的JDBC Driver为com.p6spy.engine.spy.P6SpyDriver 、修改JDBC Url为jdbc:p6spy:xxxx
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
url: jdbc:p6spy:mysql://47.98.253.2:3306/master?characterEncoding=utf8&useSSL=false
3.配置spy.properties
module.log=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,batch,resultset
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 实际驱动可多个
#driverlist=org.h2.Driver
# 是否开启慢SQL记录
outagedetection=true
# 慢SQL记录标准 2 秒
outagedetectioninterval=2
# 自定义日志打印
logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat
customLogMessageFormat=%(currentTime) | SQL耗时: %(executionTime) ms | 连接信息: %(category)-%(connectionId) | 执行语句: %(sql)
4、效果如下
可以得出结论。mybatis默认开启一级缓存