Mybatis日志模块之日志打印的实现原理

在日志模块中有BaseJdbcLogger、ConnectionLogger、PreparedStatementLogger、ResultSetLogger、StatementLogger通过动态代理的方式打印在不同的位置打印日志。几个相关的类图如下:
在这里插入图片描述

  • BaseJdbcLogger是所有日志的增强类,用于记录jdbc那些方法需要增强,保存运行期间的sql参数信息。
  • ConnectionLogger负责打印连接信息和sql语句。对Connection做增强,如果是调用prepareStatement、prepareCall、createStatement的方法,打印要执行的sql语句并返回prepareStatement的代理对象,让prepareStatement也具备日志能力,打印参数。
  • PreparedStatementLogger负责对 PrepareStatement 对象增强,增强如下:
  1. 增强 PreparedStatement 的 setxxx 方法将参数设置到 columnMap、columnNames、columnValues,为打印参数做好准备。
  2. 增强PreparedStatement的executexxx方法,当执行时打印日志。
  3. 如果是查询,则增强resultSet方法,当返回值时,打印返回数据。如果是更新,增强getUpdateCount方法,打印被修改更新的行数,并返回更新行数。
  • ResultSetLogger负责打印结果集数据。
那么mybatis在哪里加入日志主体的呢?

在mybatis中,Executor是访问数据库的组件,日志功能是在Executor中嵌入的,org.apache.ibatis.executor.SimpleExecutor类的prepareStatement方法

 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你的 MyBatis 日志打印 SQL 语句,可以按照以下步骤进行排查: 1. 确认配置文件中是否开启了日志记录,并且日志级别设置为 DEBUG。 示例配置: ```xml <configuration> <settings> <setting name="logImpl" value="LOG4J"/> </settings> <typeAliases> <typeAlias alias="Author" type="domain.blog.Author"/> <typeAlias alias="Blog" type="domain.blog.Blog"/> <typeAlias alias="Comment" type="domain.blog.Comment"/> </typeAliases> <mappers> <mapper resource="org/mybatis/example/BlogMapper.xml"/> </mappers> <plugins> <plugin interceptor="org.mybatis.example.ExamplePlugin"> <property name="pluginProperty" value="100"/> </plugin> </plugins> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="org/mybatis/example/BlogMapper.xml"/> </mappers> <logging> <logImpl>LOG4J</logImpl> <level>DEBUG</level> </logging> </configuration> ``` 2. 确认是否使用了正确的日志实现。 MyBatis 支持多种日志实现,如 Log4j、Log4j2、JDK Logging、Commons Logging 等。确保使用了你所设置的日志实现,并且该日志实现的配置文件中也开启了 DEBUG 级别的日志记录。 3. 确认是否使用了正确的日志记录器名称。 如果你使用的是 Log4j,那么你需要在配置文件中指定正确的日志记录器名称。例如,下面的配置文件指定了用于 MyBatis 的记录器名称: ```xml <configuration> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/> </layout> </appender> <logger name="org.mybatis" additivity="false"> <level value="DEBUG"/> <appender-ref ref="STDOUT"/> </logger> </log4j:configuration> </configuration> ``` 4. 确认是否使用了正确的日志引入方式。 在使用 Maven 依赖管理工具时,需要确认是否将你所使用的日志实现和 MyBatis日志模块同时引入,例如: ```xml <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.30</version> </dependency> </dependencies> ``` 以上是 MyBatis 日志打印 SQL 的可能原因,你可以根据实际情况进行排查。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值