Why should we output SQL on the console?
Of course it is for convenience during development and debugging.
If there is a problem with a database-related operation, we can quickly troubleshoot the problem based on the output SQL statement.
Output information:
[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
The output content is quite scary. Database connection characters, username and password are all output, so be careful.
However, one disadvantage is that the SQL statements and parameters are output separately. If you want to copy them to the query tool for debugging, you have to fill in the parameters yourself, which is more troublesome.
Spring 4.0.2 + Spring MVC 4.0.2 + MyBatis 3.2.6
1. The spring-mybatis.xml file does not need to be modified;
2. In mybatis.xml, log4j is specified as the log implementation, which I do not need for actual testing.
<?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> <!-- 实际测试这个文件有没有都可以 -->
The value here can be SLF4J, Apache Commons Logging, Log4J2, Log4J, JDK logging (except Log4J2, Log4J, others are not verified), and will be searched in order
3. You also need to configure
<listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>
in web.xml or use the following (not tested)
<listener> <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class> </listener>
4. Finally configure log4j.properties
### Log4j配置 ### ### 与Spring结合需要在web.xml中指定此文件位置,并添加监听器 ### #定义log4j的输出级别和输出目的地(目的地可以自定义名称,和后面的对应) #[ level ] , appenderName1 , appenderName2 log4j.rootLogger=DEBUG,console,file #-----------------------------------# #1 定义日志输出目的地为控制台 log4j.appender.console = org.apache.log4j.ConsoleAppender log4j.appender.console.Target = System.out log4j.appender.console.Threshold=DEBUG ####可以灵活地指定日志输出格式,下面一行是指定具体的格式 ### #%c: 输出日志信息所属的类目,通常就是所在类的全名 #%m: 输出代码中指定的消息,产生的日志具体信息 #%n: 输出一个回车换行符,Windows平台为"/r/n",Unix平台为"/n"输出日志信息换行 log4j.appender.console.layout = org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=[%c]-%m%n #-----------------------------------# #2 文件大小到达指定尺寸的时候产生一个新的文件 log4j.appender.file = org.apache.log4j.RollingFileAppender #日志文件输出目录 log4j.appender.file.File=log/tibet.log #定义文件最大大小 log4j.appender.file.MaxFileSize=10mb ###输出日志信息### #最低级别 log4j.appender.file.Threshold=ERROR log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n #-----------------------------------# #3 druid log4j.logger.druid.sql=INFO log4j.logger.druid.sql.DataSource=info log4j.logger.druid.sql.Connection=info log4j.logger.druid.sql.Statement=info log4j.logger.druid.sql.ResultSet=info #4 mybatis 显示SQL语句部分 log4j.logger.org.mybatis=DEBUG #log4j.logger.cn.tibet.cas.dao=DEBUG #log4j.logger.org.mybatis.common.jdbc.SimpleDataSource=DEBUG #log4j.logger.org.mybatis.common.jdbc.ScriptRunner=DEBUG #log4j.logger.org.mybatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG #log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.ResultSet=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
The above is the detailed content of MyBatis3 uses log4j to output SQL on the console (database operation skills). For more information, please follow other related articles on the PHP Chinese website!