我正在學習JdbcTemplate和NamedParameterJdbcTemplate的奇觀。我喜歡我所看到的,但有沒有簡單的方法可以查看它最終執行的底層SQL?我希望看到這是出於調試目的(爲了例如在外部工具中調試生成的SQL)。查看Spring JdbcTemplate中的底層SQL?
回答
的Spring documentation說,他們在DEBUG級別記錄:
由該類發出的所有SQL下的類別記錄在DEBUG水平對應於模板實例的完全合格類名稱(通常爲JdbcTemplate,但如果您使用的是JdbcTemplate類的自定義子類,則可能會有所不同)。
在XML方面,你需要配置的記錄是這樣的:
<category name="org.springframework.jdbc.core.JdbcTemplate">
<priority value="debug" />
</category>
這個主題是但是這裏討論在一個月前,似乎不是那麼容易得到,如Hibernate來工作, /或它沒有返回預期信息:Spring JDBC is not logging SQL with log4j每個建議使用P6Spy的這個主題都可以根據this article在Spring中集成。
使用名稱「org.springframework.jdbc」,以查看真正的SQL查詢。 – Verhagen 2012-10-24 11:38:17
我不是100%確定你會得到什麼,因爲通常你會將你的SQL查詢(參數化或不參數化)傳遞給JdbcTemplate,在這種情況下你只需要記錄那些。如果您有PreparedStatement
s,並且您不知道哪一個正在執行,那麼toString
方法應該可以正常工作。但是,當我們談論這個問題時,有一個很好的Jdbc記錄器包here,它可以讓你自動記錄你的查詢,並且每次看到綁定的參數。很有用。輸出看起來是這樣的:
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}
這適用於org.springframework.jdbc-3.0.6.RELEASE.jar。 我在Spring文檔的任何地方都找不到(也許我只是懶惰),但是我發現(試驗和錯誤)TRACE級別做到了這一點。
我與SLF4J(1.6.4)一起使用log4j的-1.2.15和屬性文件來配置log4j的:
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
不知道的SQL類型未知的,但我想我們可以在這裏忽略它
對於只是一個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
不適用於jdbcTemplate和logback – Emilio 2014-01-15 15:33:47
這個工作對我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
- 1. SQL中的JdbcTemplate
- 2. 使用Spring構建數據層JdbcTemplate
- 3. Spring - jdbcTemplate
- 4. 如何使用spring的jdbcTemplate在SQL查詢中指定參數
- 5. Spring的JdbcTemplate -
- 6. SQL Server - 更新底層表記錄來自查看AFTER INSERT
- 7. 從查詢/查看字段中查找底層字段
- 8. Spring JDBCTemplate中的addBatch支持?
- 9. java.sql.Statement.EXECUTE_FAILED Spring中的場景JdbcTemplate
- 10. Spring引導Jdbctemplate
- 11. Spring jdbcTemplate Junit
- 12. Spring jdbctemplate,datasource,transactionManager
- 13. Spring JdbcTemplate和Threading
- 14. Java Spring JdbcTemplate
- 15. 使用Spring的JdbcTemplate
- 16. HTML5多層,看不到底層
- 17. 從Spring JdbcTemplate中提取ResultSetMetaData
- 18. 在Spring Boot中使用JdbcTemplate
- 19. Spring jdbcTemplate -add過濾器查詢
- 20. DSE圖 - 如何從Gremlin查詢中查看底層的Cassandra查詢?
- 21. Spring JdbcTemplate butchUpdate UPDATE org.springframework.jdbc.BadSqlGrammarException:
- 22. Spring JdbcTemplate ConnectionPooling配置
- 23. 的Ehcache與Spring Configruation +的JdbcTemplate
- 24. Spring的JdbcTemplate和NamedParameterJdbcTemplate的
- 25. spring jdbctemplate和Hibernate的區別
- 26. 插入BLOB與Spring的JdbcTemplate
- 27. 底層的DataTable
- 28. ServiceMix中的Spring JdbcTemplate/SimpleJdbcCall有狀態
- 29. Spring中靜態JdbcTemplate的替代方案
- 30. 如何從Excel中使用Spring的JdbcTemplate
爲了澄清,我希望看到SQL與'?'確保整個過程正常工作。 – Artem 2009-12-19 07:07:06
嗨Artem,你有沒有在你的代碼中實現這一點? – 2013-09-18 17:00:31