防止数据量太大,将数据进行拆分
@Override
@Transactional(rollbackFor = Exception.class)
public Integer insertBatchForTask(List<DeductionRecordEntity> deductionRecordEntities) {
List<List<DeductionRecordEntity>> split = CollectionUtil.split(deductionRecordEntities, 1000);
Integer result = 0;
for(List<DeductionRecordEntity> d : split){
Integer integer = deductionRecordMapper.insertBatchForTask(d);
result += integer;
}
return result;
}
批量新增数据
<insert id="insertBatchForTask">
INSERT INTO tb_deduction_record (id, user_id, user_name, user_phone, business_id, amount, reason, create_by, create_time, trade_type, type, site_id)
<foreach collection="deductionRecordEntities" item="deduction" separator=" UNION ALL ">
SELECT #{deduction.id},
#{deduction.userId},
#{deduction.userName},
#{deduction.userPhone},
#{deduction.businessId},
#{deduction.amount},
#{deduction.reason},
#{deduction.createBy},
#{deduction.createTime},
#{deduction.tradeType},
#{deduction.type},
#{deduction.siteId}
FROM DUAL
WHERE NOT EXISTS (
SELECT 1 FROM tb_deduction_record
WHERE user_id = #{deduction.userId}
AND trade_type = #{deduction.tradeType}
AND type = #{deduction.type}
AND DATE_FORMAT(create_time,'%Y-%m-%d') = DATE_FORMAT(#{deduction.createTime},'%Y-%m-%d')
)
</foreach>
</insert>