在Spring JdbcTemplate中查看底层的SQL?

我正在学习JdbcTemplate和NamedParameterJdbcTemplate的奇观。 我喜欢我所看到的,但有没有简单的方法来查看它最终执行的底层SQL? 我想看到这是为了debugging目的(例如为了debugging在外部工具中产生的SQL)。

Spring文档说他们在DEBUG级别login:

所有由此类发出的SQL都将在与模板实例的完全限定类名相对应的下的DEBUG级别(通常为JdbcTemplate,但如果您使用的是JdbcTemplate类的自定义子类,则可能会有所不同)。

用XML来说,您需要configurationlogging器,如下所示:

<category name="org.springframework.jdbc.core.JdbcTemplate"> <priority value="debug" /> </category> 

但是这个主题在一个月前就已经讨论了,而且在Hibernate中工作起来并不容易,或者它没有返回期望的信息: Spring JDBC没有使用log4j来loggingSQL这个主题下每个build议使用P6Spy根据这篇文章 ,它也可以集成到Spring中。

这适用于我org.springframework.jdbc-3.0.6.RELEASE.jar。 在Spring文档中我找不到任何地方(也许我只是懒惰),但是我发现(试错)TRACE级别做了魔术。

我使用log4j-1.2.15以及slf4j(1.6.4)和properties文件来configurationlog4j:

 log4j.logger.org.springframework.jdbc.core = TRACE 

这将显示SQL语句和绑定参数,如下所示:

 Executing prepared SQL statement [select HEADLINE_TEXT, NEWS_DATE_TIME from MY_TABLE where PRODUCT_KEY = ? and NEWS_DATE_TIME between ? and ? order by NEWS_DATE_TIME] Setting SQL statement parameter value: column index 1, parameter value [aaa], value class [java.lang.String], SQL type unknown Setting SQL statement parameter value: column index 2, parameter value [Thu Oct 11 08:00:00 CEST 2012], value class [java.util.Date], SQL type unknown Setting SQL statement parameter value: column index 3, parameter value [Thu Oct 11 08:00:10 CEST 2012], value class [java.util.Date], SQL type unknown 

不知道SQLtypes未知,但我想我们可以在这里忽略它

对于只是一个SQL(即如果你不想绑定参数值) DEBUG应该是足够的。

参数值似乎打印在TRACE级别上。 这对我工作:

 log4j.logger.org.springframework.jdbc.core.JdbcTem plate=DEBUG, file log4j.logger.org.springframework.jdbc.core.StatementCreatorUtils=TRACE, file 

控制台输出:

 02:40:56,519 TRACE http-bio-8080-exec-13 core.StatementCreatorUtils:206 - Setting SQL statement parameter value: column index 1, parameter value [Tue May 31 14:00:00 CEST 2005], value class [java.util.Date], SQL type unknown 02:40:56,528 TRACE http-bio-8080-exec-13 core.StatementCreatorUtils:206 - Setting SQL statement parameter value: column index 2, parameter value [61], value class [java.lang.Integer], SQL type unknown 02:40:56,528 TRACE http-bio-8080-exec-13 core.StatementCreatorUtils:206 - Setting SQL statement parameter value: column index 3, parameter value [80], value class [java.lang.Integer], SQL type unknown 

我不是100%确定你得到什么,因为通常你会将你的SQL查询(参数化或不参数化)传递给JdbcTemplate,在这种情况下,你只需要logging这些。 如果你有PreparedStatement并且你不知道哪一个正在执行, toString方法应该可以正常工作。 但是,当我们谈论这个问题的时候,这里有一个很好的Jdbclogging器包,它可以让你自动logging你的查询,并且每次看到绑定的参数。 很有用。 输出看起来像这样:

 executing PreparedStatement: 'insert into ECAL_USER_APPT (appt_id, user_id, accepted, scheduler, id) values (?, ?, ?, ?, null)' with bind parameters: {1=25, 2=49, 3=1, 4=1} 

这对我log4j2和XML参数工作:

 <?xml version="1.0" encoding="UTF-8"?> <Configuration status="debug"> <Properties> <Property name="log-path">/some_path/logs/</Property> <Property name="app-id">my_app</Property> </Properties> <Appenders> <RollingFile name="file-log" fileName="${log-path}/${app-id}.log" filePattern="${log-path}/${app-id}-%d{yyyy-MM-dd}.log"> <PatternLayout> <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n </pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> </Policies> </RollingFile> <Console name="console" target="SYSTEM_OUT"> <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" /> </Console> </Appenders> <Loggers> <Logger name="org.springframework.jdbc.core" level="trace" additivity="false"> <appender-ref ref="file-log" /> <appender-ref ref="console" /> </Logger> <Root level="info" additivity="false"> <appender-ref ref="file-log" /> <appender-ref ref="console" /> </Root> </Loggers> </Configuration> 

结果控制台和文件日志是:

 JdbcTemplate - Executing prepared SQL query JdbcTemplate - Executing prepared SQL statement [select a, b from c where id = ? ] StatementCreatorUtils - Setting SQL statement parameter value: column index 1, parameter value [my_id], value class [java.lang.String], SQL type unknown 

只需复制/过去

HTH