Why output SQL on the console?
Of course, it is for the convenience of 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, [email protected], 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 physical 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 terrible. The database connection characters and username and password are output, so be careful.
However, one thing is bad is that SQL statements and parameters are output separately. If you want to copy them into the query tool to debug, you have to fill in the parameters yourself, which is more troublesome.
My project environment
Spring 4.0.2 + Spring MVC 4.0.2 + MyBatis 3.2.6
Method 1: Use standard log output
This method is relatively simple. You only need to configure the configuration file of MyBatis with relevant properties, and you don't need to put a log4j.properties file.
<?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="STDOUT_LOGGING"/> </settings> </configuration>
Key statements:
<setting name="logImpl" value="STDOUT_LOGGING"/>
Specifies which logging implementation MyBatis should use. If this setting does not exist, logging implementations are automatically discovered.
Method 2: Use log4j log output
1. The spring-mybatis.xml file does not need to be modified;
2. In mybatis.xml, specify to use log4j as the log implementation, which I don't need to actually test.
<?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> <!-- In fact, you can test whether this file is available-->
The value values here can be SLF4J, Apache Commons Logging, Log4J2, Log4J, JDK logging (except Log4J2, Log4J, other unverified), and will be searched in order.
3. Configuring is also required in web.xml
<listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>
Or use the following (not tested)
<listener> <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class> </listener>
4. Finally configure log4j.properties
### Log4j configuration#### ### In combination with Spring, you need to specify this file location in web.xml and add a listener #### #Define the output level and output destination of log4j (the destination can be customized, and the corresponding to the subsequent) #[ level ] , appenderName1 , appenderName2 log4j.rootLogger=DEBUG,console,file #--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- log4j.appender.console.Threshold=DEBUG #####You can flexibly specify the log output format. The following line specifies the specific format. ############ The category to which the output log information belongs is usually the full name of the class #%m: The message specified in the code, the specific log information generated #%n: Output a carriage return line break, Windows platform is "/r/n", and Unix platform is "/n" to output log information newline log4j.appender.console.layout = org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=[%c]-%m%n #-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- When the file size reaches the specified size, a new file is generated log4j.appender.file = org.apache.log4j.RollingFileAppender #Log file output directory log4j.appender.file.File=log/tibet.log #Define the maximum file size log4j.appender.file.MaxFileSize=10mb ###Output log information### #Lowest level 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 #----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Display SQL statement part 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 Comparison of the two ways
1. The configuration using standard log output is simple, while the configuration using log4j log output is relatively complex;
2. log4j has powerful functions and relatively fine control granularity;
When specified as "STDOUT_LOGGING" in the configuration file, which log implementation is actually used? Apache Commons Logging or JDK logging?
None of them, it is actually System.out.pringln.
Updated on 2017-09-14
Some friends have reported that they will not output SQL statements after configuring the second method. Please note that in the log4j.properties file:
log4j.appender.console.Threshold=DEBUG
Is it consistent with this example?
Please pay attention to the details. Friends who are just starting to study can first use the configuration provided in this example and then customize it in a personal way.
In addition, according to the configuration above, I will output a large amount of information outside of SQL statements. A friend tried to output only SQL statements, parameters, and results.
Thank you here and share with you:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.