log4j+mybatis打印数据库日志

参考文献:一:http://blog.csdn.net/rangqiwei/article/details/50825090

     二:http://www.mybatis.org/mybatis-3/zh/logging.html

要求:打印出数据库的sql语句日志

这里要强调的是,这里的数据库配置是直接放在了spring-mybatis.xml中,所以mybatis-config.xml是为了打印数据库日志单独加的

下面是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>

下面是spring-mybatis.xml的内容:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
">

     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
          <!-- 用户名-->  
          <property name="user" value="${jdbc.username}"/>  
          <!-- 用户密码-->  
          <property name="password" value="${jdbc.password}"/>  
          <property name="driverClass" value="${jdbc.driver_class}"/>  
          <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>  
  
           <!--连接池中保留的最大连接数。默认值: 15 -->   
          <property name="maxPoolSize" value="20"/>  
          <!-- 连接池中保留的最小连接数,默认为:3-->  
          <property name="minPoolSize" value="2"/>  
          <!-- 初始化连接池中的连接数,取值应在minPoolSize与maxPoolSize之间,默认为3-->  
          <property name="initialPoolSize" value="2"/>  
  
          <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。默认值: 0 -->   
          <property name="maxIdleTime" value="60"/>  
            
          <!-- 当连接池连接耗尽时,客户端调用getConnection()后等待获取新连接的时间,超时后将抛出SQLException,如设为0则无限期等待。单位毫秒。默认: 0 -->   
          <property name="checkoutTimeout" value="3000"/>  
            
          <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。默认值: 3 -->   
          <property name="acquireIncrement" value="2"/>  
  
         <!--定义在从数据库获取新连接失败后重复尝试的次数。默认值: 30 ;小于等于0表示无限次-->   
          <property name="acquireRetryAttempts" value="0"/>  
  
          <!--重新尝试的时间间隔,默认为:1000毫秒-->   
          <property name="acquireRetryDelay" value="1000" />  
  
          <!--关闭连接时,是否提交未提交的事务,默认为false,即关闭连接,回滚未提交的事务 -->   
          <property name="autoCommitOnClose" value="false"/>
  
          <!--如果为false,则获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常,但是数据源仍有效保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。默认: false-->   
          <property name="breakAfterAcquireFailure" value="false"/>  
  
          <!--每60秒检查所有连接池中的空闲连接。默认值: 0,不检查 -->   
          <property name="idleConnectionTestPeriod" value="60"/>
          <!--c3p0全局的PreparedStatements缓存的大小。如果maxStatements与maxStatementsPerConnection均为0,则缓存不生效,只要有一个不为0,则语句的缓存就能生效。如果默认值: 0-->   
          <property name="maxStatements" value="100"/>
          <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。默认值: 0 -->   
          <property name="maxStatementsPerConnection" value="0"/>
     </bean> 
   
    <!--配置sqlSessionFactory 并读取mybatis的一些配置-->
    <bean name = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean">
      <property name = "dataSource" ref = "dataSource"></property>
      <property name="configLocation" value="classpath:mybatis-config.xml"></property><!-- 配置mybatis的日志记录 -->  
      <property name="mapperLocations" value="classpath:com/quicksure/mobile/mapping/*.xml"/>
    </bean>
    
    <!--
    自动扫描 将Mapper接口生成代理注入到Spring
    -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.quicksure.mobile.dao" />
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
  </bean>
    
    <!--
    配置事物
    -->
   <bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <property name = "dataSource" ref = "dataSource"></property>
   </bean>
    
    <!--
    <tx:annotation-driven transaction-manager = "transactionManager"/>
    -->
  
   <!--
    事物的具体内容
   -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="load*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>

<!--
    定义一个切面,在定义的切面上加入事物
   -->
  <aop:config>
    <aop:pointcut id="transactionPointcut" expression="execution(* com.quicksure.mobile.dms*.*.*(..))" />
    <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
   </aop:config>
</beans>

下面是log4j.properties内容:

### set log levels ###
log4j.rootLogger = INFO , C , D , E 

# MyBatis logging configuration...
log4j.logger.com.quicksure.mobile.dao=DEBUG    上色部分是作用范围,具体详情请看链接二
### console ### log4j.appender.C = org.apache.log4j.ConsoleAppender log4j.appender.C.Target = System.out log4j.appender.C.layout = org.apache.log4j.PatternLayout log4j.appender.C.layout.ConversionPattern = [quicksureMobile][%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n ### log file ### log4j.appender.D = org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File = ../logs/quickSureMobileServer.log log4j.appender.D.Append = true log4j.appender.D.Threshold = INFO log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = [quicksureMobile][%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n ### exception ### log4j.appender.E = org.apache.log4j.DailyRollingFileAppender log4j.appender.E.File = ../logs/quickSureMobileServerError.log log4j.appender.E.Append = true log4j.appender.E.Threshold = ERROR log4j.appender.E.layout = org.apache.log4j.PatternLayout log4j.appender.E.layout.ConversionPattern =[quicksureMobile][%p] [%-d{yyyy-MM-dd HH\:mm\:ss}] %C.%M(%L) | %m%n

 

    原文作者:MyBatis
    原文地址: https://www.cnblogs.com/ldbangel/p/6236323.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