【问题标题】:How to delete records from table with like condition using Spring JDBC template update?如何使用 Spring JDBC 模板更新从具有相似条件的表中删除记录?
【发布时间】:2020-05-07 05:49:33
【问题描述】:

我正在尝试使用 JDBC 模板和 Oracle 从表中删除一些行。 我不知道如何在类似条件下删除。 我们如何在 like 条件中传递参数?

我尝试了以下方法:

this.jdbcTemplate.update("delete from logs l where l.message like '%" + ":studentId" + "%'" ,new MapSqlParameterSource("studentId", studentIds[i]));

这段代码写在一个 for 循环中,该循环应该对 studentIds 数组中的每个 studentId 应用 delete 语句。它不会抛出错误,但它也不起作用。

在调试日志中它说: 执行准备好的 SQL 语句 [从日志中删除 l where l.message like '%:studentId%']

对于成功执行的语句调试日志说: 执行准备好的 SQL 语句 [update students m set m.student_deleted='Y' where m.student_id = ? ]

这里放了“?”而不是我的参数。 我认为对于具有类似条件的语句也必须是这样的。但它却把 %:studentId%

这样做的正确方法是什么,在此先感谢。

【问题讨论】:

    标签: spring oracle spring-mvc spring-jdbc jdbctemplate


    【解决方案1】:

    我找到了解决方案here

    应该是这样的:

    this.jdbcTemplate.update("delete from logs l where l.message like :studentId" ,new MapSqlParameterSource("studentId", "%"+studentIds[i]+"%" ));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多