如何在 JdbcTemplate 中执行多批删除? [英] How to execute multi batch delete in JdbcTemplate?
本文介绍了如何在 JdbcTemplate 中执行多批删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想一次删除多个数据库条目.仅当 3 个字段匹配时才应删除每个条目(此处:姓名、电子邮件、年龄).
I want to delete multiple database entries at once. Each entry should only be deleted if 3 fields match (here: name, email, age).
如果我只想删除单个属性,我会选择:
If I'd just wanted to delete by a single property, I'd go for:
String sql = "DELETE FROM persons WHERE (email) IN (?)";
JdbcTemplate template;
template.execute(sql, Arrays.asList(emails...));
但是如果我的条件由多个字段组成怎么办?
But what if my condition is formed by multiple fields?
String sql = "DELETE FROM persons WHERE (name, email, age) IN (?, ?, ?)";
JdbcTemplate template;
template.execute(sql, ...); ???
条件应始终匹配所有 3 个字段(AND
)!
The condition should always match all 3 fields (AND
)!
推荐答案
使用 batchUpdate(sql, batchArgs, argTypes)
方法.
Use the batchUpdate(sql, batchArgs, argTypes)
method.
String sql = "DELETE FROM persons WHERE name = ? AND email = ? AND age = ?";
int[] argTypes = { Types.VARCHAR, Types.VARCHAR, Types.INTEGER };
List<Object[]> batchArgs = new ArrayList<>();
batchArgs.add(new Object[] { "John Doe", "john@example.com", 42 });
batchArgs.add(new Object[] { "Jane Smith", "jane@example.com", 47 });
. . .
JdbcTemplate template = ...;
int[] rowCounts = template.batchUpdate(sql, batchArgs, argTypes);
这篇关于如何在 JdbcTemplate 中执行多批删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文