在IDEA中配置log4j,步驟很簡單
1.在Maven中加入以下配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
< 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文件
1
2
3
4
5
6
7
8
9
10
|
<? 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文件
1
2
3
4
5
6
|
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文件中
1
2
3
4
5
6
7
8
|
<!-- 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
【拓展】
“細(xì)粒度”控制:Log4j打印出MyBatis中僅僅單個Mapper的配置。
1
2
3
4
|
<!-- 下面是通過配置log4j2,僅僅打印出單個mapper的SQL語句的配置--> < logger name = "com.beebank.dao.iface.UserMapper" > < level >DEBUG</ level > </ logger > |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.jianshu.com/p/8e4f648161e1