這篇文章主要為大家展示了Mybaits如何實現打印sql語句,內容簡而易懂,下面讓小編帶大家一起學習一下吧。
mybatis本身沒有提供日志的實現,引入的是第三方組件。mybatis支持多個第三方日志插件,優先級由低到高為slf4J、commonsLoging、Log4J2、Log4J和JdkLog。
mybatis中有一個LogFactory,獲取log的工廠類,在工程類中可以回去對應的日志實現。分析工程類,可以發現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);
}
}關于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 ,中會設置mybatis使用的log類型。把引用的log設置到logConstructor中后,后續其他類型的log也不會再加載。所以在mybatis中優先級由低到高為slf4J、commonsLoging、Log4J2、Log4J和JdkLog。感覺也是屬于SPI的一種實現方式,不同的是各種類型的第三方日志,無法形成一個統一的接口。故此,mybatis為了解決這一問題,使用了適配器模式。

適配器的實現一般是讓適配器實現或者繼承目標,并且內部持有一個適配者的引用。這樣調用目標對象方法,實際上是調用適配者的方法。
mybatis 又是如何把這log,用起來的。根據mybatis的習慣,應該會使用代理模式,來打印這個日志。 舉例查詢的語句查看,根據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在獲取連接的時候,會根據日志的打印級別來判斷是否會創建一個代理類。到這里就基本可以猜到,在代理類中,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在里面還可以更具情況創建代理類。代理類又一次被代理,這也是mybatis喜歡的編程方式,比如插件也是代理類再次被代理,來實現多個插件并行。
以上就是關于Mybaits如何實現打印sql語句的內容,如果你們有學習到知識或者技能,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。