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

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

相关推荐
老华带你飞1 小时前
机电公司管理小程序|基于微信小程序的机电公司管理小程序设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·微信小程序·小程序·机电公司管理小程序
拾忆,想起2 小时前
AMQP协议深度解析:消息队列背后的通信魔法
java·开发语言·spring boot·后端·spring cloud
不会吃萝卜的兔子2 小时前
springboot websocket 原理
spring boot·后端·websocket
Fency咖啡3 小时前
Spring Boot 内置日志框架 Logback - 以及 lombok 介绍
spring boot·后端·logback
ptc学习者3 小时前
oracle logwr,ckpt,dbwn 如何协同工作的
数据库·sql
西岭千秋雪_4 小时前
RAG核心特性:ETL
数据仓库·人工智能·spring boot·ai编程·etl
卷Java4 小时前
小程序前端功能更新说明
java·前端·spring boot·微信小程序·小程序·uni-app
卷Java4 小时前
小程序原生导航栏返回键实现
spring boot·云原生·微信小程序·uni-app
Lin_Aries_04218 小时前
基于 CI/CD(Jenkins)将 Spring Boot 应用自动部署到 Kubernetes 集群
spring boot·ci/cd·docker·容器·自动化·jenkins
咖啡Beans8 小时前
SseEmitter + WebClient + Flux实现SSE事件流推送
java·spring boot·flux