Home > Database > Mysql Tutorial > body text

MyBatis3 uses log4j to output SQL on the console (database operation skills)

PHPz
Release: 2017-04-02 17:20:32
Original
1568 people have browsed it

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
Copy after login

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.

My project environment

Spring 4.0.2 + Spring MVC 4.0.2 + MyBatis 3.2.6

Configuration steps

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.




    
        
    

Copy after login

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


     org.springframework.web.util.Log4jConfigListener
 
Copy after login

in web.xml or use the following (not tested)


        org.apache.logging.log4j.web.Log4jServletContextListener
    
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!