mybatis 批量更新

参考文章:

https://blog.csdn.net/xu1916659422/article/details/77971696/

https://blog.csdn.net/xyjawq1/article/details/74129316

最近用到了批量更新操作,网络了一下,具体采用了下面的方式。

目前没有考虑执行效率。

 示例一

 <update id="checkBatchUpdate" parameterType="java.util.List">
        update warehouse_inventory
        <trim prefix="set" suffixOverrides=",">
            <!-- 总库存-->
            <trim prefix="total_store =case" suffix="end,">
                <foreach collection="list" item="item">
                    <if test="item.totalStore != null">
                        when id=#{item.id} then #{item.totalStore,jdbcType=DECIMAL}
                    </if>
                </foreach>
            </trim>
            <!-- 可用库存-->
            <trim prefix="apply_store =case" suffix="end,">
                <foreach collection="list" item="item">
                    <if test="item.applyStore != null">
                        when id=#{item.id} then #{item.applyStore,jdbcType=DECIMAL}
                    </if>
                </foreach>
            </trim>
            <!-- 占用库存-->
            <trim prefix="occ_store =case" suffix="end,">
                <foreach collection="list" item="item">
                    <if test="item.occStore != null">
                        when id=#{item.id} then #{item.occStore,jdbcType=DECIMAL}
                    </if>
                </foreach>
            </trim>
            <!-- 冻结库存-->
            <trim prefix="frozen_store =case" suffix="end,">
                <foreach collection="list" item="item">
                    <if test="item.frozenStore != null">
                        when id=#{item.id} then #{item.frozenStore,jdbcType=DECIMAL}
                    </if>
                </foreach>
            </trim>
            <trim prefix="update_user =case" suffix="end,">
                <foreach collection="list" item="item">
                    <if test="item.updateUser != null">
                        when id=#{item.id} then #{item.updateUser,jdbcType=BIGINT}
                    </if>
                </foreach>
            </trim>
            <trim prefix="update_time =case" suffix="end,">
                <foreach collection="list" item="item">
                        when id=#{item.id} then now()
                </foreach>
            </trim>
        </trim>
        <where>
            <foreach collection="list" separator="or" item="item">
                id = #{item.id}
            </foreach>
        </where>
    </update>

mapper接口层:

Integer checkBatchUpdate(@Param("list") List<WarehouseInventory> list);

我这里list设置两条数据ids【354910763363991553,354910763368185857】,最后转换后的sql:

2019-08-15 19:04:24 [main] DEBUG c.r.s.w.m.W.checkBatchUpdate - ==>  Preparing: update warehouse_inventory set total_store =case when id=? then ? when id=? then ? end, apply_store =case when id=? then ? when id=? then ? end, occ_store =case when id=? then ? when id=? then ? end, frozen_store =case when id=? then ? when id=? then ? end, update_time =case when id=? then now() when id=? then now() end WHERE id = ? or id = ?  
2019-08-15 19:04:24 [main] DEBUG c.r.s.w.m.W.checkBatchUpdate - ==> Parameters: 354910763363991553(Long), 100(BigDecimal), 354910763368185857(Long), 100(BigDecimal), 354910763363991553(Long), 100(BigDecimal), 354910763368185857(Long), 100(BigDecimal), 354910763363991553(Long), 0.00(BigDecimal), 354910763368185857(Long), 0.00(BigDecimal), 354910763363991553(Long), 0.00(BigDecimal), 354910763368185857(Long), 0.00(BigDecimal), 354910763363991553(Long), 354910763368185857(Long), 354910763363991553(Long), 354910763368185857(Long) 
2019-08-15 19:04:24 [main] DEBUG c.r.s.w.m.W.checkBatchUpdate - <==    Updates: 2 

示例二

这里还用到了ON DUPLICATE KEY UPDATE,作用:没有新增,有则更新。

<!--更新暂存区库存-->
    <insert id="updateInventoryInTemp" parameterType="java.util.List">

        insert into warehouse_inventory
        values
        <foreach collection="list" index="index" separator="," item="item">
            (#{item.id,jdbcType=BIGINT},
            #{item.storeId,jdbcType=BIGINT},
            #{item.locationId,jdbcType=BIGINT},
            #{item.skuId,jdbcType=BIGINT},
            #{item.totalStore,jdbcType=DECIMAL},
            #{item.applyStore,jdbcType=DECIMAL},
            #{item.occStore,jdbcType=DECIMAL},
            #{item.frozenStore,jdbcType=DECIMAL},
            #{item.customerId,jdbcType=BIGINT},
            #{item.qualityType,jdbcType=TINYINT},
            #{item.batchNo,jdbcType=BIGINT},
            now(),
            #{item.createUser,jdbcType=BIGINT},
            #{item.updateTime,jdbcType=TIMESTAMP},
            #{item.updateUser,jdbcType=BIGINT}
            )
        </foreach>
        on duplicate key update frozen_store = (case
        <foreach collection="list" item="item" separator=" " index="index" open=" " close="">
            when id = #{item.id,jdbcType=BIGINT} then
            frozen_store + #{item.frozenStore,jdbcType=DECIMAL}
        </foreach>
        end
        ),
        total_store = (case
        <foreach collection="list" item="item" separator=" " index="index" open=" " close="">
            when id = #{item.id,jdbcType=BIGINT} then
            total_store + #{item.totalStore,jdbcType=DECIMAL}
        </foreach>
        END
        ),
        update_user = (case
        <foreach collection="list" item="item" separator=" " index="index" open=" " close="">
            when id = #{item.id,jdbcType=BIGINT} then
            #{item.updateUser,jdbcType=BIGINT}
        </foreach>
        end),
        update_time = (case
        <foreach collection="list" item="item" separator=" " index="index" open=" " close="">
            when id = #{item.id,jdbcType=BIGINT} then
            now()
        </foreach>
        end)
    </insert>

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值