Mybatis 批量更新实体对象

(1)Dao层接口:

    /**
     * 根据更新采购计划(批量)
     * @param plans
     */
    void batchUpdatePlan(List<PubPurchasePlan> plans);

(2)Mapper.xml 文件:

<sql id="batchUpdatePlanCondition">
    <where>
        <foreach collection="list" item="item" open="( " separator=") or (" close=" )">
            comId = #{item.comId} AND id = #{item.id}
        </foreach>
    </where>
</sql>
<update id="batchUpdatePlan" parameterType="list">
        UPDATE pub_purchase_plan
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="warehouseId=case" suffix="end,">
                 <foreach collection="list" item="item" index="index">
                    WHEN comId = #{item.comId} AND id = #{item.id} THEN #{item.warehouseId}
                 </foreach>
            </trim>
            <trim prefix="productId=case" suffix="end,">
                 <foreach collection="list" item="item" index="index">
                   WHEN comId = #{item.comId} AND id = #{item.id} THEN #{item.productId}
                 </foreach>
            </trim>
            <trim prefix="amount=case" suffix="end,">
                 <foreach collection="list" item="item" index="index">
                   WHEN comId = #{item.comId} AND id = #{item.id} THEN #{item.amount}
                 </foreach>
            </trim>
            <trim prefix="deleted=case" suffix="end,">
                 <foreach collection="list" item="item" index="index">
                   WHEN comId = #{item.comId} AND id = #{item.id} THEN #{item.deleted}
                 </foreach>
            </trim>
            <trim prefix="price=case" suffix="end,">
                 <foreach collection="list" item="item" index="index">
                   WHEN comId = #{item.comId} AND id = #{item.id} THEN #{item.price}
                 </foreach>
            </trim>
            <trim prefix="type=case" suffix="end,">
                 <foreach collection="list" item="item" index="index">
                   WHEN comId = #{item.comId} AND id = #{item.id} THEN #{item.type}
                 </foreach>
            </trim>
        </trim>
        <include refid="batchUpdatePlanCondition"/>
</update>

 

相关推荐
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页