mybatis执行批量更新update

mybatis执行批量更新update

Mybatis的批量插入这里有http://ljhzzyx.blog.163.com/blog/static/38380312201353536375/。目前想批量更新,如果update的值是相同的话,很简单,组织

update table set column='...' where id in (1,2,3)l
这样的sql就可以了。Mybatis中这样写就行
<update id="batchUpdateStudentWithMap" parameterType="java.util.Map" >
    UPDATE STUDENT SET name = #{name} WHERE id IN
    <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
        #{item}
    </foreach>
</update>
      但是这样的需求很少,一般是有个集合,每个元素中的值是不一样的,然后需要一次性更新。一般的处理方式是使用for循环。这样的效率较低,当数据量大时,期望有种一次性插入的操作。如果使用的是mysql,有
insert into table (aa,bb,cc) values(xx,xx,xx),(oo,oo,oo) on duplicate key update
replace into table (aa,bb,cc) values(xxx,xxx,xxx),(ooo,ooo,ooo),(ccc,ccc,ccc) 
两种方式可以处理。
      当前数据库是oracle,可以使用case when来拼成一长串sql处理
UPDATE mytable
    SET myfield = CASE id
        WHEN 1 THEN 'value'
        WHEN 2 THEN 'value'
        WHEN 3 THEN 'value'
    END
WHERE id IN (1,2,3)
实际上这种方式对于mysql也有效。
      最开始的时候,想着写一系列并列的更新语句就可以了
<update id="updateBatch" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" separator=";"
  open="" close="">
  update REGION_CODE set
    CODE=#{item.Code,jdbcType=VARCHAR},
    NAME=#{item.Name,jdbcType=VARCHAR}
    where ID = #{item.id,jdbcType=DECIMAL}
</foreach>
</update>
这样直接报错,因为Mybatis映射文件中的sql语句不允许 ; 符号。按照可行的case when处理方式,Mybatis映射文件书写方式如下:
<update id="updateBatch" parameterType="java.util.List">
  update REGION_CODE set
    CODE=
  <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
      when #{item.id,jdbcType=DECIMAL} then #{item.Code,jdbcType=VARCHAR}
  </foreach>
  ,NAME=
  <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
      when #{item.id,jdbcType=DECIMAL} then #{item.Name,jdbcType=VARCHAR}
  </foreach>
  where ID in
  <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
      #{item.id,jdbcType=DECIMAL}
  </foreach>
</update>
      至此,批量更新功能完成。

项目中实际使用案例:
[html]  view plain  copy
  1. <update id="updateForBatch" parameterType="java.util.List">  
  2.       update user_credit_black_list set  
  3.       type=  
  4.       <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">  
  5.         when #{item.id,jdbcType=BIGINT} then #{item.type,jdbcType=VARCHAR}  
  6.       </foreach>  
  7.       ,user_id=  
  8.       <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">  
  9.         when #{item.id,jdbcType=BIGINT} then #{item.userId,jdbcType=BIGINT}  
  10.       </foreach>  
  11.       ,update_time=  
  12.       <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">  
  13.         when #{item.id,jdbcType=BIGINT} then #{item.updateTime,jdbcType=TIMESTAMP}  
  14.       </foreach>  
  15.       ,delete_flg=  
  16.       <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">  
  17.         when #{item.id,jdbcType=BIGINT} then #{item.deleteFlg,jdbcType=BIT}  
  18.       </foreach>  
  19.       ,update_code=  
  20.       <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">  
  21.         when #{item.id,jdbcType=BIGINT} then #{item.updateCode,jdbcType=BIGINT}  
  22.       </foreach>  
  23.       where ID in  
  24.       <foreach collection="list" index="index" item="item" separator="," open="(" close=")">  
  25.         #{item.id,jdbcType=BIGINT}  
  26.       </foreach>  
  27.   </update>  

由于这种批量更新是一次执行多个update语句,所以mybatis需要额外的配置:

在spring.datasource.url后加上allowMultiQueries=true
如:jdbc:mysql://10.10.20.36:3306/testdb?allowMultiQueries=true

否则,在执行sql语句时会报错误

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值