问题描述
今天调试的时候,希望将Mybatis的SQL语句输出到控制台,在网上找了许多方法,都没有生效,最后经过多种方法的尝试,解决了问题。
解决方法
我的框架是SpringBoot+Mybatis,日志采用log4j
第一步需要在application.yml中加入:
logging.level.com.xxx.xxx.mapper: debug
具体路径为:logging.level + 你的mapper包路径
第二步修改log4j.yml:
Loggers:
Root:
level: debug
将输出日志等级设置为debug
效果:
[] 2018-05-22 16:37:45 - [DEBUG] [SqlSessionUtils:97 getSqlSession] Creating a new SqlSession
[] 2018-05-22 16:37:46 - [DEBUG] [SqlSessionUtils:148 registerSessionHolder] SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6d57b264] was not registered for synchronization because synchronization is not active
[] 2018-05-22 16:37:46 - [DEBUG] [DataSourceUtils:110 doGetConnection] Fetching JDBC Connection from DataSource
[] 2018-05-22 16:37:46 - [DEBUG] [BaseJdbcLogger:181 debug] ==> Preparing: SELECT id, customer_phone, middle_number, unique_id, xxx, xxx, xxx, xxxx FROM xxxx_table WHERE xxx = ? and xx = ? limit 100
[] 2018-05-22 16:37:47 - [DEBUG] [BaseJdbcLogger:181 debug] ==> Parameters: (String), 877(String)
[] 2018-05-22 16:37:47 - [DEBUG] [BaseJdbcLogger:181 debug] <== Total: 0
[] 2018-05-22 16:37:47 - [DEBUG] [PreparedStatementPool:129 put] {conn-10005, pstmt-20000} enter cache
[] 2018-05-22 16:37:47 - [DEBUG] [SqlSessionUtils:191 closeSqlSession] Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6d57b264]