且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用spring-mybatis进行Spring-boot - 如何强制它记录所有SQL查询

更新时间:2023-08-21 15:32:46

Spring启动使用logback作为默认日志记录Slf4j的提供者。 Ibatis内部日志工厂加载SLF4j作为首选记录器。您所要做的就是配置Spring启动记录器以发布ibatis映射器的日志消息。

Spring boot uses logback as default logging provider for Slf4j. Ibatis internal log factory loads the SLF4j as the first choice logger. All you have to do is configure your spring boot logger to publish log messages for ibatis mapper.

在启动应用程序属性中添加以下行。

Add the below lines in boot application properties.

logging.level.org.springframework=WARN
logging.level.com.spring.ibatis.UserMapper=DEBUG
logging.file=logs/spring-boot-logging.log

第二行是您定义日志条目的位置具有DEBUG日志级别的ibatis映射器。 com.spring.ibatis 是包, UserMapper 是样本映射器。

The second line is where you define the logging entry for ibatis mapper with DEBUG log level. com.spring.ibatis is package and the UserMapper is sample mapper.

以下日志将开始出现在控制台和spring-boot-logging文件中。这些是从 saveUser findByName 生成的日志消息 ApplicationTest class。

The following logs will start to appear in the console and in the spring-boot-logging file. These are the log messages generated from saveUser and findByName method of ApplicationTest class.

2016-12-19 22:07:06.358  INFO 7248 --- [main] com.spring.ibatis.ApplicationTest        : Started ApplicationTest in 3.048 seconds (JVM running for 4.209)
2016-12-19 22:07:06.424 DEBUG 7248 --- [main] com.spring.ibatis.UserMapper.saveUser    : ==>  Preparing: insert into users(name) values(?) 
2016-12-19 22:07:06.444 DEBUG 7248 --- [main] com.spring.ibatis.UserMapper.saveUser    : ==> Parameters: ibatis(String)
2016-12-19 22:07:06.445 DEBUG 7248 --- [main] com.spring.ibatis.UserMapper.saveUser    : <==    Updates: 1
2016-12-19 22:07:06.457 DEBUG 7248 --- [main] com.spring.ibatis.UserMapper.findByName  : ==>  Preparing: select name from users WHERE name=? 
2016-12-19 22:07:06.470 DEBUG 7248 --- [main]  com.spring.ibatis.UserMapper.findByName  : ==> Parameters: ibatis(String)
2016-12-19 22:07:06.504 DEBUG 7248 --- [main]  com.spring.ibatis.UserMapper.findByName  : <==      Total: 1

您当然可以配置任何您想要的记录器选项。如果需要,我可以轻松地为任何其他记录器添加一个示例。

You can of course configure any choice of logger you want. I can easily add an example for any other logger should you need.

您可以在
找到包含Junit测试用例的完整代码 https://github.com/saagar2000/ibatis