当前位置 博文首页 > 小小舍的博客:mybatis批量更新和批量添加

    小小舍的博客:mybatis批量更新和批量添加

    作者:[db:作者] 时间:2021-08-09 18:52

    批量添加:

    <insert id="insertBatch" parameterType="java.util.List" useGeneratedKeys="true">
        insert into sys_order_csn (order_id,csn)
        values
        <foreach collection="list" item="order" index="index"  separator=",">
          (#{order.orderId,jdbcType=VARCHAR},#{order.csn,jdbcType=VARCHAR})
        </foreach>
      </insert>

    批量更新:

     <update id="updateBatch" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
          update sys_repair
          <set>
              <if test=" null != item.updateTime and '' != item.updateTime ">
                update_time = #{item.updateTime,jdbcType=TIMESTAMP},
              </if>
              <if test="null != item.customerName and '' != item.customerName ">
                customer_name = #{item.customerName,jdbcType=VARCHAR},
              </if>
              <if test="null != item.customerPhone and '' != item.customerPhone ">
                customer_phone = #{item.customerPhone,jdbcType=VARCHAR},
              </if>
              <if test="null != item.customerAddress and '' != item.customerAddress ">
                 customer_address = #{item.customerAddress,jdbcType=VARCHAR},
              </if>
              <if test="null != item.customerFaultDetail and '' != item.customerFaultDetail ">
                customer_fault_detail = #{item.customerFaultDetail,jdbcType=VARCHAR},
              </if>
          </set>
          where repair_id = #{item.repairId,jdbcType=VARCHAR}
        </foreach>
      </update>

    可能不是高效率方案。

    注意:db链接url后面带一个参数 ?&allowMultiQueries=true

    cs