[mybatis] batch insert

public interface BfCusVoucherMapper extends BaseMapper<BfCusVoucher> {

   void insertBatch(@Param("list") List<BfCusVoucher> list);
}
 
 
<insert id="insertBatch" parameterType="java.util.List">
   insert into t_vcar_cus_voucher (customer_id, obtain_type, voucher_way, voucher_id, start_time, end_time, is_userd, voucher_status, created_time, updated_time) values
   <foreach collection="list" item="item" index="index" separator=",">
      (#{item.customerId},
      #{item.obtainType},
      #{item.voucherWay},
      #{item.voucherId},
      #{item.startTime},
      #{item.endTime},
      #{item.isUserd},
      #{item.voucherStatus},
      #{item.createdTime},
      #{item.updatedTime})
   </foreach>
</insert>

The method in the service:

private void doBatchInsertCustomerVoucher(Long customerId, String voucherWay, String voucherStatus, BfSysInwardPlanDTO sysInwardPlan, int batchNum){
    if(sysInwardPlan == null){
        return ;
    }
    List<BfCusVoucher> list = new ArrayList<>(batchNum);
    for(int i = 0 ;  i < batchNum; i++){
        BfCusVoucher cusVoucher = constructCustomerVoucher(customerId, voucherWay, voucherStatus, sysInwardPlan);
        list.add(cusVoucher);
    }
    bfCusVoucherMapper.insertBatch(list);
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325644521&siteId=291194637