How can I print SQL query result log with log4j?

35,399

Two ways:

  • log4j.logger.java.sql.ResultSet=TRACE

Or use the namespaces to set logging. This is the only logging method in mybatis 3.2

http://mybatis.github.io/mybatis-3/logging.html

Share:
35,399
firia2000
Author by

firia2000

I'm programmer but not good at math. I'm studying math for myself.

Updated on September 20, 2020

Comments

  • firia2000
    firia2000 over 3 years

    I'm using Spring 3.1.1, MyBatis 3.1.1, MySQL 5.0.67. My Spring configuration is below:

    <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"/>
    

    And log4.properties is below:

    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
    

    With these configuration, I can see SQL query statement which is executed and parameters to that query but I can't see query result log. My log is like this:

    [org.mybatis.spring.SqlSessionUtils] - Creating a new SqlSession
    [org.mybatis.spring.SqlSessionUtils] - SqlSession             [org.apache.ibatis.session.defaults.DefaultSqlSession@4ccdd1f] 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[com.mysql.jdbc.JDBC4Connection@3cfde82]]] will not be managed by Spring
    [java.sql.Connection] - ooo Using Connection     [ProxyConnection[PooledConnection[com.mysql.jdbc.JDBC4Connection@3cfde82]]]
    [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     [org.apache.ibatis.session.defaults.DefaultSqlSession@4ccdd1f]
    [org.springframework.jdbc.datasource.DataSourceUtils] - Returning JDBC Connection to DataSource
    

    Is there any way to print log including query result?