

当然是为了开发调试的时候方便了。
如果一个 数据库相关的操作出现了问题,我们可以根据输出的SQL语句快速排查问题。
输出的信息:
[org.mybatis.spring.SqlSessionUtils]-Creating a new SqlSession [org.mybatis.spring.SqlSessionUtils]-SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@33290f98] was not registered for synchronization because synchronization is not active [org.springframework.jdbc.datasource.DataSourceUtils]-Fetching JDBC Connection from DataSource [org.mybatis.spring.transaction.SpringManagedTransaction]-JDBC Connection [jdbc:mysql://rds.aliyuncs.com:3306/yyyy?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull, UserName=223323@222.222.xxx.xxx, MySQL Connector Java] will not be managed by Spring [Datenumber.pageSelect]-==> Preparing: SELECT x.Id, DATE_FORMAT(x.`Date`, '%Y%m%d') `datestr`, x.befor_num, x.after_num, x.physician_id, y.department_id, y.clinic_id, y.true_name, y.avatar, y.title, y.telephone FROM datenumber x right join physician y on x.physician_id=y.id AND ( x.`Date` >= ? AND x.`Date` <= ? ) Where 1=1 AND y.clinic_id = ? ORDER BY x.Date ASC [Datenumber.pageSelect]-==> Parameters: 2017-3-28(String), 2017-4-4(String), 1(Long) [Datenumber.pageSelect]-<== Total: 19
输出的内容比较可怕,数据库连接字符,用户名密码都输出来了,一定要小心。
不过有一点不好的是SQL语句和参数是分开输出的,想复制到查询工具中调试的话还得自己填写参数,比较麻烦。
Spring 4.0.2 + Spring MVC 4.0.2 + MyBatis 3.2.6
1、spring-mybatis.xml 文件不需要修改;
2、在mybatis.xml中,指定使用log4j为日志实现,这个我实际测试也不需要。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="logImpl" value="LOG4J"/> </settings> </configuration> <!-- 实际测试这个文件有没有都可以 -->
这里的value值可以是SLF4J、Apache Commons Logging、Log4J2、Log4J、JDK logging(除Log4J2、Log4J外,其他未验证),并会按顺序查找
3、在web.xml中也需要配置
<listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>
或者用下面的(未测试)
<listener> <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class> </listener>
4、最后配置log4j.properties
### Log4j配置 ### ### 与Spring结合需要在web.xml中指定此文件位置,并添加 ### #定义log4j的
