1、批量插入
-
<insert
-
id= "insertBatch"
-
parameterType= "java.util.List">
-
insert into
-
t_student(name, age, class)
-
values
-
<foreach collection="list" item="item" index="index" separator=",">
-
(
-
-
-
-
)
-
</ foreach>
-
</insert>
2、批量更新
方式一:
-
<update id= "updateBatch">
-
< foreach collection="list" separator=";" item="stud">
-
update t_studetn set
-
name =
-
age =
-
class =
-
where id =
-
</ foreach>
-
</update>
方式二:
-
<update id= "updateBatch" parameterType="list">
-
UPDATE t_student
-
SET name = CASE id
-
<foreach collection= "list" item="i" index="index">
-
WHEN
-
THEN
-
</foreach>
-
END,
-
age = CASE id
-
<foreach collection= "list" item="i" index="index">
-
WHEN
-
</foreach>
-
END
-
WHERE id IN
-
<foreach collection= "list" separator="or" item="i" index="index" >
-
id=
-
</foreach>
-
</update>
3、批量刪除
-
< delete id="deleteBatchByParams">
-
delete from
-
t_student
-
where
-
id IN
-
< foreach collection="ids" item="item" index="index" open="("close=")"separator=",">
-
#{item}
-
< /foreach>
-
</delete>
item | 循環體中的具體對象。支持屬性的點路徑訪問,如item.age,item.info.details。 具體說明:在list和數組中是其中的對象,在map中是value。 該參數為必選。 |
collection | 要做foreach的對象,作為入參時,List<?>對象默認用list代替作為鍵,數組對象有array代替作為鍵,Map對象沒有默認的鍵。 |
separator | 元素之間的分隔符,例如在in()的時候,separator=","會自動在元素中間用“,“隔開,避免手動輸入逗號導致sql錯誤,如in(1,2,)這樣。該參數可選。 |
open | foreach代碼的開始符號,一般是(和close=")"合用。常用在in(),values()時。該參數可選。 |
close | foreach代碼的關閉符號,一般是)和open="("合用。常用在in(),values()時。該參數可選。 |
index | 在list和數組中,index是元素的序號,在map中,index是元素的key,該參數可選。 |
https://blog.csdn.net/u014252478/article/details/91386289