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>
      至此,批量更新功能完成。
 
--------------------------------------------------------------------------------------------------------------------------------
 
原mybatis執行批量更新batch update 的方法(oracle,mysql)
 

oracle數據庫:

<update id="batchUpdate" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="begin" close="end;" separator=";"> update test <set> test=${item.test}+1 </set> where id = ${item.id} </foreach> </update>


mysql數據庫:

mysql數據庫采用一下寫法即可執行,但是數據庫連接必須配置:&allowMultiQueries=true

例如:jdbc:mysql://192.168.1.236:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&allowMultiQueries=true

<update id="batchUpdate" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="" close="" separator=";"> update test <set> test=${item.test}+1 </set> where id = ${item.id} </foreach> </update>


------------------------------------------------------------------------------------------------------------------------------

https://my.oschina.net/zouqun/blog/405424

--------------------------------------------------------------------------------------------------------------------------------

 

mybatis執行批量更新update 的方法oracle小記

標簽: mybatisjava批量更新
<update id="updateLicaiAllList" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";">
   update tmi_licai_all t 
   set 
   t.est_amount=#{item.estAmount,jdbcType=NUMERIC}
   where t.licai_id = #{item.licaiId,jdbcType=NUMERIC}
   </foreach>
</update>
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM