p6spy和p6spy-spring-boot-starter的SpringBoot3集成配置
官方文档参考:SQL分析与打印 | MyBatis-Plus
p6spy-spring-boot-starter集成SpringBoot3
引入依赖
<dependency><groupId>com.github.gavlyukovskiy</groupId><artifactId>p6spy-spring-boot-starter</artifactId><version>1.9.0</version>
</dependency>
禁用mybatis-plus日志输出
mybatis-plus:configuration:# 更详细的日志输出 会有性能损耗 org.apache.ibatis.logging.stdout.StdOutImpl# 关闭日志记录 (可单纯使用 p6spy 分析) org.apache.ibatis.logging.nologging.NoLoggingImpl# 默认日志输出 org.apache.ibatis.logging.slf4j.Slf4jImpllogImpl: org.apache.ibatis.logging.nologging.NoLoggingImpl
数据库准备
spring:datasource:type: com.zaxxer.hikari.HikariDataSourcedriverClassName: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://xxx.xxx.xxx.xxx:3306/数据库名?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=trueusername: xxxxpassword: xxxx
p6spy的yml配置
decorator:datasource:p6spy:# 使用默认的控制台输出logging: sysout# 日志格式log-format: "time:%(current_time) | executionTime:%(executionTime) ms | sql:%(sql)"
控制台结果展示
2025-08-11 12:02:30 [http-nio-8080-exec-1] INFO com.zaxxer.hikari.pool.HikariPool- HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@61f586cb
2025-08-11 12:02:30 [http-nio-8080-exec-1] INFO com.zaxxer.hikari.HikariDataSource- HikariPool-1 - Start completed.
time:%(current_time) | executionTime:35 ms | sql:SELECT user_id FROM sys_user
p6spy集成SpringBoot3
引入依赖
<dependency><groupId>p6spy</groupId><artifactId>p6spy</artifactId><version>3.9.1</version>
</dependency>
禁用mybatis-plus日志输出
mybatis-plus:configuration:# 更详细的日志输出 会有性能损耗 org.apache.ibatis.logging.stdout.StdOutImpl# 关闭日志记录 (可单纯使用 p6spy 分析) org.apache.ibatis.logging.nologging.NoLoggingImpl# 默认日志输出 org.apache.ibatis.logging.slf4j.Slf4jImpllogImpl: org.apache.ibatis.logging.nologging.NoLoggingImpl
数据库准备
spring:datasource:type: com.zaxxer.hikari.HikariDataSourcedriverClassName: com.p6spy.engine.spy.P6SpyDriver # 使用p6spy代理驱动url: jdbc:p6spy:mysql://xxx.xxx.xxx.xxx:3306/数据库名?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=trueusername: xxxxpassword: xxxx
配置spy.properties(位于资源文件夹resources下)
# 配置 P6Spy 模块列表,集成 MyBatis-Plus 日志工厂和超时检测
modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory# 日志消息格式,使用 MyBatis-Plus 提供的格式化器
logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger# 日志输出方式:使用 MyBatis-Plus 标准输出器(直接打印到控制台)
appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger
# 可选:使用 Slf4J 输出(需配合项目日志框架,如 Logback/Log4j2)
# appender=com.p6spy.engine.spy.appender.Slf4JLogger# 是否在日志前添加前缀
useprefix=true# 排除的日志类别(不输出这些类型的日志)
excludecategories=info,debug,result,commit,resultset# 日期时间格式
dateformat=yyyy-MM-dd HH:mm:ss# 数据库时间戳格式
databaseDialectTimestampFormat=yyyy-MM-dd HH:mm:ss# 真实数据库驱动类(P6Spy 会代理此驱动)
driverlist=com.mysql.cj.jdbc.Driver# 是否启用 SQL 执行超时检测
outagedetection=true# 超时检测间隔(单位:秒)
outagedetectioninterval=2# 是否启用 SQL 过滤功能
filter=true# 需要排除的 SQL 语句(不记录此 SQL 的日志)
exclude=SELECT 1
控制台结果展示
2025-08-11 12:26:10 [http-nio-8080-exec-1] INFO com.zaxxer.hikari.pool.HikariPool- HikariPool-1 - Added connection com.p6spy.engine.wrapper.ConnectionWrapper@57f7f158
2025-08-11 12:26:10 [http-nio-8080-exec-1] INFO com.zaxxer.hikari.HikariDataSource- HikariPool-1 - Start completed.Consume Time:36 ms 2025-08-11 12:26:10Execute SQL:SELECT user_id FROM sys_user