springboot使用p6spy打印完整SQL

本文介绍了如何在SpringBoot应用中使用P6Spy来打印完整的SQL语句,以解决ORM框架默认日志打印占位符的问题。通过引入P6Spy依赖,更改数据库驱动和URL,配置spy.properties文件以及自定义日志格式,最终能够实现清晰的SQL执行日志。
摘要由CSDN通过智能技术生成

我们在使用orm框架时,一般默认提供的日志打印方式打印出来的sql中间的占位符并没有被替换掉:
例如mybatis:
使用日志打印

logging.level.com.wk.entity.dao=debug
2018-07-14 14:38:39.497 DEBUG 4088 --- [nio-8082-exec-1] c.k.e.e.d.A.selectByPrimaryKey           : ==>  Preparing: select id, score from a_innodb_test where id = ? 
2018-07-14 14:38:39.520 DEBUG 4088 --- [nio-8082-exec-1] c.k.e.e.d.A.selectByPrimaryKey           : ==> Parameters: 1(Integer)

打印出来的sql带有占位符,并不利于调试。

P6Spy 是针对数据库访问操作的动态监测框架(为开源项目,项目首 页:www.p6spy.com)它使得数据库数据可无缝截取和操纵,而不必对现有应用程序的代码作任何修改。P6Spy 分发包包括P6Log,它是一 个可记录任何 Java 应用程序的所有JDBC事务的应用程序。其配置完成使用时,可以进行数据访问性能的监测。
使用p6spy我们可以直接查看到数据库执行的sql而不是预编译带?的SQL。

首先我们导入p6spy的依赖:

    // https://mvnrepository.com/artifact/p6spy/p6spy
    compile group: 'p6spy', name: 'p6spy', version: '3.7.0'

然后修改数据库连接驱动和url:

spring.datasource.driver-class-name=com.p6spy.engine.spy.P6SpyDriver
spring.datasource.url=jdbc:p6spy:mysql://localhost:3306/test

数据库驱动将com.mysql.jdbc.Driver替换为com.p6spy.engine.spy.P6SpyDriver,然后在url中的按上面的格式加入p6spy即可。
再在资源目录下加入spy.properties配置文件,内容如下:

###
# #%L
# P6Spy
# %%
# Copyright (C) 2013 P6Spy
# %%
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#      http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# #L%
###
#################################################################
# P6Spy Options File                                            #
# See documentation for detailed instructions                   #
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值