解決辦法
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