ibatis 批量更新

本文介绍了一种使用SQL及MyBatis进行高效批量更新的方法,通过案例展示了如何利用CASE WHEN语句来更新特定条件下的记录,并提供了MyBatis XML配置示例,包括动态SQL元素的使用技巧。

一条条更新效率太低,以下方式将提高效率。当然还有replace into的方式,但是该方式需要所有的字段,否则就为null。并且是根据主键或者是唯一索引来更新,有时候并不方便。

更新的sql:

    update tblsupertitleresult set result =case   
      
    when (userHhCode=2001 and titleId=1)then  90  
       
    when (userHhCode=2001 and titleId=2)then  70  
       
    end  
       
    ,checkState = case    
      
    when (userHhCode=2001 and titleId=1)then  80  
       
    when (userHhCode=2001 andtitleId=2)then  120  
       
    end  
       
    where (userHhCode=2001 and titleId=1) or(userHhCode=2001 and titleId=2)  

xml配置:

<update id="batchUpdateKeywords">
        update sem_keywords
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="platPlanId = case" suffix="end,">
                <foreach collection="list" item="it" index="index">
                        when platKeywordsId=#{it.platKeywordsId}  then #{it.platPlanId}
                </foreach>
            </trim>
             </trim>
        where
        <foreach collection="list" separator="or" item="it" index="index">
            (id= #{it.id} )
        </foreach>
    </update>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值