mybatis父子表批量插入

Posted java渣渣

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis父子表批量插入相关的知识,希望对你有一定的参考价值。

 <!--父子表批量插入  -->
<insert id="insertBatch" parameterType="com.niwopay.dto.benifit.JFOrderVipDTO">
begin
           insert into TB_JF_ORDER
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="orderId != null" >
        ORDER_ID,
      </if>
      <if test="extOrderId != null" >
        EXT_ORDER_ID,
      </if>
      <if test="deviceChannel != null" >
        DEVICE_CHANNEL,
      </if>
      <if test="bizChannel != null" >
        BIZ_CHANNEL,
      </if>
      <if test="orderType != null" >
        ORDER_TYPE,
      </if>
      <if test="userId != null" >
        USER_ID,
      </if>
      <if test="orderDate != null" >
        ORDER_DATE,
      </if>
      <if test="orderAmount != null" >
        ORDER_AMOUNT,
      </if>
      <if test="productAmount != null" >
        PRODUCT_AMOUNT,
      </if>
      <if test="payAmount != null" >
        PAY_AMOUNT,
      </if>
      <if test="fee != null" >
        FEE,
      </if>
      <if test="sales != null" >
        SALES,
      </if>
      <if test="freight != null" >
        FREIGHT,
      </if>
      <if test="deliveryState != null" >
        DELIVERY_STATE,
      </if>
      <if test="orderState != null" >
        ORDER_STATE,
      </if>
      <if test="orderDesc != null" >
        ORDER_DESC,
      </if>
      <if test="remark != null" >
        REMARK,
      </if>
      <if test="createDate != null" >
        CREATE_DATE,
      </if>
      <if test="payDate != null" >
        PAY_DATE,
      </if>
      <if test="finishDate != null" >
        FINISH_DATE,
      </if>
      <if test="returnDate != null" >
        RETURN_DATE,
      </if>
      <if test="closeDate != null" >
        CLOSE_DATE,
      </if>
      <if test="deliveryDate != null" >
        DELIVERY_DATE,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="orderId != null" >
        #{orderId,jdbcType=VARCHAR},
      </if>
      <if test="extOrderId != null" >
        #{extOrderId,jdbcType=VARCHAR},
      </if>
      <if test="deviceChannel != null" >
        #{deviceChannel,jdbcType=VARCHAR},
      </if>
      <if test="bizChannel != null" >
        #{bizChannel,jdbcType=VARCHAR},
      </if>
      <if test="orderType != null" >
        #{orderType,jdbcType=CHAR},
      </if>
      <if test="userId != null" >
        #{userId,jdbcType=VARCHAR},
      </if>
      <if test="orderDate != null" >
        #{orderDate,jdbcType=CHAR},
      </if>
      <if test="orderAmount != null" >
        #{orderAmount,jdbcType=VARCHAR},
      </if>
      <if test="productAmount != null" >
        #{productAmount,jdbcType=VARCHAR},
      </if>
      <if test="payAmount != null" >
        #{payAmount,jdbcType=VARCHAR},
      </if>
      <if test="fee != null" >
        #{fee,jdbcType=VARCHAR},
      </if>
      <if test="sales != null" >
        #{sales,jdbcType=VARCHAR},
      </if>
      <if test="freight != null" >
        #{freight,jdbcType=VARCHAR},
      </if>
      <if test="deliveryState != null" >
        #{deliveryState,jdbcType=CHAR},
      </if>
      <if test="orderState != null" >
        #{orderState,jdbcType=CHAR},
      </if>
      <if test="orderDesc != null" >
        #{orderDesc,jdbcType=VARCHAR},
      </if>
      <if test="remark != null" >
        #{remark,jdbcType=VARCHAR},
      </if>
      <if test="createDate != null" >
        #{createDate,jdbcType=DATE},
      </if>
      <if test="payDate != null" >
        #{payDate,jdbcType=DATE},
      </if>
      <if test="finishDate != null" >
        #{finishDate,jdbcType=DATE},
      </if>
      <if test="returnDate != null" >
        #{returnDate,jdbcType=DATE},
      </if>
      <if test="closeDate != null" >
        #{closeDate,jdbcType=DATE},
      </if>
      <if test="deliveryDate != null" >
        #{deliveryDate,jdbcType=DATE},
      </if>
    </trim>;
    
    insert into TB_JF_ORDER_VIP
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="orderId != null" >
        ORDER_ID,
      </if>
      <if test="accountName != null" >
        ACCOUNT_NAME,
      </if>
      <if test="cardNo != null" >
        CARD_NO,
      </if>
      <if test="bankName != null" >
        BANK_NAME,
      </if>
      <if test="province != null" >
        PROVINCE,
      </if>
      <if test="city != null" >
        CITY,
      </if>
      <if test="branch != null" >
        BRANCH,
      </if>
      <if test="cpTradingStatus != null" >
        CP_TRADING_STATUS,
      </if>
      <if test="bankCardId != null" >
        BANK_CARD_ID,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="orderId != null" >
        #{orderId,jdbcType=VARCHAR},
      </if>
      <if test="accountName != null" >
        #{accountName,jdbcType=VARCHAR},
      </if>
      <if test="cardNo != null" >
        #{cardNo,jdbcType=VARCHAR},
      </if>
      <if test="bankName != null" >
        #{bankName,jdbcType=VARCHAR},
      </if>
      <if test="province != null" >
        #{province,jdbcType=VARCHAR},
      </if>
      <if test="city != null" >
        #{city,jdbcType=VARCHAR},
      </if>
      <if test="branch != null" >
        #{branch,jdbcType=VARCHAR},
      </if>
      <if test="cpTradingStatus != null" >
        #{cpTradingStatus,jdbcType=CHAR},
      </if>
      <if test="bankCardId != null" >
        #{bankCardId,jdbcType=CHAR},
      </if>
    </trim>;
    end;
