Seeing the underlying SQL in the Spring JdbcTemplate?

后端 未结 7 990
深忆病人
深忆病人 2020-11-30 02:24

I am learning about the wonders of JdbcTemplate and NamedParameterJdbcTemplate. I like what I see, but is there any easy way to see the underlying SQL that it ends up execut

相关标签:
7条回答
  • 2020-11-30 02:54

    This works for me with org.springframework.jdbc-3.0.6.RELEASE.jar. I could not find this anywhere in the Spring docs (maybe I'm just lazy) but I found (trial and error) that the TRACE level did the magic.

    I'm using log4j-1.2.15 along with slf4j (1.6.4) and properties file to configure the log4j:

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

    This displays both the SQL statement and bound parameters like this:

    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
    

    Not sure about the SQL type unknown but I guess we can ignore it here

    For just an SQL (i.e. if you're not interested in bound parameter values) DEBUG should be enough.

    0 讨论(0)
提交回复
热议问题