【问题标题】:spring NamedParameterJdbcTemplate get (final) sql query with parameters substitutedspring NamedParameterJdbcTemplate 获取(最终)sql查询,参数被替换
【发布时间】:2017-10-24 22:05:44
【问题描述】:

我需要获取从 NamedParameterJdbcTemplate 发送到数据库的最终 sql 查询。例如:

Select * from tbl where name = :name

我需要这样的东西:

Select * from tbl where name ='mark'

非常感谢

【问题讨论】:

标签: sql spring-data spring-jdbc jdbctemplate


【解决方案1】:

快速浏览at the source code https://github.com/spring-projects/spring-framework/blob/master/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.java 所有查询都通过'getParsedSql' methodNamedParameterUtils 进行解析。

Select * from tbl where name = :name

可能会被翻译成类似的东西

Select * from tbl where name = ?

并且参数将作为单独的对象提供,因为 JDBC 就是这样工作的。

如果您只想检查语句,您可以添加一些断点并查看一下。如果您想实际获取值,您可以更改代码,使其可通过反射或实际使用您自己的“NamedparameterJdbcTemplate”版本来访问

请注意,您可能不会看到预期的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 2012-06-18
    相关资源
    最近更新 更多