1. 程式人生 > 程式設計 >Mybaits 實現列印sql語句的程式碼

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為了解決這一問題,使用了介面卡模式。

Mybaits 實現列印sql語句的程式碼

介面卡的實現一般是讓介面卡實現或者繼承目標,並且內部持有一個適配者的引用。這樣呼叫目標物件方法,實際上是呼叫適配者的方法。

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,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,queryStack);
  return stmt;
 } else if ("prepareCall".equals(method.getName())) {
  if (isDebugEnabled()) {
  debug(" Preparing: " + removeBreakingWhitespace((String) params[0]),queryStack);
  return stmt;
 } else if ("createStatement".equals(method.getName())) {
  Statement stmt = (Statement) method.invoke(connection,params);
  stmt = StatementLogger.newInstance(stmt,queryStack);
  return stmt;
 } else {
  return method.invoke(connection,params);
 }
 } catch (Throwable t) {
 throw ExceptionUtil.unwrapThrowable(t);
 }
}

可以看到,mybatis在裡面還可以更具情況建立代理類。代理類又一次被代理,這也是mybatis喜歡的程式設計方式,比如外掛也是代理類再次被代理,來實現多個外掛並行。

到此這篇關於Mybaits 實現列印sql語句的程式碼的文章就介紹到這了,更多相關Mybaits 列印sql語句內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!