如题,很多时候批量更新的条件比较复杂
private boolean updateBatch(List<UpdateWrapper> updateWrapperList, int batchSize) {
String sqlStatement = sqlStatement(SqlMethod.UPDATE);
try (SqlSession batchSqlSession = sqlSessionBatch()) {
int i = 0;
for (UpdateWrapper updateWrapper : updateWrapperList) {
Map<String, Object> map = new HashMap<>(1);
map.put(Constants.WRAPPER, updateWrapper);
batchSqlSession.update(sqlStatement, map);
if (i >= 1 && i % batchSize == 0) {
batchSqlSession.flushStatements();
}
i++;
}
batchSqlSession.flushStatements();
}
return true;
}
调用时候:
updateBatch(list.stream().map(p -> {
UpdateWrapper<VO> updateWrapper = new UpdateWrapper<>();
updateWrapper.lambda().set(VO :: getName, p.getName());
updateWrapper.lambda().eq(VO:: getCode, p.getCode());
return updateWrapper;
}).collect(Collectors.toList()), 2000);
实体类VO换成对应数据库的实体类,UpdateWrapper中的内容可以随便换,验证完是可用的,欢迎指正。