我想一次删除多个数据库条目。仅当3个字段匹配时(此处为:姓名,电子邮件,年龄),才能删除每个条目。
如果我只想删除一个属性,则可以选择:
String sql = "DELETE FROM persons WHERE (email) IN (?)"; JdbcTemplate template; template.execute(sql, Arrays.asList(emails...));
但是,如果我的病情是由多个领域构成的,那该怎么办?
String sql = "DELETE FROM persons WHERE (name, email, age) IN (?, ?, ?)"; JdbcTemplate template; template.execute(sql, ...); ???
条件应始终匹配所有3个字段(AND)!
AND
使用batchUpdate(sql, batchArgs, argTypes)方法。
batchUpdate(sql, batchArgs, argTypes)
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);