1.修改的字段值都是一樣的,id不同
1
2
3
4
5
6
7
8
9
|
< update id= "batchUpdate" parameterType= "String" > update cbp_order set status=1 where id in <foreach item= "id" collection= "array" open = "(" separator= "," close = ")" > #{id} </foreach> </ update > ---參數說明--- |
collection:表示類型,就寫成array,如果是集合,就寫成list
item : 是一個變量名,自己隨便起名
2.這種方式,可以一次執行多條SQL語句
1
2
3
4
5
6
7
8
9
|
<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> |
3.整體批量更新
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
< update id= "updateBatch" parameterType= "java.util.List" > update mydata_table <trim prefix= "set" suffixOverrides= "," > <trim prefix= "status =case" suffix= "end," > <foreach collection= "list" item= "item" index = "index" > <if test= "item.status !=null and item.status != -1" > when id=#{item.id} then #{item.status} </if> <if test= "item.status == null or item.status == -1" > when id=#{item.id} then mydata_table.status//原數據 </if> </foreach> </trim> </trim> where id in <foreach collection= "list" index = "index" item= "item" separator= "," open = "(" close = ")" > #{item.id,jdbcType= BIGINT } </foreach> </ update > ----<trim>屬性說明------- |
1.prefix,suffix 表示在trim標簽包裹的部分的前面或者后面添加內容
2.如果同時有prefixOverrides,suffixOverrides 表示會用prefix,suffix覆蓋Overrides中的內容。
3.如果只有prefixOverrides,suffixOverrides 表示刪除開頭的或結尾的xxxOverides指定的內容。
總結
以上所述是小編給大家介紹的Mybatis批量修改的操作代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://blog.csdn.net/qq_39365823/article/details/78122820