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

阅读更多

pom依赖:

 

        
            org.slf4j
            log4j-over-slf4j
            1.7.21
            test
        
        
            junit
            junit
            4.12
            test
        
            org.mybatis
            mybatis-spring
            1.2.2
        

        
            org.mybatis.caches
            mybatis-redis
            1.0.0-beta1
        
        
            org.mybatis
            mybatis
            3.2.8
        
       
            org.springframework
            spring-test
            4.1.3.RELEASE
        

        
            commons-logging
            commons-logging
            1.2
            test
        

        
            ch.qos.logback
            logback-classic
            1.2.3
            test
        


        
            org.springframework
            spring-context
            
                
                
                    commons-logging
                    commons-logging
                
            
            4.1.3.RELEASE
         

 在junit测试类上添加注解

如:

@RunWith(JUnit4ClassRunner.class) //此处为自定义类 代码如后所示
@ContextConfiguration(locations = { "classpath*:dao-context.xml" })
//@Transactional
public class BaseDaoTest{...}
public class JUnit4ClassRunner extends SpringJUnit4ClassRunner {
   static {
      try {
         Log4jConfigurer.initLogging("classpath*:log4j.properties");
      } catch (FileNotFoundException ex) {
         System.err.println("Cannot Initialize log4j");
      }
   }
   public JUnit4ClassRunner(Class clazz) throws InitializationError {
      super(clazz);
   }
}

 

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

# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.
log4j.rootLogger=debug,stdout,PRODUCT
 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout

log4j.appender.PRODUCT=org.apache.log4j.DailyRollingFileAppender
log4j.appender.PRODUCT.File=/data/logs/kaola-jdbc.log
log4j.appender.PRODUCT.Append=true
log4j.appender.PRODUCT.DatePattern='.'yyyy-MM-dd
log4j.appender.PRODUCT.Encoding=UTF-8
log4j.appender.PRODUCT.layout=org.apache.log4j.PatternLayout
log4j.appender.PRODUCT.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n

log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO

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

xml version="1.0" encoding="UTF-8"?>
configuration
PUBLIC"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        
<setting name="logImpl" value="LOG4J" />
        <setting name="logPrefix" value="com.*.dao."/>
    settings>
configuration>

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

 

你可能感兴趣的:(Spring+Junit+Mybatis打印jdbc log 操作日志)