Mybatis 實現(xiàn)打印sql語句的代碼
mybatis本身沒有提供日志的實現(xiàn),引入的是第三方組件。mybatis支持多個第三方日志插件,優(yōu)先級由低到高為slf4J、commonsLoging、Log4J2、Log4J和JdkLog。
mybatis中有一個LogFactory,獲取log的工廠類,在工程類中可以回去對應(yīng)的日志實現(xiàn)。分析工程類,可以發(fā)現(xiàn)mybatis如何來選擇log
public static Log getLog(String logger) { try { return logConstructor.newInstance(logger); } catch (Throwable t) { throw new LogException("Error creating logger for logger " + logger + ". Cause: " + t, t); } }
關(guān)于logConstructor的加載如下
static { tryImplementation(LogFactory::useSlf4jLogging); tryImplementation(LogFactory::useCommonsLogging); tryImplementation(LogFactory::useLog4J2Logging); tryImplementation(LogFactory::useLog4JLogging); tryImplementation(LogFactory::useJdkLogging); tryImplementation(LogFactory::useNoLogging); }
private static void tryImplementation(Runnable runnable) { if (logConstructor == null) { try { runnable.run(); } catch (Throwable t) { // ignore } } }
在 tryImplementation ,中會設(shè)置mybatis使用的log類型。把引用的log設(shè)置到logConstructor中后,后續(xù)其他類型的log也不會再加載。所以在mybatis中優(yōu)先級由低到高為slf4J、commonsLoging、Log4J2、Log4J和JdkLog。感覺也是屬于SPI的一種實現(xiàn)方式,不同的是各種類型的第三方日志,無法形成一個統(tǒng)一的接口。故此,mybatis為了解決這一問題,使用了適配器模式。
適配器的實現(xiàn)一般是讓適配器實現(xiàn)或者繼承目標(biāo),并且內(nèi)部持有一個適配者的引用。這樣調(diào)用目標(biāo)對象方法,實際上是調(diào)用適配者的方法。
mybatis 又是如何把這log,用起來的。根據(jù)mybatis的習(xí)慣,應(yīng)該會使用代理模式,來打印這個日志。 舉例查詢的語句查看,根據(jù)MapperProxy,查到最后查詢的語句
public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { Statement stmt = null; try { Configuration configuration = ms.getConfiguration(); StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql); stmt = prepareStatement(handler, ms.getStatementLog()); return handler.query(stmt, resultHandler); } finally { closeStatement(stmt); } }
private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException { Statement stmt; Connection connection = getConnection(statementLog); stmt = handler.prepare(connection, transaction.getTimeout()); handler.parameterize(stmt); return stmt; }
protected Connection getConnection(Log statementLog) throws SQLException { Connection connection = transaction.getConnection(); if (statementLog.isDebugEnabled()) { return ConnectionLogger.newInstance(connection, statementLog, queryStack); } else { return connection; } }
到此處可以看到mybatis在獲取連接的時候,會根據(jù)日志的打印級別來判斷是否會創(chuàng)建一個代理類。到這里就基本可以猜到,在代理類中,mybatis會去打印這個sql的語句
public static Connection newInstance(Connection conn, Log statementLog, int queryStack) { InvocationHandler handler = new ConnectionLogger(conn, statementLog, queryStack); ClassLoader cl = Connection.class.getClassLoader(); return (Connection) Proxy.newProxyInstance(cl, new Class[]{Connection.class}, handler); }
用 ConnectionLogger 來舉例,看到里面的invoke的方法
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable { try { if (Object.class.equals(method.getDeclaringClass())) { return method.invoke(this, params); } if ("prepareStatement".equals(method.getName())) { if (isDebugEnabled()) { debug(" Preparing: " + removeBreakingWhitespace((String) params[0]), true); } PreparedStatement stmt = (PreparedStatement) method.invoke(connection, params); stmt = PreparedStatementLogger.newInstance(stmt, statementLog, queryStack); return stmt; } else if ("prepareCall".equals(method.getName())) { if (isDebugEnabled()) { debug(" Preparing: " + removeBreakingWhitespace((String) params[0]), true); } PreparedStatement stmt = (PreparedStatement) method.invoke(connection, params); stmt = PreparedStatementLogger.newInstance(stmt, statementLog, queryStack); return stmt; } else if ("createStatement".equals(method.getName())) { Statement stmt = (Statement) method.invoke(connection, params); stmt = StatementLogger.newInstance(stmt, statementLog, queryStack); return stmt; } else { return method.invoke(connection, params); } } catch (Throwable t) { throw ExceptionUtil.unwrapThrowable(t); } }
可以看到,mybatis在里面還可以更具情況創(chuàng)建代理類。代理類又一次被代理,這也是mybatis喜歡的編程方式,比如插件也是代理類再次被代理,來實現(xiàn)多個插件并行。
到此這篇關(guān)于Mybatis 實現(xiàn)打印sql語句的代碼的文章就介紹到這了,更多相關(guān)Mybatis 打印sql語句內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring 中事務(wù)注解@Transactional與trycatch的使用
這篇文章主要介紹了spring 中事務(wù)注解@Transactional與trycatch的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06springboot接口參數(shù)校驗JSR303的實現(xiàn)
本文主要介紹了springboot接口參數(shù)校驗JSR303的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08java遞歸實現(xiàn)復(fù)制一個文件夾下所有文件功能
這篇文章主要介紹了java遞歸實現(xiàn)復(fù)制一個文件夾下所有文件功能n次,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08Java繼承extends與super關(guān)鍵字詳解
本篇文章給大家詳細(xì)講述了Java繼承extends與super關(guān)鍵字的相關(guān)知識點,需要的朋友們可以參考學(xué)習(xí)下。2018-02-02Java中使用Lambda表達(dá)式和函數(shù)編程示例
這篇文章介紹了Java中使用Lambda表達(dá)式和函數(shù)編程示例,該文章會演示多個示列,分別是變量聲明上下文中的lambda、return語句上下文中的lambda、賦值上下文中的lambda、lambda在數(shù)組初始值設(shè)定項上下文中的用法等等,需要的朋友可以參考一下2021-10-10Java for循環(huán)Map集合優(yōu)化實現(xiàn)解析
這篇文章主要介紹了Java for循環(huán)Map集合優(yōu)化實現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01Java中的 FilterInputStream簡介_動力節(jié)點Java學(xué)院整理
FilterInputStream 的作用是用來“封裝其它的輸入流,并為它們提供額外的功能”。接下來通過本文給大家分享Java中的 FilterInputStream簡介,感興趣的朋友一起學(xué)習(xí)吧2017-05-05