请看下面我用来更新表中字段的方法,
现在我有一个条件,如果 Id 存在于表中,则更新相关字段,否则在表中插入一条新记录。
如果id存在则更新
如果表中不存在id则插入一条新记录
有没有办法通过批量更新来满足这两个条件或任何其他方式,因为我必须总共处理数百行。
public void updateTable(List<TestClass> testList){
String UPDATE_ITEM_ARTICLENO = "UPDATE itemTable SET articleno=? WHERE itemId=?";
jdbcTemplate.batchUpdate(UPDATE_ITEM_ARTICLENO, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
TestClass article = testList.get(i);
ps.setString(1, article.getItemId());
ps.setString(2, article.getArticleno());
}
@Override
public int getBatchSize() {
return testList.size();
}
});
}
最佳答案
有一个标准Merge (SQL)陈述。但并非所有 RDBMS 都支持它。其中一些有替代方案。
关于java - JDBC 模板 : either Update or Insert if ID doesn't exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21549805/