本文介绍了使用MyBatis和Spring在项目中记录SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有

<bean id="ABCSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="ABCDataSource" />
  <property name="mapperLocations">
      <list>
        <value>classpath:com/myco/dao/XYZMapper.xml</value>
       </list>
  </property>
<bean>

log4j.logger.java.sql.Connection=debug, stdout, abclog
log4j.logger.java.sql.PreparedStatement=debug, stdout, abclog
log4j.logger.java.sql=debug, stdout, abclog
log4j.logger.org.mybatis=debug, stdout, abclog
log4j.logger.org.apache.ibatis=debug, stdout, abclog

我在日志中运行Applicartion时看不到SQL查询想知道我在想什么

I dont see the SQL queries when i run the applicartion in logWanted to know what am i missing

看到此帖子如何为Mybatis配置log4j打印我的SQL 建议更改mybatis类的配置,但不确定如何使用spring SqlSessionFactoryBean

saw this post how to configure log4j for Mybatis to print my SQLsuggesting to change mybatis class configuration but not sure how to do with spring SqlSessionFactoryBean

推荐答案

您可以通过Mybatis-config.xml添加Mybatis的日志记录.

You can add logging for Mybatis via it's mybatis-config.xml.

像这样添加log4j:

Add log4j like so:

mybatis-config.xml

<configuration>
  <settings>
    ...
    <setting name="logImpl" value="LOG4J"/>
    ...
  </settings>
</configuration>

然后在您的log4j.properties中,添加您想要记录的类:

Then in your log4j.properties, add the class that you'd like to log:

log4j.logger.org.mybatis.example.MyMapper=TRACE

SQL语句以DEBUG级别记录,因此将输出设置为DEBUG:

SQL statements are logged at the DEBUG level, so set output to DEBUG:

log4j.logger.org.mybatis.example=DEBUG

有关更多详细信息,请参见文档.

For more details, see the documentation.

这篇关于使用MyBatis和Spring在项目中记录SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 09:00