[英]How to delete multiple rows with JdbcTemplate
Want to delete multiple rows with the help of JdbcTemplate
. 想要在
JdbcTemplate
的帮助下删除多行。 In the below code msgNos
is a String variable containing comma separated value like 26,27. 在下面的代码中,
msgNos
是一个String变量,包含逗号分隔值,如26,27。 After executing the statements it is only deleting one record. 执行语句后,它只删除一条记录。
String sqlQuery = " delete from canned_message where msg_no in (?)";
Object[] params = {msgNos};
int rows = smsdbJdbcTemplate.update(sqlQuery, params);
Instead of org.springframework.jdbc.core.JdbcTemplate
use org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
where you can use named parameter. 而不是
org.springframework.jdbc.core.JdbcTemplate
使用org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
,您可以在其中使用命名参数。
Then in your query you can pass List
as parameter value in the in clause 然后在您的查询中,您可以在in子句中将
List
作为参数值传递
String sqlQuery = "delete from canned_message where msg_no in (:msgNos)";
List<Integer> params = <array list of number>;
Map namedParameters = Collections.singletonMap("msgNos", params);
int rows = smsdbJdbcTemplate.update(sqlQuery, namedParameters);
Note: I've used List<Integer>
use appropriate datatype as you need. 注意:我已经使用
List<Integer>
根据需要使用适当的数据类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.