欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

批量插入

程序员文章站 2022-06-10 10:38:49
/** * 批量插入 * @param mergerDtos * @param mergerId */private void batchSaveMergerItem(List mergerDtos, Long mergerId){ if (CollectionUtils.is ......
/**
* 批量插入
* @param mergerDtos
* @param mergerId
*/
private void batchSaveMergerItem(List<MergerDto> mergerDtos, Long mergerId){
if (CollectionUtils.isNotEmpty(mergerDtos)) {
for (MergerDto mergerDto : mergerDtos) {
List<Long> ids = mergerDto.getIds();

int size = ids.size();
List<TrMergerMemberItemEo> batch = initList(size,BizConstant.DB_MAX_BATCH_NUM);
boolean flushFlag = false;
for (int i = 0; i < size; i++) {
if ( ((i+1) % BizConstant.DB_MAX_BATCH_NUM == 0 )
||( i == size -1)) {
flushFlag = true;
}
TrMergerMemberItemEo trMergerMemberItemEo = new TrMergerMemberItemEo();
trMergerMemberItemEo.setMergerId(mergerId);
trMergerMemberItemEo.setTableName(mergerDto.getTableName());
trMergerMemberItemEo.setBizId(ids.get(i));
batch.add(trMergerMemberItemEo);
if (flushFlag) {
trMergerMemberItemDas.insertBatch(batch);
if (i != size -1) {
flushFlag = false;
batch = initList(size,BizConstant.DB_MAX_BATCH_NUM);
}
}
}
}
}
}

private List initList(int size, int maxSize){
List list ;
if (size > maxSize) {
list = new ArrayList<>(maxSize);
} else {
list = new ArrayList<>(size);
}
return list;
}