mybatis本身没有提供日志的实现,引入的是第三方组件。mybatis支持多个第三方日志插件,优先级由低到高为slf4J、commonsLoging、Log4J2、Log4J和JdkLog。
mybatis中有一个LogFactory,获取log的工厂类,在工程类中可以回去对应的日志实现。分析工程类,可以发现mybatis如何来选择log
1
2
3
4
5
6
7
|
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); } } |
关于logConstructor的加载如下
1
2
3
4
5
6
7
8
|
static { tryImplementation(LogFactory::useSlf4jLogging); tryImplementation(LogFactory::useCommonsLogging); tryImplementation(LogFactory::useLog4J2Logging); tryImplementation(LogFactory::useLog4JLogging); tryImplementation(LogFactory::useJdkLogging); tryImplementation(LogFactory::useNoLogging); } |
1
2
3
4
5
6
7
8
9
|
private static void tryImplementation(Runnable runnable) { if (logConstructor == null ) { try { runnable.run(); } catch (Throwable t) { // ignore } } } |
在 tryImplementation ,中会设置mybatis使用的log类型。把引用的log设置到logConstructor中后,后续其他类型的log也不会再加载。所以在mybatis中优先级由低到高为slf4J、commonsLoging、Log4J2、Log4J和JdkLog。感觉也是属于SPI的一种实现方式,不同的是各种类型的第三方日志,无法形成一个统一的接口。故此,mybatis为了解决这一问题,使用了适配器模式。
适配器的实现一般是让适配器实现或者继承目标,并且内部持有一个适配者的引用。这样调用目标对象方法,实际上是调用适配者的方法。
mybatis 又是如何把这log,用起来的。根据mybatis的习惯,应该会使用代理模式,来打印这个日志。 举例查询的语句查看,根据MapperProxy,查到最后查询的语句
1
2
3
4
5
6
7
8
9
10
11
|
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); } } |
1
2
3
4
5
6
7
|
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; } |
1
2
3
4
5
6
7
8
|
protected Connection getConnection(Log statementLog) throws SQLException { Connection connection = transaction.getConnection(); if (statementLog.isDebugEnabled()) { return ConnectionLogger.newInstance(connection, statementLog, queryStack); } else { return connection; } } |
到此处可以看到mybatis在获取连接的时候,会根据日志的打印级别来判断是否会创建一个代理类。到这里就基本可以猜到,在代理类中,mybatis会去打印这个sql的语句
1
2
3
4
5
|
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的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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在里面还可以更具情况创建代理类。代理类又一次被代理,这也是mybatis喜欢的编程方式,比如插件也是代理类再次被代理,来实现多个插件并行。
到此这篇关于Mybaits 实现打印sql语句的代码的文章就介绍到这了,更多相关Mybaits 打印sql语句内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://juejin.im/post/5f0dbfed6fb9a07ec07b4888