1. 程式人生 > >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'    ENDWHERE 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>      至此,批量更新功能完成。專案中實際使用案例:
  1. <
    updateid="updateForBatch"parameterType="java.util.List">
  2.       update user_credit_black_list set  
  3.       type=  
  4.       <foreachcollection="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.       <foreachcollection="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.       <foreachcollection="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.       <foreachcollection="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.       <foreachcollection="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.       <foreachcollection="list"index="index"item="item"separator=","open="("close=")">
  25.         #{item.id,jdbcType=BIGINT}  
  26.       </foreach>
  27.   </update>