根据某个或者多个非ID字段进行批量更新
示例通过名称与id两个字段更新
@Override public boolean updateBatchByColumn(List<TestTable> list) { return updateBatchByQueryWrapper(list, item->new QueryWrapper<>().eq("name",item.getName()).eq("id",item.getId())); } @Transactional(rollbackFor = Exception.class) public boolean updateBatchByQueryWrapper(Collection<TestTable> entityList, Function<TestTable, QueryWrapper> wrapperFunction) { String sqlStatement = this.getSqlStatement(SqlMethod.UPDATE); return this.executeBatch(entityList, DEFAULT_BATCH_SIZE, (sqlSession, entity) -> { Map<String, Object> param = CollectionUtils.newHashMapWithExpectedSize(2); param.put(Constants.ENTITY, entity); param.put(Constants.WRAPPER, wrapperFunction.apply(entity)); sqlSession.update(sqlStatement, param); }); }
通过其他字段批量更新或新增
public boolean saveOrUpdateBatchByColumn(Collection<TestTable> entityList, Function<TestTable, QueryWrapper> function ) { return SqlHelper.saveOrUpdateBatch(this.entityClass, this.mapperClass, LogFactory.getLog(AvgRowSub.class), entityList, 1000, (sqlSession, entity) -> { Map<String, Object> param = Maps.newHashMap(); param.put(Constants.ENTITY, entity); param.put(Constants.WRAPPER, function.apply(entity)); return CollectionUtils.isEmpty(sqlSession.selectList(this.getSqlStatement(SqlMethod.SELECT_MAPS), param)); }, (sqlSession, entity) -> { Map<String, Object> param = Maps.newHashMap(); param.put(Constants.ENTITY, entity); param.put(Constants.WRAPPER, function.apply(entity)); sqlSession.update(this.getSqlStatement(SqlMethod.UPDATE), param); }); }
来源地址:https://blog.csdn.net/qq_43383907/article/details/129225531