mybatis執行批量更新update
阿新 • • 發佈:2019-01-02
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- <
- update user_credit_black_list set
- type=
- <foreachcollection="list"item="item"index="index"separator=" "open="case ID"close="end">
- when #{item.id,jdbcType=BIGINT} then #{item.type,jdbcType=VARCHAR}
- </foreach
- ,user_id=
- <foreachcollection="list"item="item"index="index"separator=" "open="case ID"close="end">
- when #{item.id,jdbcType=BIGINT} then #{item.userId,jdbcType=BIGINT}
- </foreach>
- ,update_time=
- <foreachcollection="list"item="item"index="index"separator=" "open="case ID"close="end">
- when #{item.id,jdbcType=BIGINT} then #{item.updateTime,jdbcType=TIMESTAMP}
- </foreach>
- ,delete_flg=
- <foreachcollection="list"item="item"index="index"separator=" "open="case ID"close="end">
- when #{item.id,jdbcType=BIGINT} then #{item.deleteFlg,jdbcType=BIT}
- </foreach>
- ,update_code=
- <foreachcollection="list"item="item"index="index"separator=" "open="case ID"close="end">
- when #{item.id,jdbcType=BIGINT} then #{item.updateCode,jdbcType=BIGINT}
- </foreach>
- where ID in
- <foreachcollection="list"index="index"item="item"separator=","open="("close=")">
- #{item.id,jdbcType=BIGINT}
- </foreach>
- </update>