idea mybatis configures log4j to print an example of an sql statement
- 2021-01-14 05:52:03
- OfStack
To configure log4j in IDEA, the steps are simple
1. Add the following configuration to Maven
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.6</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.5.6</version>
</dependency>
2. Prepare mybatis-config.xml 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>
<!-- Print query statement -->
<setting name="logImpl" value="LOG4J" />
</settings>
</configuration>
3. Preparation of log4j.properties
log4j.rootLogger=error, Console
log4j.logger.com.wocus.wine.dao=debug
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
4. In the xml file scanned by mapper
<!-- spring and MyBatis Perfect integration, no need mybatis The configuration mapping file for -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- Automatic scanning mapping.xml file -->
<property name="mapperLocations" value="classpath:com.wocus.wine/dao/*Mapper.xml"/>
<!-- configuration log4j-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
Note: Log output in IDEA is in output. If you need to filter, shortcut Ctrl+F
【 extend 】
"Fine grained" control: Log4j prints out the configuration of only a single Mapper in MyBatis.
<!-- The following is by configuration log4j2 , just print out a single mapper the SQL Statement configuration -->
<logger name="com.beebank.dao.iface.UserMapper">
<level>DEBUG</level>
</logger>