mybatis-plus配置控制臺打印完整帶參數(shù)SQL語句的實現(xiàn)
問題背景
通常我們開發(fā)的時候,需要聯(lián)合控制臺和Navicat/PLSQL等工具進(jìn)行語句的拼接檢查,如果只是輸出了一堆???,那么將極大降低我們的效率。因此我們需要輸出完整的SQL語句以便調(diào)試。
update 2020-July : 新增官方p6spy打印分析sql語句方案
解決方案(StdOutImpl)
請注意: 部分朋友反饋不生效,估計跟引入的包有一定關(guān)系,druid+mybatis-plus-boot-starter 就親測有用。請檢查是否有l(wèi)og4j相關(guān)實現(xiàn)類。
如果是application.yml
#by zhengkai.blog.csdn.net #mybatis-plus配置控制臺打印完整帶參數(shù)SQL語句 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
如果是application.properties,添加:
#mybatis-plus配置控制臺打印完整帶參數(shù)SQL語句 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
Mybatis內(nèi)置的日志工廠提供日志功能,具體的日志實現(xiàn)有以下幾種方式:
- SLF4J
- Apache Commons Logging
- Log4j 2
- Log4j
- JDK logging
- no logging
具體選擇哪個日志實現(xiàn)由MyBatis的LogFactory內(nèi)置日志工廠確定。它會使用最先找到的(按上文列舉的順序查找)。 如果一個都未找到,日志功能就會被禁用。
static { tryImplementation(LogFactory::useSlf4jLogging); tryImplementation(LogFactory::useCommonsLogging); tryImplementation(LogFactory::useLog4J2Logging); tryImplementation(LogFactory::useLog4JLogging); tryImplementation(LogFactory::useJdkLogging); tryImplementation(LogFactory::useNoLogging); }
不少應(yīng)用服務(wù)器的classpath中已經(jīng)包含Commons Logging,如Tomcat和WebShpere, 所以MyBatis會把它作為具體的日志實現(xiàn)。
記住這點非常重要。這意味著,在諸如 WebSphere的環(huán)境中——WebSphere提供了Commons Logging的私有實現(xiàn),你的Log4J配置將被忽略。
這種做法不免讓人悲摧,MyBatis怎么能忽略你的配置呢?事實上,因Commons Logging已經(jīng)存 在,按優(yōu)先級Log4J自然就被忽略了!
控制臺輸出
--- [ XNIO-1 task-12] c.s.cms.controller.IndexController : username-admin-password-123456-****
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@708e9ffd] was not registered for synchronization because synchronization is not active
--- [ XNIO-1 task-12] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@62b13210] will not be managed by Spring
==> Preparing: select * from user t where t.user_code='admin' and t.password='123456'
==> Parameters:
<== Columns: user_id, user_code, create_date, modify_date, user_name, password, status, role_id, department_id, major_id, classes_id, year
<== Row: 1, admin, 2020-02-15 22:14:32, 2020-02-18 23:38:51, Moshow K ZHENG, 123456, 1, 9, 1, 13, 113, 2020
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@708e9ffd]
解決方案2(手寫一個MybatisPlusOutImpl)
配置文件
mybatis-plus: configuration: # log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 改為自己寫的 log-impl: com.softdev.system.config.MybatisPlusOutImpl
java類 MybatisPlusOutImpl
package com.softdev.system.config; import org.apache.ibatis.logging.Log; /** * @Description MybatisPlusOutImpl,直接使用控制臺輸出日志 * @Author zhengkai.blog.csdn.net **/ public class MybatisPlusOutImpl implements Log { public MybatisPlusOutImpl(String clazz) { System.out.println("MybatisPlusOutImpl::"+clazz); } public boolean isDebugEnabled() { return true; } public boolean isTraceEnabled() { return true; } public void error(String s, Throwable e) { System.err.println(s); e.printStackTrace(System.err); } public void error(String s) { System.err.println("MybatisPlusOutImpl::"+s); } public void debug(String s) { System.out.println("MybatisPlusOutImpl::"+s); } public void trace(String s) { System.out.println("MybatisPlusOutImpl::"+s); } public void warn(String s) { System.out.println("MybatisPlusOutImpl::"+s); } }
官方解決方案p6spy
查看p6spy最新版本 ,請注意,該方案為侵入式的JDBC級方案。
pom.xml引入
<!-- https://mvnrepository.com/artifact/p6spy/p6spy --> <dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifactId> <version>3.9.1</version> </dependency>
這是yaml版本,還沒試過,待我實驗一下.
spring: datasource: driver-class-name: com.p6spy.engine.spy.P6SpyDriver url: jdbc:p6spy:h2:mem:test ...
這是官方提供的properties版本
#3.2.1以上使用 modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory #3.2.1以下使用或者不配置 #modulelist=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory # 自定義日志打印 logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger #日志輸出到控制臺 appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger # 使用日志系統(tǒng)記錄 sql #appender=com.p6spy.engine.spy.appender.Slf4JLogger # 設(shè)置 p6spy driver 代理 deregisterdrivers=true # 取消JDBC URL前綴 useprefix=true # 配置記錄 Log 例外,可去掉的結(jié)果集有error,info,batch,debug,statement,commit,rollback,result,resultset. excludecategories=info,debug,result,commit,resultset # 日期格式 dateformat=yyyy-MM-dd HH:mm:ss # 實際驅(qū)動可多個 #driverlist=org.h2.Driver # 是否開啟慢SQL記錄 outagedetection=true # 慢SQL記錄標(biāo)準(zhǔn) 2 秒 outagedetectioninterval=2
到此這篇關(guān)于mybatis-plus配置控制臺打印完整帶參數(shù)SQL語句的文章就介紹到這了,更多相關(guān)mybatis-plus 打印帶參數(shù)SQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Swing實現(xiàn)坦克大戰(zhàn)游戲
這篇文章主要介紹了Java Swing實現(xiàn)坦克大戰(zhàn)游戲,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有很大的幫助喲,需要的朋友可以參考下2021-05-05Java并發(fā)Map面試線程安全數(shù)據(jù)結(jié)構(gòu)全面分析
本文將探討如何在Java中有效地應(yīng)對這些挑戰(zhàn),介紹一種強(qiáng)大的工具并發(fā)Map,它能夠幫助您管理多線程環(huán)境下的共享數(shù)據(jù),確保數(shù)據(jù)的一致性和高性能,深入了解Java中的并發(fā)Map實現(xiàn),包括ConcurrentHashMap和ConcurrentSkipListMap,及相關(guān)知識點2023-09-09Java實現(xiàn)將數(shù)字日期翻譯成英文單詞的工具類實例
這篇文章主要介紹了Java實現(xiàn)將數(shù)字日期翻譯成英文單詞的工具類,結(jié)合完整實例形式分析了Java日期轉(zhuǎn)換與字符串操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2017-09-09淺談JAVA實現(xiàn)選擇排序,插入排序,冒泡排序,以及兩個有序數(shù)組的合并
這篇文章主要介紹了JAVA實現(xiàn)選擇排序,插入排序,冒泡排序,以及兩個有序數(shù)組的合并,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Spring Security中successHandler無效問題及解決
這篇文章主要介紹了Spring Security中successHandler無效問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08