iBatis, spring, how to log the sql that is executed?

感情迁移 提交于 2019-11-30 04:49:41

Add the following to your log4j configuration (uncomment what you want to see).

# SqlMap logging configuration.
#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=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

Add this in your log4j.xml

<logger name="com.ibatis" additivity="false">
    <level value="debug"/>
    <appender-ref ref="APPENDER"/>
</logger>

If you are using Log4j as your logging framework you need to set mybatis to use log4j as its default logging tool. You can do this by setting it in the mybatis-config.xml like this,

<setting name="logImpl" value="LOG4J"/>

Or if you are not using mybatis-config.xml and just annotations, then you want to use

org.apache.ibatis.logging.LogFactory.useLog4JLogging();

before invoking any other mybatis methods to set the default logging implementation. Read More...

Use this configuration in your log4j.properties,

# Global logging configuration
log4j.rootLogger=INFO, stdout

# MyBatis mapper interfaces logging configuration...
log4j.logger.com.sample.mappers=DEBUG

# SqlMap logging configuration.
log4j.logger.org.mybatis.spring=DEBUG
log4j.logger.org.apache.ibatis=DEBUG

# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%p] %c - %m%n

If you are using log4j.xml configuration try this equivalent of the above,

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>

  <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%p] %c{1} - %m%n"/>
    </layout>
  </appender>

  <logger name="org.mybatis.spring" additivity="false">
    <level value="debug"/>
    <appender-ref ref="STDOUT"/>
  </logger>

  <logger name="com.sample.mappers">
    <level value="debug"/>
    <appender-ref ref="STDOUT"/>
  </logger>

  <!-- Other custom 3rd party logger configs -->

  <root>
    <priority value ="debug" />
    <appender-ref ref="STDOUT" />
  </root>

</log4j:configuration>

Either use properties file or xml file to configure log4j as above and place it in your classpath for this to work correctly.

Add this in your log4j

<logger name="java.sql" additivity="false">
    <level value="debug" />
    <appender-ref ref="console" /> </logger>

This will print out the sql as well as the output results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!