idea mybatis配置log4j列印sql語句的樣本,mybatislog4j
在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 #Consolelog4j.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>
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。