1.Maven refers to the jar package
The default mybatis can't print out the SQL log, which is not easy to view and debug. It needs to be combined with log4jdbc-log4j2 to complete the input of the debugging information of SQL.
Configure maven with pom.xml. Note that all three of the following are required
- <dependency>
- <groupId>org.bgee.log4jdbc-log4j2</groupId>
- <artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
- <version>1.16</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>1.7.13</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.7.13</version>
- </dependency>
2. Configuration information
log4jdbc.log4j2.prpperties
- log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
- ### Set up Logger Output Level and Output Destination ###Deug is more detailed. If set to info, the printed table data will not be displayed when it encounters a string. In addition, there is logfile.
- log4j.rootLogger=debug,stdout
- ### Export log information to console ###
- log4j.appender.stdout=org.apache.log4j.ConsoleAppender
- #log4j.appender.stdout.Target=System.err
- log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
- ### Export log information to files: jbit.log ###
- #log4j.appender.logfile=org.apache.log4j.FileAppender
- #log4j.appender.logfile.File=jbit.log
- #log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
- #log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n
- ###Display the SQL statement section
- #log4j.logger.com.mybatis=DEBUG
- #log4j.logger.com.mybatis.common.jdbc.SimpleDataSource=DEBUG
- #log4j.logger.com.mybatis.common.jdbc.ScriptRunner=DEBUG
- #log4j.logger.com.mybatis.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
Modify myBatis configuration file (MYSQL version)
- <!--No output SQL
- <property name="driver" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://..." />
- -->
- <!--output SQL-->
- <property name="driver" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy" />
- <property name="url" value="jdbc:log4jdbc:mysql://..." />
SQL server version:
- <!--No output sql Configuration
- <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />-->
- <property name="url" value="jdbc:sqlserver://..." />-->
- <!--output sql Configuration-->
- <property name="driver" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy" />
- <property name="url" value="jdbc:log4jdbc:sqlserver://..." />
Need attention
If log4j. rootLogger = info is configured, Console will not output strings in the SQL table. It must be log4j. rootLogger = DEBUG before Console can output strings.
If you print too many logs, it's easy to configure unnecessary log packages to log4j.logger. if you don't need them.
- log4j.logger.org.springframework=error
- log4j.rootLogger=DEBUG,Console
- #Console
- log4j.appender.Console=org.apache.log4j.ConsoleAppender
- log4j.appender.console.Target=System.out
- log4j.appender.Console.layout=org.apache.log4j.PatternLayout
- log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
- log4j.logger.org.apache=ERROR
- log4j.logger.org.mybatis=ERROR
- log4j.logger.org.springframework=ERROR
- #This need.
- log4j.logger.log4jdbc.debug=ERROR
- log4j.logger.com.gk.mapper=ERROR
- log4j.logger.jdbc.audit=ERROR
- log4j.logger.jdbc.resultset=ERROR
- #This print SQL statement is very important.
- log4j.logger.jdbc.sqlonly=DEBUG
- log4j.logger.jdbc.sqltiming=ERROR
- log4j.logger.jdbc.connection=FATAL