Mybatis中进行批量更新

参考博文:

Mybatis中进行批量更新(updateBatch)

mybatis 实现批量更新

MySQL没有提供直接的方法来实现批量更新,但可以使用case when语法来实现这个功能。

语法:

UPDATE course
    SET name = CASE id 
        WHEN 1 THEN 'name1'
        WHEN 2 THEN 'name2'
        WHEN 3 THEN 'name3'
    END, 
    title = CASE id 
        WHEN 1 THEN 'New Title 1'
        WHEN 2 THEN 'New Title 2'
        WHEN 3 THEN 'New Title 3'
    END
WHERE id IN (1,2,3)

这条sql的意思是,如果id为1,则name的值为name1,title的值为New Title1;依此类推。

Mybatis样例:

<update id="updateBylists" parameterType="list">
    update cm_track_replay 
    set   province = 
    <foreach collection="list" item="item" index="index" 
        separator=" " open="case id" close="end,">
        when #{item.id} then #{item.province}
    </foreach>
      city =
    <foreach collection="list" item="item" index="index" 
        separator=" " open="case id" close="end,">
        when #{item.id} then #{item.city }
    </foreach>
      district =
    <foreach collection="list" item="item" index="index" 
        separator=" " open="case id" close="end,">
        when #{item.id} then #{item.district}
    </foreach>
      street =
    <foreach collection="list" item="item" index="index" 
        separator=" " open="case id" close="end">
        when #{item.id} then #{item.street}
    </foreach>
    where id in
    <foreach collection="list" index="index" item="item" 
        separator="," open="(" close=")">
        #{item.id,jdbcType=BIGINT}
    </foreach>
 </update>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值