mybatis 中foreach的三種遍歷

  1. 各參數解釋
    collection :collection屬性的值有三個分別是list、array、map三種,分別對應的參數類型爲:List、數組、map集合,我在上面傳的參數爲數組,所以值爲array
    item : 表示在迭代過程中每一個元素的別名
    index :表示在迭代過程中每次迭代到的位置(下標)
    open :前綴
    close :後綴
    separator :分隔符,表示迭代時每個元素之間以什麼分隔
  2. 數組
          int[] ids = {1,2,3,4,5}
        <select id="getTeam" parameterType="java.util.arraylist" resultType="Team">
             <foreach collection="array" index="index" item="item" open="(" separator="," close=")">
              #{item}
          </foreach>
        </select>

3 .Map

        <select id="getTeam" parameterType="java.util.HashMap" resultType="Team">
            select * from team where title like "%"#{name}"%" and id in
             <foreach collection="keys" index="index" item="item" open="(" separator="," close=")">
                  #{item}
             </foreach>
       </select>

4 .List

         <select id="getTeam" parameterType="java.util.List" resultType="Team">
               select * from team where id in
           <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
                   #{item}       
           </foreach>    
        </select>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章