温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Mybaits如何实现打印sql语句

发布时间:2020-07-16 14:26:55 来源:亿速云 阅读:176 作者:小猪 栏目:开发技术

这篇文章主要为大家展示了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, 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语句的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI