技术框架对MyBatis调试信息的学习

MyBatis开启调试信息

在 Maven 工程的 resources/pom.xml 中导入 log4j 依赖包

<!--导入 log4j 日志依赖包-->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.7.5</version>
</dependency>

在 Maven 工程的 resources 创建 log4j.properties 文件

该文件是 log4j 日志框架的配置文件,如下:

log4j.rootLogger=DEBUG,A1
log4j.logger.org.apache=DEBUG
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n

如果你对 log4j 日志框架的使用不了解,可以看我的关于介绍 log4j 日志框架的系列博客文章。

重新执行 MyBatisDemo 结果如下:

2020-06-19 11:11:24,656 [main] [org.apache.ibatis.logging.LogFactory]-[DEBUG] Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
2020-06-19 11:11:24,681 [main] [org.apache.ibatis.datasource.pooled.PooledDataSource]-[DEBUG] PooledDataSource forcefully closed/removed all connections.
2020-06-19 11:11:24,682 [main] [org.apache.ibatis.datasource.pooled.PooledDataSource]-[DEBUG] PooledDataSource forcefully closed/removed all connections.
2020-06-19 11:11:24,682 [main] [org.apache.ibatis.datasource.pooled.PooledDataSource]-[DEBUG] PooledDataSource forcefully closed/removed all connections.
2020-06-19 11:11:24,682 [main] [org.apache.ibatis.datasource.pooled.PooledDataSource]-[DEBUG] PooledDataSource forcefully closed/removed all connections.
2020-06-19 11:11:24,882 [main] [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-[DEBUG] Opening JDBC Connection
2020-06-19 11:11:25,333 [main] [org.apache.ibatis.datasource.pooled.PooledDataSource]-[DEBUG] Created connection 1076496284.
2020-06-19 11:11:25,333 [main] [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-[DEBUG] Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@402a079c]
2020-06-19 11:11:25,337 [main] [mapper.UserMapper.selectUserByAge]-[DEBUG] ==>  Preparing: select * from tb_user where age > ? 
2020-06-19 11:11:25,405 [main] [mapper.UserMapper.selectUserByAge]-[DEBUG] ==> Parameters: 20(Integer)
2020-06-19 11:11:25,450 [main] [mapper.UserMapper.selectUserByAge]-[DEBUG] <==      Total: 2
[entity.UserEntity{id=1, userName='zs', password='123456', name='张三', age=22, sex=1, birthday=Sun Sep 02 00:00:00 IRKST 1990, created='2020-06-17 09:30:58.0', updated='2020-06-17 09:30:58.0'}, entity.UserEntity{id=2, userName='ls', password='123456', name='李四', age=24, sex=1, birthday=Sun Sep 05 00:00:00 IRKST 1993, created='2020-06-17 09:30:58.0', updated='2020-06-17 09:30:58.0'}]
2020-06-19 11:11:25,451 [main] [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-[DEBUG] Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@402a079c]
2020-06-19 11:11:25,452 [main] [org.apache.ibatis.transaction.jdbc.JdbcTransaction]-[DEBUG] Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@402a079c]
2020-06-19 11:11:25,452 [main] [org.apache.ibatis.datasource.pooled.PooledDataSource]-[DEBUG] Returned connection 1076496284 to pool.

Process finished with exit code 0

这样我们就开启了 MyBatis 框架的 log4j 调试信息开关,由于增加了 Debug 调试信息,打印的内容比之前就多很多。我们从这些调试信息中可以清晰的看到 MyBatis 框架内部的执行过程,非常有利于使用 MyBatis 出错时帮助分析和定位问题。

如果不想控制台打印这么多调试信息,可以修改 log4j 配置中的 DEBUG,将其改为 ERROR 即可,如下:

log4j.rootLogger=ERROR,A1
log4j.logger.org.apache=ERROR
posted @ 2024-11-30 20:56  BingBing爱化学-04044  阅读(4)  评论(0编辑  收藏  举报