通过上篇文章《Mybatis 框架日志相关源码分析(二)》了解了 Mybatis 通过工厂模式创建 Log 接口的实现类,那么拿到实现类之后, Mybatis 是如何输出日志的呢?本文将分析 Mybatis 框架的日志相关源码,了解 Mybatis 使用 JDBC 时,是通过何种方式输出日志。Mybatis 执行过程也可使用 JDBC 差不多,首先是要获取 Connection 对象。而获取此对象是通过 BaseExecutor#getConnection() 方法。protected Connection getConnection(Log statementLog) throws SQLException { Connection connection = transaction.getConnection(); if (statementLog.isDebugEnabled()) { return ConnectionLogger.newInstance(connection, statementLog, queryStack); } return con
通过上篇文章《Mybatis 框架日志相关源码分析(一)》了解了 Mybatis 框架通过适配器模式,来整合其它日志框架。而我们都知道要想使用 Log 接口,就需要有具体的实现类对象,那么 Mybatis 是如何创建具体的实现类对象呢?本文将分析 Mybatis 框架的日志相关源码,了解 Mybatis 如何创建日志实现类对象。Mybatis 在解析到配置中的具体日志 Value 内容时,不仅保存了对应的 Class<? extends Log> 属性,还有一行代码:LogFactory.useCustomLogging(this.logImpl):public void setLogImpl(Class<? extends Log> logImpl) { if (logImpl != null) { this.logImpl = logImpl; LogFactory.useCustomLogging(this.logImpl); } }LogFactory 这个类可谓是见名知意,Log 工厂,在设计模式中,这明显就是工厂模式。继续深入
日志是框架必不可少的一个部分,它有助于开发人员排除和发现问题。在 Mybatis 框架中,通过内置的日志工厂提供日志功能。内置日志工厂将会把日志工作委托给下面的实现之一:SLF4JApache Commons LoggingLog4j 2Log4j (3.5.9 起废弃)JDK logging本文将分析 Mybatis 框架的日志相关源码,了解适配器模式在日志源码中的应用。先从配置说起,配置 Mybatis 日志,是通过在 mybatis-config.xml 文件里面添加一项 setting 来使用:<configuration> <settings> ... <setting name="logImpl" value="LOG4J"/> ... </settings> </configuration>上面所示的 value 值为:LOG4J ,这是 Mybatis 可选的几个值之一,通过源码 Configuration#205 可知所有 value 可
此间少年
事以密成,语以泄败