idea mybatis配置log4j打印sql语句的示例
在IDEA中配置log4j,步骤很简单
1.在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.编写mybatis-config.xml文件
<?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>
3.编写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.在扫描mapper的xml文件中
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com.wocus.wine/dao/*Mapper.xml"/> <!-- 配置log4j--> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean>
注意:IDEA中的日志输出在output中,如果需要筛选,快捷键Ctrl+F
【拓展】
“细粒度”控制:Log4j打印出MyBatis中仅仅单个Mapper的配置。
<!-- 下面是通过配置log4j2,仅仅打印出单个mapper的SQL语句的配置--> <logger name="com.beebank.dao.iface.UserMapper"> <level>DEBUG</level> </logger>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)