一、xml文件中foreach的主要屬性
foreach元素的屬性主要有 collection,item,index,separator,open,close。
collection: 表示集合,數據源
item :表示集合中的每一個元素
index :用于表示在迭代過程中,每次迭代到的位置
separator :表示在迭代時數據以什么符號作為分隔符
open :表示該語句以什么開始
close :表示以什么結束二、foreach批量查詢數據
1、當查詢的參數只有一個時例如:findByIds(List ids)
a.如果參數類型為List,在使用時,collection的屬性需指定為list
b.如果參數類型為數組,則在使用時,collection屬性需指定為array<select id="findByIdsMap" resultMap="BaseResultMap">
? ? ? ? ?Select ?sum(mark)
? ? ? ? ?from table_user where user_id in
? ? ? ? ? ? ? ? ? <foreach item="item" index="index" collection="list"?
? ? ? ? ? ? ? ? ? ? ? ? ?open="(" separator="," close=")">
? ? ? ? ? ? ? ? ? ? ? ? #{item}
? ? ? ? ? ? ? ? </foreach>
? </select>?
2、當查詢的參數是一個對象時例如:findByIds(List userList)
<select id="findByIdsMap" resultMap="BaseResultMap">
? ? ? ? ?Select sum(mark)
? ? ? ? ?from table_user where user_id in
? ? ? ? ? ? ? ? ? <foreach item="item" index="index" collection="list"?
? ? ? ? ? ? ? ? ? ? ? ? ?open="(" separator="," close=")">
? ? ? ? ? ? ? ? ? ? ? ? #{item.userId}
? ? ? ? ? ? ? ? </foreach>
? </select>?
三、foreach批量插入數據
實現foreach批量插入數據有兩種方法,一種是只發送一條 SQL,插入的多條數據之間通過”,” 分隔開,另一種方式是每插入一條數據就發送一條 SQL 語句,多個 SQL 語句之間用“;”分割。1.一條 SQL 批量插入數據
對應的Mapper接口代碼如下:/** 返回值為 Integer 類型 */
Integer addStudentByList(@Param("list") List<Student> list);
1
2
對應的SQL 映射文件如下:<insert id="addStudentByList" parameterType="">
? ? INSERT INTO Student(username, age, password) VALUES?
? ? <foreach collection="list" item="item" separator=",">
? ? ? ? (#{ item.username}, #{ item.age}, #{ item.password})
? ? </foreach>
</insert>
2.執行多條 SQL 批量插入數據
對應的Mapper接口代碼和一條 SQL 批量插入數據相同
對應的SQL 映射文件在一條 SQL 批量插入數據的基礎上修改如下:<insert id="addEmpsByList" parameterType="com.jas.mybatis.bean.Employee">
? ? <!-- 每插入一條數據就執行一次 SQL,中間用";"分隔開 ?-->
? ? <foreach collection="list" item="emp" separator=";">
? ? ? ? INSERT INTO Student(username, age, password) VALUES
? ? ? ? (#{ item.username}, #{ item.age}, #{ item.password})
? ? </foreach>
</insert>
三、foreach批量更新數據
實現foreach批量插入數據有兩種種方法,第一種是一條sql語句來批量更新數據,第二種是批量更新的sql。一條 SQL 批量更新數據
一條sql語句來批量更新所有數據,下面直接看一下在mybatis中通常是怎么寫的(去掉mybatis語法就是原生的sql語句了,所有就沒單獨說sql是怎么寫的)
對應的SQL 映射文件如下:<update id="updateBatch" parameterType="java.util.List">
? ? update Student set ?username=
? ? <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
? ? ? ? when #{ item.id} then #{ item.username}
? ? </foreach>
? ? where id in
? ? <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
? ? ? ? #{ item.id,jdbcType=BIGINT}
? ? </foreach>
</update>
<------------------------------------分隔符-------------------------------->
?<update id="updateBatch" ?parameterType="java.util.List"> ?
? ? <foreach collection="list" item="item" index="index" open="" close="" separator=";">
? ? ? ? update Student
? ? ? ? <set>
? ? ? ? ? ? username=${ item.username}
? ? ? ? </set>
? ? ? ? where id = ${ item.id}
? ? </foreach> ? ? ?
</update>
四、foreach批量刪除數據
所對應的SQL映射文件如下:<delete id="delAssetstype" parameterType="java.util.List">
? ? ? ?DELETE FROM WHERE id in
? ? ? ?<foreach collection="list" item="item" open="(" close=")" separator=",">
? ? ? ? ? ?#{ item}
? ? ? ?</foreach>
? ?</delete>
對應的Mapper接口代碼如下:public void deleteAssetstype(List<Integer> ids);