手动分割数组 mybatisPlus 大量插入

	public void pmsCount(String date) {
	      if (CollectionUtil.isNotEmpty(addCountVOS)) {
	          List<List<CountVO>> addLists = subToList(addCountVOS);//addCountVOS 为大数组
	          for (List<CountVO> toInsertC : addLists) {
	              pmsCountMapper.batchInsertOrderCount(toInsertC);
	          }
	      }
	}

	//数组分割 拆分数可以自己定  防止sql过长执行失败
    public <T> List<List<T>> subToList(List<T> numberList){
        // 拆分数
        int splitNumber = 300;
        // 拆分后的数据集
        List<List<T>> dividedList = new ArrayList<>();
    
        int size = numberList.size();
        int count = (size + splitNumber - 1) / splitNumber;
        for (int i = 0; i < count; i++) {
            List<T> subList = numberList.subList(i * splitNumber, (Math.min((i + 1) * splitNumber, size)));
            dividedList.add(subList);
        }
        return dividedList;
    }

**

XML就是正常的for循环

**

  <insert id="batchInsertOrderCount">
    insert into table_order
    (hotel_no, `date`, hotel_name, city, province, brand, dep_name, city_proper, area,
      hotel_type, city_group, cuss_order_count, cuss_check_in_count, cuss_check_out_count,
      cuss_room_count, cuss_face_count, device_no)
    values
    <foreach collection="list" item="item" separator=",">
      (#{item.hotelNo,jdbcType=VARCHAR}, #{item.date,jdbcType=TIMESTAMP}, #{item.hotelName,jdbcType=VARCHAR},
        #{item.city,jdbcType=VARCHAR}, #{item.province,jdbcType=VARCHAR}, #{item.brand,jdbcType=VARCHAR},
        #{item.depName,jdbcType=VARCHAR}, #{item.cityProper,jdbcType=VARCHAR}, #{item.area,jdbcType=VARCHAR},
        #{item.hotelType,jdbcType=VARCHAR}, #{item.cityGroup,jdbcType=VARCHAR}, #{item.cussOrderCount,jdbcType=INTEGER},
        #{item.cussCheckInCount,jdbcType=INTEGER}, #{item.cussCheckOutCount,jdbcType=INTEGER},
        #{item.cussRoomCount,jdbcType=INTEGER}, #{item.cussFaceCount,jdbcType=INTEGER},#{item.deviceNo,jdbcType=VARCHAR})
    foreach>
  insert>

你可能感兴趣的:(mybatis)