【Mybatis】【10】foreach 批量操做

正文:sql

1,foreach屬性spa

屬性 是否必選 描述
item 元素進行迭代時的別名
collection 要作迭代的數據集合
separator 元素之間的分隔符
open 表示該語句以什麼開始
close 表示該語句以什麼結束
index 每次迭代到的位置

2,select count(1) from user where id in ( ? , ? ) .net

必定要注意到$和#的區別,$的參數直接輸出,#的參數會被替換爲?,而後傳入參數值執行。code

<select id="test" resultType="int">  
  select count(1) from user  
  <where>  
    id in  
    <foreach item="item" collection="list" separator="," open="(" close=")" index="">  
      #{item.id}  
    </foreach>  
  </where>  
</select> 

3,select count(1) from user  where col_a = ? and col_b = ?  blog

<select id="test" resultType="int">  
    select count(1) from user where  
    <foreach item="item" index="key" collection="map" open="" separator="AND" close="">
    ${key} = #{item}
    </foreach>  
</select>

 

4,Oracle :insert into user select ?,? from dual union all select ?,? from dualget

<insert id="test">        
    insert into user (id, name)     
    <foreach collection="userList" item="item" separator="UNION ALL">
        select #{item.id}, #{item.name} from user_temp
    </foreach>
</insert>

5,Mysql :insert into user(key, value) values (?, ?) , (?, ?) 博客

<insert id="test">  
    insert into user(key, value) values  
    <foreach item="item" index="key" collection="map" open="" separator="," close="">
    (#{key}, #{item})
    </foreach>  
</insert> 

參考博客:it

Mybatis foreach 批量操做 - jason5186 - CSDN博客
https://blog.csdn.net/jason5186/article/details/40896043io

相關文章
相關標籤/搜索