2013-04-26 138 views
7

我使用Spring 3.1.1,MyBatis 3.1.1,MySQL 5.0.67。我的Spring配置低於:如何使用log4j打印SQL查詢結果日誌?

<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">  
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="${jdbc.url}" /> 
    <property name="username" value="${jdbc.username}" /> 
    <property name="password" value="${jdbc.password}" /> 
    <property name="validationQuery" value="select 1"/> 
    <property name="testWhileIdle" value="true"/> 
    <property name="timeBetweenEvictionRunsMillis" value="14400000"/>    
    <property name="testOnBorrow" value="false"/>  
</bean> 

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="configLocation" value="classpath:mybatis/myBatisConfig.xml"/> 
</bean> 

<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> 
    <constructor-arg ref="sqlSessionFactory"/> 
</bean> 

<bean id="transactionManager" 
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager" 
    p:dataSource-ref="dataSource"/> 

<tx:annotation-driven transaction-manager="transactionManager"/> 

而且log4.properties低於:

log4j.logger.org.springframework=DEBUG 
log4j.logger.org.apache=DEBUG 
log4j.logger.org.mybatis=DEBUG 

log4j.logger.java.sql=DEBUG 
log4j.logger.java.sql.Connection=DEBUG 
log4j.logger.java.sql.Statement=DEBUG 
log4j.logger.java.sql.PreparedStatement=DEBUG 
log4j.logger.java.sql.ResultSet=DEBUG 

有了這些配置,我可以看到這是執行的SQL查詢語句和參數,該查詢,但我不能查看查詢結果日誌。我的日誌是這樣的:

[org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession 
[org.mybatis.spring.SqlSessionUtils] - SqlSession    [[email protected]] was not registered for synchronization because synchronization is not active 
[org.springframework.jdbc.datasource.DataSourceUtils] - Fetching JDBC Connection from DataSource 
[org.mybatis.spring.transaction.SpringManagedTransaction] - JDBC Connection  [ProxyConnection[PooledConnection[[email protected]]]] will not be managed by Spring 
[java.sql.Connection] - ooo Using Connection  [ProxyConnection[PooledConnection[[email protected]]]] 
[java.sql.Connection] - ==> Preparing: SELECT col FROM table WHERE col1=? AND col2=? 
[java.sql.PreparedStatement] - ==> Parameters: 93(Integer), 4(Integer) 
[org.mybatis.spring.SqlSessionUtils] - Closing non transactional SqlSession  [[email protected]] 
[org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource 

有沒有辦法打印包括查詢結果的日誌?

回答

0

我什麼工作原理是:

log4j.logger.org.springframework.jdbc.core.StatementCreatorUtils = TRACE

這將打印入口像這樣的:

... 
TRACE SimpleAsyncTaskExecutor-1 org.springframework.jdbc.core.StatementCreatorUtils - Setting SQL statement parameter value: column index 1, parameter value [12345], value class [java.math.BigDecimal], SQL type 3 
TRACE SimpleAsyncTaskExecutor-1 org.springframework.jdbc.core.StatementCreatorUtils - Setting SQL statement parameter value: column index 2, parameter value [ADDRESS], value class [java.lang.String], SQL type 12 
TRACE SimpleAsyncTaskExecutor-1 org.springframework.jdbc.core.StatementCreatorUtils - Setting SQL statement parameter value: column index 3, parameter value [20130916], value class [java.math.BigDecimal], SQL type 3 
... 

在我的情況下,我使用的是運行在Spring Batch的應用JdbcBatchItemWriter內的查詢,WH我使用命名參數,所以它可能不是一個通用的解決方案。