批量插入
程序员文章站
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;
}
下一篇: 直播软件源码如何实现音视频同步(一)