SQL分析与打印-p6spy组件

有性能消耗,只推荐在非生产环境下使用

SpringBoot3+MybatisPlus

https://baomidou.com/guides/p6spy/

MyBatis-Plus提供了SQL分析与打印的功能,通过集成p6spy组件,可以方便地输出SQL语句及其执行时长。本功能适用于MyBatis-Plus 3.1.0及以上版本。

对于Spring Boot项目,可以使用p6spy-spring-boot-starter来简化集成过程。

引入依赖

pom.xml

xml 复制代码
<dependency>
    <groupId>com.github.gavlyukovskiy</groupId>
    <artifactId>p6spy-spring-boot-starter</artifactId>
    <version>1.11.0</version>
</dependency>

配置

application.yml

yaml 复制代码
spring:
  datasource:
#    driver-class-name: com.mysql.cj.jdbc.Driver
#    url: jdbc:mysql://127.0.0.1:3308/springboot3-admin
    driver-class-name: com.p6spy.engine.spy.P6SpyDriver
    url: jdbc:p6spy:mysql://127.0.0.1:3306/springboot3-admin
    username: root
    password: 123456
    
decorator:
  datasource:
    p6spy:
      # 日志格式
      log-format: "[executionTime]:%(executionTime) ms  [sql]:%(sqlSingleLine)"

SpringBoot2+Mybatis

这个版本集成中,使用上面的配置不生效

yaml 复制代码
decorator:
 datasource:
   p6spy:
     # 日志格式
     log-format: "[executionTime]:%(executionTime) ms  [sql]:%(sqlSingleLine)"

需要添加 spy.properties 配置

引入依赖

pom.xml

xml 复制代码
<dependency>
    <groupId>com.github.gavlyukovskiy</groupId>
    <artifactId>p6spy-spring-boot-starter</artifactId>
    <version>1.11.0</version>
</dependency>

配置

application.yml

yaml 复制代码
spring:
  datasource:
#    driver-class-name: com.mysql.cj.jdbc.Driver
#    url: jdbc:mysql://127.0.0.1:3308/springboot3-admin
    driver-class-name: com.p6spy.engine.spy.P6SpyDriver
    url: jdbc:p6spy:mysql://127.0.0.1:3306/springboot3-admin
    username: root
    password: 123456

spy.properties

bash 复制代码
# spy.properties
appender=com.p6spy.engine.spy.appender.StdoutLogger
logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat
customLogMessageFormat=[executionTime]:%(executionTime) ms  [sql]:%(sqlSingleLine)
# 如果你希望将日志输出到文件,可以这样配置:
# appender=com.p6spy.engine.spy.appender.FileLogger
# logMessageFormat=com.p6spy.engine.spy.appender.SingleLineFormat
# file=spy.log

如有帮到你可以点赞,收藏或鼓励一下(^_^),谢谢,有什么疑问可以评论交流互相学习

相关推荐
allbs17 小时前
spring boot项目excel导出功能封装——4.导入
spring boot·后端·excel
q***428217 小时前
SpringCloud-持久层框架MyBatis Plus的使用与原理详解
spring·spring cloud·mybatis
l***775217 小时前
总结:Spring Boot 之spring.factories
java·spring boot·spring
2***s67218 小时前
给SQL server数据库表字段添加注释SQL,附修改、删除注释SQL及演示
数据库·sql·oracle
x***010619 小时前
springboot中配置logback-spring.xml
spring boot·spring·logback
w***42419 小时前
springboot使用logback自定义日志
java·spring boot·logback
北郭guo19 小时前
MyBatis框架讲解,工作原理、核心内容、如何实现【从浅入深】让你看完这篇文档对于MyBatis的理解更加深入
java·数据库·mybatis
庄宿正19 小时前
【Vue2+SpringBoot+SM2】Vue2 + Spring Boot 实现 SM2 双向非对称加密完整实战
java·spring boot·后端
A***F15719 小时前
使用 Spring Boot 实现图片上传
spring boot·后端·状态模式