使用 MyBatis 和 Spring 在项目中记录 SQL 查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17465739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Log SQL queries in project using MyBatis and Spring
提问by Lav
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
我在日志中运行应用程序时看不到 SQL 查询 想知道我错过了什么
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 配置 log4j 以打印我的 SQL建议更改 mybatis 类配置但不确定如何使用 spring SqlSessionFactoryBean
回答by Matt
You can add logging for Mybatis via it's mybatis-config.xml.
您可以通过 mybatis-config.xml 为 Mybatis 添加日志记录。
Add log4j like so:
像这样添加 log4j:
mybatis-config.xml
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.properties 中,添加您想要记录的类:
log4j.logger.org.mybatis.example.MyMapper=TRACE
SQL statements are logged at the DEBUG level, so set output to DEBUG:
SQL 语句记录在 DEBUG 级别,因此将输出设置为 DEBUG:
log4j.logger.org.mybatis.example=DEBUG
For more details, see the documentation.
有关更多详细信息,请参阅文档。
回答by Gunith D
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.
引用如何为 Mybatis 配置 logback 以打印我的 SQL的答案,我不确定这是否完全适合您。它提供了用于日志记录的 Spring 配置。这种方法对我有用。
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
要记录特定 mybatis 映射器的 SQL 语句,请为具有完全限定映射器名称的记录器设置调试(跟踪以查看查询参数和结果)级别
<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
如果它们在同一个包中,您可以记录来自所有映射器的所有 SQL 语句
<logger name="com.mycompany.myapp.mapper" level="DEBUG"/>
Give it a go, if the problem is still there. Good luck!
试一试,如果问题仍然存在。祝你好运!
回答by Paul Vargas
Test with the simplest way configuration and see in the log. Then customize the output (e.g. the files, levels).
用最简单的方式配置进行测试,并在日志中查看。然后自定义输出(例如文件、级别)。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN"
"log4j.dtd" >
<log4j:configuration>
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p (%c.java:%L).%M - %m%n"/>
</layout>
</appender>
<root>
<priority value="TRACE" />
<appender-ref ref="STDOUT"/>
</root>
</log4j:configuration>
回答by Nailgun
Try to add all the necessary lines to your configuration.
尝试将所有必要的行添加到您的配置中。
Here is the sample that should work:
这是应该工作的示例:
#configure root logger
log4j.rootLogger=ERROR, file, stdout
#configure all mybatis mappers logging
log4j.logger.com.myco.dao=ERROR
#configure your mapper logging
log4j.logger.com.myco.dao.XYZMapper=DEBUG
#configure appender
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
回答by Arash
Another shortcut is to set the debug level of your mybatis mappers to true in your application.properties file:
另一个快捷方式是在 application.properties 文件中将 mybatis 映射器的调试级别设置为 true:
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

