解决办法
mapper 包日志级别设置为DEBUG
原因:
- Mybatis是用Mapper类的全路径名,作为logger的名称的。
- Mybatis中,打印SQL语句的日志级别是DEBUG,而打印SQL执行结果集的日志级别是TRACE。
下边是笔者的打印日志
1 [INFO ] [17:47:14] com.fx.interceptors.ScreenHostInterceptor.preHandle - Request Method Is:public java.lang.Object com.fx.controller.LoginController.showCurrentUser(javax.servlet.http.HttpServletRequest) 2 [INFO ] [17:47:14] com.fx.interceptors.ScreenHostInterceptor.preHandle - The Request Method Is:GET 3 [DEBUG] [17:47:18] com.fx.mapper.MyTestMapper.findAll.debug - ==> Preparing: select * from my_test; 4 [DEBUG] [17:47:19] com.fx.mapper.MyTestMapper.findAll.debug - ==> Parameters: 5 [TRACE] [17:47:19] com.fx.mapper.MyTestMapper.findAll.trace - <== Columns: id, name, age, gender 6 [TRACE] [17:47:19] com.fx.mapper.MyTestMapper.findAll.trace - <== Row: 1, 测试事务mybatis, null, male 7 [TRACE] [17:47:19] com.fx.mapper.MyTestMapper.findAll.trace - <== Row: 2, 测试事务jpa, null, female 8 [TRACE] [17:47:19] com.fx.mapper.MyTestMapper.findAll.trace - <== Row: 4, tom, 4, male 9 [TRACE] [17:47:19] com.fx.mapper.MyTestMapper.findAll.trace - <== Row: 5, hony, 5, male 10 [DEBUG] [17:47:19] com.fx.mapper.MyTestMapper.findAll.debug - <== Total: 4
第3,4行为DEBUG级别日志 6到10行为TRACE级别日志
比如mapper类所在包名为com.fx.mapper
如果只是想输出sql语句
则在log4j.properties文件里配置
log4j.logger.com.fx.mapper=DEBUG
如果既需要打印sql语句也需要打印sql执行结果集则修改日志级别为TRACE
log4j.logger.com.fx.mapper=TRACE
参考资源 https://bglmmz.iteye.com/blog/2154563