</insert>

1、然后调用public int batchUpdate(final String statementName, final List<?> parameters);传入paramter参数的集合即可。

这种方式使用批量插入父子表。

2、不需要批量插入,但是插入一张主表的同时插入多张字表

  <insert id="insertQuotePlan" parameterType="com.taolue.api.interf.insurance.dto.QuotePlanDTO">
    begin
    insert into TB_CAR_INSURANCE_QUOTA_PLAN
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="QuotePlanID != null" >
        QUOTA_PLAN_ID,
      </if>
      <if test="userId != null" >
        USER_ID,
      </if>
      <if test="CityID != null" >
        CITY_ID,
      </if>
      <if test="VipCarID != null" >
        VIP_CAR_ID,
      </if>
      <if test="InsureStartDate != null" >
        INSURE_START_DATE,
      </if>
      <if test="BusinessStartDate != null" >
        BUSINESS_START_DATE,
      </if>
      <if test="createTime != null" >
        CREATE_TIME,
      </if>
      <if test="itemKind != null" >
        INTEM_KIND,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="QuotePlanID != null" >
        #{QuotePlanID,jdbcType=VARCHAR},
      </if>
      <if test="userId != null" >
        #{userId,jdbcType=VARCHAR},
      </if>
      <if test="CityID != null" >
        #{CityID,jdbcType=VARCHAR},
      </if>
      <if test="VipCarID != null" >
        #{VipCarID,jdbcType=VARCHAR},
      </if>
      <if test="InsureStartDate != null" >
        #{InsureStartDate,jdbcType=VARCHAR},
      </if>
      <if test="BusinessStartDate != null" >
        #{BusinessStartDate,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        #{createTime,jdbcType=DATE},
      </if>
      <if test="itemKind != null" >
        #{itemKind,jdbcType=CLOB},
      </if>
    </trim>;
    <if test="quotePriceList != null and quotePriceList.size>0" >
    <foreach collection="quotePriceList" item="item" index="index">
       insert into TB_CAR_INSURANCE_QUOTA_PRICE (quota_id,quota_plan_id,company_id,company_name,company_code) values (#{item.quotaId},#{QuotePlanID},#{item.companyId},#{item.companyName},#{item.companyCode});
    </foreach>
     </if>
    end;
   </insert> 

 

以上是关于mybatis父子表批量插入的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis+Oracle --批量插入

Mybatis Plus 批量插入性能优化,非常实用!

MySQL+MyBatis一条命令批量插入或更新

MySQL+MyBatis一条命令批量插入或更新

Java--MyBatis批量插入批量更新和批量删除

mybatis+mysql批量插入和批量更新