根据某个或者多个非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);
});
}
引用mybatis-plus根据某个指定字段批量更新数据库文章来源:https://www.toymoban.com/news/detail-531683.html
通过其他字段批量更新或新增文章来源地址https://www.toymoban.com/news/detail-531683.html
/**
* 条件批量添加更新
*
* @param entityList 数据
* @param function 条件
* @return boolean
*/
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);
});
}
到了这里,关于Mybatis-plus通过其他字段批量更新或新增的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!