该功能依赖 p6spy
组件,完美的输出打印 SQL 及执行时长 3.1.0
以上版本
1. P6spy 依赖引入
# maven依赖
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>最新版本</version>
</dependency>
2. application.yml 配置
server.port= 9000
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.datasource.url=jdbc:mysql://localhost:3306/oms?serverTimezone=GMT%2B8
#spring.datasource.username=root
#spring.datasource.password=root
#mybatis-plus提供的驱动类,连接数据库
spring.datasource.driver-class-name=com.p6spy.engine.spy.P6SpyDriver
spring.datasource.url=jdbc:p6spy:mysql://localhost:3306/oms?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
3. spy.properties 配置
#3.2.1以上使用
modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory
#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
-------------------------------------------------说明------------------------------------------------------------
driver-class-name 为 p6spy 提供的驱动类
url 前缀为 jdbc:p6spy 跟着冒号为对应数据库连接地址
打印出sql为null,在excludecategories增加commit
批量操作不打印sql,去除excludecategories中的batch
批量操作打印重复的问题请使用MybatisPlusLogFactory (3.2.1新增)
该插件有性能损耗,不建议生产环境使用
-----------------------------------------------------------------------------------------------------------------
4. 启动项目,运行SQL语句
Consume Time:11 ms 2020-12-13 17:42:42
Execute SQL:SELECT COUNT(1) FROM mgr_user WHERE is_deleted = 0
Consume Time:1 ms 2020-12-13 17:42:42
Execute SQL:SELECT user_id,username,password,name,user_desc,employee_no,email,is_deleted,create_by,create_time,update_by,update_time FROM mgr_user WHERE is_deleted=0 LIMIT 10