【mybatis】批量插入

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>

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);
}


猜你喜欢

转载自blog.csdn.net/shiki_41/article/details/80111480