ssm中批量update語句
阿新 • • 發佈:2019-02-09
最近遇到了一個關於批量修改的需求,想了一種方法,不知道是否有更好的解法,求指教(雖然並沒人看)
1.需求是處理一個list物件,根據物件的批次號和專案id屬性批量更改資料庫一個狀態欄位。
舉例表資料如下:
pch | xmid | status |
---|---|---|
abc | cba | 0 |
bcd | cba | 0 |
abd | cba | 0 |
型別為List<Map<String,Object>>
的集合如下:
[
{
"pch": "abc",
"xmid": "cba"
},
{
"pch ": "bcd",
"xmid": "cba"
}
]
將符合該集合條件的資料status記錄改為1
2.Mapper.xml語句為:
<update id="batchUpdate" parametertype="java.util.List">
update table_name t
set t.status=
( case
<foreach collection="list" item="item" index="index" open ="" close="" separator="">
when t.pch=#{item.pch,jdbcType=VARCHAR} and t.xmid=#{item.xmid,jdbcType=VARCHAR} then 1
</foreach>
else t.status end )
where t.pch in
<foreach collection="list" item="item" index="index" open="(" close =")" separator=",">
'${item.pch}'
</foreach>
</update>