mybatis + mysql batch update

Recent project development is having problems encountered in the original, to the forgotten, check online again, this time recorded here

I have here is the most versatile batch updates (for readability deleted some fields)

Code

<update id="updateBatch" parameterType="java.util.List" >
    <foreach collection="list" item="item" index="index" open="" close="" separator=";">
        update purchase_order_detail
        <set >          
            <if test='item.consumeCode !=null and item.consumeCode !=""' >consumeCode = #{item.consumeCode},</if>
            <if test='item.consumeName !=null and item.consumeName !=""' >consumeName = #{item.consumeName},</if>          
            <if test='item.productId !=null and item.productId !=""' >productId = #{item.productId},</if>         
        </set>
        WHERE purchaseOrderDetailId = #{item.purchaseOrderDetailId}
    </foreach>
</update>

 



However, when an error has been executed, was not too careful analysis, found just a debugging code behind each case, an update of the data is possible, but not more than it is passed in the parameter list size is greater than 1:00 unenforceable;
sensory problems gradually clear, then the online search a bit, could be the problem description is not clear enough, there are a number of other online batch update statement
to update the same value like this
<update id="updateStatus">
    UPDATE purchase_order_detail
    set status= #{status}   
    WHERE id in
    <foreach collection="ids" index="index" item="id" separator="," open="(" close=")">
        #{id}
    </foreach>
</update>

 



There is such a
<update id="updateListByHouseId" parameterType="java.util.ArrayList">
    <foreach close=";" collection="list" index="index" item="item" open="" separator=";">
        update rba_house_status
        <trim prefix="set" suffixOverrides=",">
            last_push_time = now(),
            <if test="item.pushStatus != null">push_status = #{item.pushStatus},</if>
            <if test="item.createStatus != null">create_status = #{item.createStatus},</if>
            <if test="item.houseCreateTime != null">house_create_time = #{item.houseCreateTime},</if>
            <if test="item.updateStatus != null">update_status = #{item.updateStatus},</if>
            update_time = now(),
            <if test="item.auditStatus != null">audit_status = #{item.auditStatus},</if>
            <if test="item.auditDesc != null">audit_desc = #{item.auditDesc},</if>
            <if test="item.hotelId != null">hotel_id = #{item.hotelId},</if>
        </trim>
        <where>house_id = #{item.houseId}</where>
    </foreach>
</update>

 



Watching feel legibility is poor; still want to use the first update in batch and then checked the relevant information, which comes to mysql need to turn supports batch execution sql statement ------ it is this "; "separated sql statement in the batch execution need to add support msyql
added after the database connection string
allowMultiQueries=true


E.g
url: jdbc:mysql://127.0.0.1:3306/clouddo?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true


Development: At this point I just do not think about what to ask when added to support the second local loop condition as you can enforce it, including some local circulation of the insert statement
<insert id="insertBatch">
    INSERT INTO purchase_order(
        purchaseOrderId,
        purchaseOrderNo,
        purchaseOrderName,
        purchaseApplyNo,
        purchaseOrderType,
        purchaseOrderTypeName,
        supplierId,
        supplierName,
        hosptialId,
        hosptialName,
        purchaseOrderAmount,
        purchaseOrderStatus,
        purchaseOrderDate,
        shippingAddress,
        shippingPerson,
        shippingPhone
    )
    VALUES
    <foreach collection ="list" item="purchaseOrder" separator =",">
    (
        #{purchaseOrder.purchaseOrderId},
        #{purchaseOrder.purchaseOrderNo},
        #{purchaseOrder.purchaseOrderName},
        #{purchaseOrder.purchaseApplyNo},
        #{purchaseOrder.purchaseOrderType},
        #{purchaseOrder.purchaseOrderTypeName},
        #{purchaseOrder.supplierId},
        #{purchaseOrder.supplierName},
        #{purchaseOrder.hosptialId},
        #{purchaseOrder.hosptialName},
        #{purchaseOrder.purchaseOrderAmount},
        #{purchaseOrder.purchaseOrderStatus},
        #{purchaseOrder.purchaseOrderDate},
        #{purchaseOrder.shippingAddress},
        #{purchaseOrder.shippingPerson},
        #{purchaseOrder.shippingPhone}
    )
    </foreach>
</insert>

 



Try holding the mentality I'm in the background sql print out

INSERT INTO distribution_order_detail (
distributionOrderNo,
distributionOrderDetailNo,
purchaseOrderNo,
purchaseOrderDetailNo,
consumeCode,
)
VALUES
(    ?,    ?,    ?,    ?,    ?    ),
(    ?,    ?,    ?,    ?,    ?    )

 

 

At this point it is discovered after the sql statement to achieve the purpose, although the bulk of it is just a statement and with the first batch of new sql statement is just two sql statement with ";" to separate the

update purchase_order_detail SET waitDistributionCount = ? WHERE purchaseOrderDetailId = ? ;

update purchase_order_detail SET waitDistributionCount = ? WHERE purchaseOrderDetailId = ? 

 

 

The second batch updates modify the same value even more of a sql

UPDATE consume_material SET consumeStatus = ? WHERE consumeId in ( ? , ? )

 

These are only personal understanding and as a record, imperfect hope you help correct it.

 



 

Guess you like

Origin www.cnblogs.com/yutf/p/11271992.html