如何使用 Spring JDBC 模板更新从 table 中删除条件相似的记录?
How to delete records from table with like condition using Spring JDBC template update?
我正在尝试使用 JDBC 模板和 Oracle 从 table 中删除一些行。
我无法弄清楚如何在类似条件下删除。
我们如何在like条件中传递参数?
我尝试了以下方法:
this.jdbcTemplate.update("delete from logs l where l.message like '%" + ":studentId" + "%'" ,new MapSqlParameterSource("studentId", studentIds[i]));
这段代码是在一个 for 循环中编写的,它应该对 studentIds 数组中的每个 studentId 应用 delete 语句。它不会抛出错误,但它也不起作用。
在调试日志中它说:
正在执行准备好的 SQL 语句 [从日志 l 中删除,其中 l.message 如 '%:studentId%']
对于一条成功执行的语句,调试日志说:
正在执行准备好的 SQL 语句 [update students m set m.student_deleted='Y' where m.student_id = ? ]
这里是“?”而不是我的参数。
我认为对于具有类似条件的语句也必须是这样的。但相反,它把 %:studentId%
这样做的正确方法是什么,在此先感谢。
我找到了解决方案 here。
本来应该是这样的:
this.jdbcTemplate.update("delete from logs l where l.message like :studentId" ,new MapSqlParameterSource("studentId", "%"+studentIds[i]+"%" ));
我正在尝试使用 JDBC 模板和 Oracle 从 table 中删除一些行。 我无法弄清楚如何在类似条件下删除。 我们如何在like条件中传递参数?
我尝试了以下方法:
this.jdbcTemplate.update("delete from logs l where l.message like '%" + ":studentId" + "%'" ,new MapSqlParameterSource("studentId", studentIds[i]));
这段代码是在一个 for 循环中编写的,它应该对 studentIds 数组中的每个 studentId 应用 delete 语句。它不会抛出错误,但它也不起作用。
在调试日志中它说: 正在执行准备好的 SQL 语句 [从日志 l 中删除,其中 l.message 如 '%:studentId%']
对于一条成功执行的语句,调试日志说: 正在执行准备好的 SQL 语句 [update students m set m.student_deleted='Y' where m.student_id = ? ]
这里是“?”而不是我的参数。 我认为对于具有类似条件的语句也必须是这样的。但相反,它把 %:studentId%
这样做的正确方法是什么,在此先感谢。
我找到了解决方案 here。
本来应该是这样的:
this.jdbcTemplate.update("delete from logs l where l.message like :studentId" ,new MapSqlParameterSource("studentId", "%"+studentIds[i]+"%" ));