Skip to main content
 首页 » 编程设计

sql之看到 Spring JdbcTemplate 中的底层 SQL 了吗

2024年04月17日14cmt

我正在了解 JdbcTemplate 和 NamedParameterJdbcTemplate 的奇妙之处。我喜欢我所看到的,但是有没有简单的方法来查看它最终执行的底层 SQL?我希望看到这个用于调试目的(例如,为了在外部工具中调试生成的 SQL)。

请您参考如下方法:

Spring documentation说他们记录在调试级别:

All SQL issued by this class is logged at the DEBUG level under the category corresponding to the fully qualified class name of the template instance (typically JdbcTemplate, but it may be different if you use a custom subclass of the JdbcTemplate class).

用 XML 术语来说,您需要对记录器进行如下配置:

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

然而,这个主题是一个月前在这里讨论的,它似乎不像在 Hibernate 中那么容易开始工作和/或它没有返回预期的信息:Spring JDBC is not logging SQL with log4j 。您还可以使用P6Spy相反,也可以是 integrated in Spring Boot .