Spring+Junit+Mybatis打印jdbc log 操作日志

释放双眼,带上耳机,听听看~!

pom依赖:

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
1        <dependency>
2            <groupId>org.slf4j</groupId>
3            <artifactId>log4j-over-slf4j</artifactId>
4            <version>1.7.21</version>
5            <scope>test</scope>
6        </dependency>
7        <dependency>
8            <groupId>junit</groupId>
9            <artifactId>junit</artifactId>
10            <version>4.12</version>
11            <scope>test</scope>
12        </dependency><dependency>
13            <groupId>org.mybatis</groupId>
14            <artifactId>mybatis-spring</artifactId>
15            <version>1.2.2</version>
16        </dependency>
17
18        <dependency>
19            <groupId>org.mybatis.caches</groupId>
20            <artifactId>mybatis-redis</artifactId>
21            <version>1.0.0-beta1</version>
22        </dependency>
23        <dependency>
24            <groupId>org.mybatis</groupId>
25            <artifactId>mybatis</artifactId>
26            <version>3.2.8</version>
27        </dependency>
28       <dependency>
29            <groupId>org.springframework</groupId>
30            <artifactId>spring-test</artifactId>
31            <version>4.1.3.RELEASE</version>
32        </dependency>
33
34        <dependency>
35            <groupId>commons-logging</groupId>
36            <artifactId>commons-logging</artifactId>
37            <version>1.2</version>
38            <scope>test</scope>
39        </dependency>
40<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
41        <dependency>
42            <groupId>ch.qos.logback</groupId>
43            <artifactId>logback-classic</artifactId>
44            <version>1.2.3</version>
45            <scope>test</scope>
46        </dependency>
47
48
49        <dependency>
50            <groupId>org.springframework</groupId>
51            <artifactId>spring-context</artifactId>
52            <exclusions>
53                <!-- Exclude Commons Logging in favor of SLF4j -->
54                <exclusion>
55                    <groupId>commons-logging</groupId>
56                    <artifactId>commons-logging</artifactId>
57                </exclusion>
58            </exclusions>
59            <version>4.1.3.RELEASE</version>
60        </dependency> 
61

 在junit测试类上添加注解

如:


1
2
3
4
5
1@RunWith(JUnit4ClassRunner.class) //此处为自定义类 代码如后所示
2@ContextConfiguration(locations = { "classpath*:dao-context.xml" })
3//@Transactional
4public class BaseDaoTest{...}
5

1
2
3
4
5
6
7
8
9
10
11
12
13
1public class JUnit4ClassRunner extends SpringJUnit4ClassRunner {
2   static {
3      try {
4         Log4jConfigurer.initLogging("classpath*:log4j.properties");
5      } catch (FileNotFoundException ex) {
6         System.err.println("Cannot Initialize log4j");
7      }
8   }
9   public JUnit4ClassRunner(Class<?> clazz) throws InitializationError {
10      super(clazz);
11   }
12}
13

 

在classpath下添加配置文件
log4j.properties内容如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
1# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
2# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.
3log4j.rootLogger=debug,stdout,PRODUCT
4
5log4j.appender.stdout=org.apache.log4j.ConsoleAppender
6
7log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
8
9log4j.appender.PRODUCT=org.apache.log4j.DailyRollingFileAppender
10log4j.appender.PRODUCT.File=/data/logs/kaola-jdbc.log
11log4j.appender.PRODUCT.Append=true
12log4j.appender.PRODUCT.DatePattern='.'yyyy-MM-dd
13log4j.appender.PRODUCT.Encoding=UTF-8
14log4j.appender.PRODUCT.layout=org.apache.log4j.PatternLayout
15log4j.appender.PRODUCT.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
16
17log4j.logger.com.ibatis=DEBUG
18log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
19log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
20log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
21log4j.logger.java.sql.Connection=DEBUG
22log4j.logger.java.sql.Statement=DEBUG
23log4j.logger.java.sql.PreparedStatement=DEBUG
24log4j.logger.java.sql.ResultSet=INFO
25log4j.logger.org.apache=INFO
26

添加mybatis对jdbclog的支持:(此文件名为:mybatis-config.xml)


1
2
3
4
5
6
7
8
9
10
11
12
1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPEconfiguration
3PUBLIC"-//mybatis.org//DTD Config 3.0//EN"
4"http://mybatis.org/dtd/mybatis-3-config.dtd">
5<configuration>
6    <settings>
7        <!-- 打印查询语句 -->
8<setting name="logImpl" value="LOG4J" />
9        <setting name="logPrefix" value="com.*.dao."/>
10    </settings>
11</configuration>
12

最后在spring读取的context.xml中引入该配置即可.

 

给TA打赏
共{{data.count}}人
人已打赏
安全经验

职场中的那些话那些事

2021-9-24 20:41:29

安全经验

IM服务器的架构

2021-11-28 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索