/**
* 批量更新
*/
public int[] batchUpdate(final List<User> userList) {
String sql="update user set name=? where id=?";
//BatchPreparedStatementSetter该接口需要有两个方法;
int[] count=jdbcTempalte.batchUpdate(sql, new BatchPreparedStatementSetter(){
//需要更新的次数
public int getBatchSize() {
return userList.size();
}
//为ps赋值
public void setValues(PreparedStatement ps, int i)
throws SQLException {
ps.setString(1, userList.get(i).getName());
ps.setInt(2, userList.get(i).getId());
}
});
return count;
}
本文介绍了一种使用 JDBC 模板批量更新数据库中用户信息的方法。通过实现 BatchPreparedStatementSetter 接口,可以有效地设置批量更新语句,提高数据更新效率。
2190

被折叠的 条评论
为什么被折叠?



