使用MyBatis和Spring在项目中记录SQL查询[英] Log SQL queries in project using MyBatis and Spring

本文是小编为大家收集整理的关于使用MyBatis和Spring在项目中记录SQL查询的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

在我的项目中,我有

<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

我在日志中运行申请书时看不到SQL查询 想知道我想念什么

看这篇文章如何为mybatis配置log4j打印我的SQL 建议更改Mybatis类配置,但不确定如何使用Spring SqlSessionFactoryBean

推荐答案

引用如何如何如何如何要配置Mybatis的记录以打印我的SQL ,我不确定这是否对您有效.它提供了用于记录的弹簧配置.这种方法对我有用.

登录特定Mybatis映射器SET DEBUG的SQL语句(跟踪 要查看查询参数和结果)logger的级别 合格的映射名称

<logger name="com.mycompany.myapp.mapper.MyMapper" level="DEBUG"/>

,如果它们在 像这样的包装

<logger name="com.mycompany.myapp.mapper" level="DEBUG"/>

如果问题还在那里,请尝试一下.祝你好运!

其他推荐答案

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

添加log4j喜欢:

mybatis-config.xml

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

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

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

SQL语句已记录在调试级别上,因此将输出设置为调试:

log4j.logger.org.mybatis.example=DEBUG

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

其他推荐答案

另一个快捷方式是将mybatis映射器的调试级别设置为true.properties文件:

logging.level.<packageName>.mapper=DEBUG

在控制台或您的日志文件中打印的示例日志:

2020-03-03 09:41:27.057 DEBUG 10495 --- [io-50006-exec-1] c.f.t.d.m.M.countByExample               : ==>  Preparing: SELECT count(*) FROM MessageRecivers WHERE ((ReciverId = ? and ReadStats = ? and ReciverMessageFolder <> ?)) 
2020-03-03 09:41:27.066 DEBUG 10495 --- [io-50006-exec-1] c.f.t.d.m.M.countByExample               : ==> Parameters: 58(Long), 0(Short), 1(Short)
2020-03-03 09:41:27.473 DEBUG 10495 --- [io-50006-exec-1] c.f.t.d.m.M.countByExample               : <==      Total: 1

本文地址:https://www.itbaoku.cn/post/1574873.html

问题描述

In my project i have

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

and

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

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

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

推荐答案

Quoting from an answer of how to configure logback for Mybatis to print my SQL, I'm not sure if this will work for you in entirety. It provides the Spring config for logging. This approach worked for me.

To log SQL statements for particular mybatis mapper set DEBUG (TRACE to see query parameters and results) level for logger with fully qualified mapper name

<logger name="com.mycompany.myapp.mapper.MyMapper" level="DEBUG"/>

You can log all SQL statements from all mappers if they are in the same package like this

<logger name="com.mycompany.myapp.mapper" level="DEBUG"/>

Give it a go, if the problem is still there. Good luck!

其他推荐答案

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

Add log4j like so:

mybatis-config.xml

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

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

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

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.

其他推荐答案

Another shortcut is to set the debug level of your mybatis mappers to true in your application.properties file:

logging.level.<packageName>.mapper=DEBUG

Example log printed in console or your log file:

2020-03-03 09:41:27.057 DEBUG 10495 --- [io-50006-exec-1] c.f.t.d.m.M.countByExample               : ==>  Preparing: SELECT count(*) FROM MessageRecivers WHERE ((ReciverId = ? and ReadStats = ? and ReciverMessageFolder <> ?)) 
2020-03-03 09:41:27.066 DEBUG 10495 --- [io-50006-exec-1] c.f.t.d.m.M.countByExample               : ==> Parameters: 58(Long), 0(Short), 1(Short)
2020-03-03 09:41:27.473 DEBUG 10495 --- [io-50006-exec-1] c.f.t.d.m.M.countByExample               : <==      Total: 1