Thx
http://ppyymm000.blog.163.com/blog/static/2834658420121141142231/
http://blog.163.com/ng_0125@126/blog/static/170162360201392105823927/
---------------------------------------分割线是我的矜持---------------------------------------
很多时候需要查看Mybatis执行的sql语句,以及执行情况。我们需要在log4j的配置文件中,添加一段配置。
log4j.rootLogger=DEBUG, sql
log4j.appender.sql=org.apache.log4j.DailyRollingFileAppender
log4j.appender.sql.DatePattern='.'yyyyMMdd
log4j.appender.sql.File=E:logsql.log
log4j.appender.sql.layout=org.apache.log4j.PatternLayout
log4j.appender.sql.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} | %m%n
log4j.logger.org.apache.ibatis=DEBUG
log4j.logger.org.apache.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.org.apache.common.jdbc.ScriptRunner=DEBUG
log4j.logger.org.apache.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
不了解Mybatis内部关于这个log是怎么配置的。经过测试,Mybatis的appender用的是和根级rootLogger用的是同一个
据说 # MyBatis logging configuration ... 这里可以针对package、interface、method或Mapper的namespace进行日志的输出
使用上面的配置已经可以满足我的需求了
2015-04-03 17:02:33 | Acquired Connection [jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf-8, UserName=root@192.168.0.11, MySQL Connector Java] for JDBC transaction 2015-04-03 17:02:33 | Setting JDBC Connection [jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf-8, UserName=root@192.168.0.11, MySQL Connector Java] read-only 2015-04-03 17:02:33 | Switching JDBC Connection [jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf-8, UserName=root@192.168.0.11, MySQL Connector Java] to manual commit 2015-04-03 17:02:33 | Creating a new SqlSession 2015-04-03 17:02:33 | Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@10da1d8] 2015-04-03 17:02:34 | JDBC Connection [jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf-8, UserName=root@192.168.0.11, MySQL Connector Java] will be managed by Spring 2015-04-03 17:02:34 | ==> Preparing: select id from user WHERE login_name = ? and password = ? 2015-04-03 17:02:34 | ==> Parameters: hehe(String), F379EAF3C831B04DE153469D1BEC345E(String) 2015-04-03 17:02:34 | <== Total: 1 2015-04-03 17:02:34 | Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@10da1d8] 2015-04-03 17:02:34 | Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@10da1d8] 2015-04-03 17:02:34 | Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@10da1d8] 2015-04-03 17:02:34 | Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@10da1d8] 2015-04-03 17:02:34 | Initiating transaction commit 2015-04-03 17:02:34 | Committing JDBC transaction on Connection [jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf-8, UserName=root@192.168.0.11, MySQL Connector Java] 2015-04-03 17:02:34 | Resetting read-only flag of JDBC Connection [jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf-8, UserName=root@192.168.0.11, MySQL Connector Java] 2015-04-03 17:02:34 | Releasing JDBC Connection [jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf-8, UserName=root@192.168.0.11, MySQL Connector Java] after transaction 2015-04-03 17:02:34 | Returning JDBC Connection to DataSource