不灭的焱

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2023-11-12 20:45:36  修改时间:2024-05-03 22:08:13  分类:Java框架/系统  编辑

简介: 在开发的过程中控制台打印sql的查询条件都是?,运行起来很麻烦,在这帮助大家获得到完整的执行SQL。

第一步:引入依赖

<!-- p6spy -->
<dependency>
    <groupId>p6spy</groupId>
    <artifactId>p6spy</artifactId>
    <version>3.9.1</version>
</dependency>

第二步:MySQL驱动连接配置加上jdbc:p6spy代理

spring:
  datasource:
    driver-class-name: com.p6spy.engine.spy.P6SpyDriver
    url: jdbc:p6spy:mysql://106.52.xxx.137:53306/albert_test?useUnicode=true&useSSL=false&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&rewriteBatchedStatements=true&autoReconnect=true&serverTimezone=Asia/Shanghai&useInformationSchema=true
    username: albert_user_test
    password: ycCX0uw@76zz
    type: com.zaxxer.hikari.HikariDataSource
    hikari:
      minimum-idle: 2
      maximum-pool-size: 10
      auto-commit: true
      connection-test-query: select 1

第三步:在resources目录下,放置配置文件 spy.properties

module.log=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory

# 自定义日志打印
logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat
# 使用日志系统记录 sql
appender=com.p6spy.engine.spy.appender.Slf4JLogger
# 是否开启日志过滤 默认false,这项配置是否生效前提是配置了 include/exclude/sqlexpression
filter=true
# 过滤 Log 时所排除的表名列表,以逗号分隔
exclude=QRTZ_SCHEDULER_STATE,QRTZ_TRIGGERS,QRTZ_LOCKS,QRTZ_FIRED_TRIGGERS,dual
# 配置记录 Log 例外,可去掉的结果集有error,info,batch,debug,statement,commit,rollback,result,resultset.
excludecategories=info,debug,result,commit,resultset
# 设置使用 p6spy driver来做代理
deregisterdrivers=true
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
databaseDialectTimestampFormat=yyyy-MM-dd HH:mm:ss
# 实际驱动(可多个)
#driverlist=org.h2.Driver
driverlist=com.mysql.cj.jdbc.Driver
# 是否开启慢SQL记录
outagedetection=true
# 慢SQL记录标准 2 秒
outagedetectioninterval=2
# 取消JDBC URL前缀
useprefix=true


# 自定义日志配置
# 可用的变量为:
#   %(connectionId)            connection id
#   %(currentTime)             当前时间
#   %(executionTime)           执行耗时
#   %(category)                执行分组
#   %(effectiveSql)            提交的SQL 换行
#   %(effectiveSqlSingleLine)  提交的SQL 不换行显示
#   %(sql)                     执行的真实SQL语句,已替换占位
#   %(sqlSingleLine)           执行的真实SQL语句,已替换占位 不换行显示
customLogMessageFormat=[%(currentTime)] [%(category)-%(connectionId)] [execute time: %(executionTime) ms] execute sql:\n%(sqlSingleLine)

 

 

参考:https://blog.csdn.net/whk_15502266662/article/details/126866189