简介
p6spy-spring-boot-starter 通过 Spring Boot 自动配置 + 动态代理 + 约定式默认值 三重机制,将原本需要手动配置的 P6Spy 复杂流程简化为几行 YAML,是 Spring 生态 “习惯优于配置” 理念的典型实践。
- 自动代理:通过 DataSourceDecorator 动态包装所有数据源,无侵入拦截 SQL。
- 简化配置:基于 @ConfigurationProperties 将复杂配置映射为 YAML/Properties,如 log-format 和 log-filter。
- 默认优化:内置 Slf4j 日志输出和智能过滤(如空 SQL 处理),开箱即用。
- 无缝集成:深度适配 Spring Boot 和多数据源场景,自动兼容主流连接池(HikariCP/Tomcat JDBC)。
依赖引入
<properties><p6spy-spring-boot-starter.version>1.11.0</p6spy-spring-boot-starter.version>
</properties><dependency><groupId>com.github.gavlyukovskiy</groupId><artifactId>p6spy-spring-boot-starter</artifactId><version>${p6spy-spring-boot-starter.version}</version>
</dependency>
增加环境配置
application.yaml
# p6spy sql打印
decorator:datasource:p6spy:log-format: "[executionTime]:%(executionTime) ms [sql]:%(sqlSingleLine)\n"log-filter:pattern: ^(?!(.*QRTZ_|^$)).+$ # 只记录不包含 QRTZ_ 和空 的 SQL