MyBatis - 使用foreach迭代List/Array的說明

        在 MyBatis 中的 foreach 元素,主要用於迭代 集合數據 以動態生成執行語句;主要有 item、index、collection、open、separator、close 等屬性

屬性說明

        collection:要迭代的數據集對象,必填項

        item:迭代出的元素的別名,必填項

        index:元素的序號(map時爲key),可選項

        open:迭代開始符,比如in的左括號,可選項

        close:迭代的關閉符,比如in的右括號,可選項

        separator:元素之間的分隔符,比如in的逗號,可選項

Collection

       別名註解 org.apache.ibatis.annotations.Param 類

       在單個參數的 Dao 接口聲明中,將 List 或 Array 作爲參數傳遞給 MyBatis 時

              默認會將它包裝在一個 Map 中並以名稱爲鍵,即 List 以 list 爲鍵(collection=list),Array 以 array 鍵(collection=array)

              如果使用 @Param("alias") 對參數進行了指定命名,則參數在 Map 中的鍵以指定命名爲準(collection=alias)

        當多個參數在 Dao 接口聲明時,無論將什麼類型的參數傳遞到 MyBatis 中

              所有參數會被一起封裝成 Map 對象,參數默認使用 arg[index] 作爲鍵(collection=arg0)

              如果使用 @Param("alias") 對參數進行了指定命名,則參數在 Map 中的鍵以指定命名爲準(collection=alias)

       支持內嵌屬性通過點路徑訪問,比如 User.Role.List<Permission> 式的對象,當以 User 爲參數時

              單參數無別名時,collection="role.permissions"

              多數/別名/Map時,collection="user.role.permissions"

判斷長度

        List:<if test="list != null and list.size() > 0"> </if>

        Array:<if test="array != null and array.length > 0"> </if>

使用示例

  •     單參數 List 未別名
public List<Student> findStuList(List<String> clsIds);
<select id="findStuList" resultType="com.xl.entity.Student">
    SELECT * FROM student WHERE cls_id IN
    <foreach item="clsId" collection="list" separator="," open="(" close=")" index="index">
        #{clsId}
    </foreach>
</select>
  •     單參數 Array 未別名
public List<Student> findStuList(String[] clsIds);
<select id="findStuList" resultType="com.xl.entity.Student">
    SELECT * FROM student WHERE cls_id IN
    <foreach item="clsId" collection="array" separator="," open="(" close=")" index="index">
        #{clsId}
    </foreach>
</select>
  •     單參數 List/Array 定別名
public List<Student> findStuList(@Param("clsIdList") List<String> clsIds);
<select id="findStuList" resultType="com.xl.entity.Student">
    SELECT * FROM student WHERE user_id IN
    <foreach item="clsId" collection="clsIdList" separator="," open="(" close=")" index="index">
        #{clsId}
    </foreach>
</select>
  •     單參數 對象.List 未別名
/**
 * stu.setClsIds(new ArrayList<String>());
 */
public List<Student> findStuList(StuVo stuVo);
<select id="findStuList" resultType="com.xl.entity.Student">
    SELECT * FROM student WHERE user_id IN
    <foreach item="clsId" collection="clsIds" separator="," open="(" close=")" index="index">
        #{clsId}
    </foreach>
</select>
  •     單參數 對象.List 定命名
/**
 * stu.setClsIds(new ArrayList<String>());
 */
public List<Student> findStuList(@Param("stuVo") StuVo stuVo);
<select id="findStuList" resultType="com.xl.entity.Student">
    SELECT * FROM student WHERE user_id IN
    <foreach item="clsId" collection="stuVo.clsIds" separator="," open="(" close=")" index="index">
        #{clsId}
    </foreach>
</select>
  •     單參數 List<對象>.屬性 未別名
public List<Student> findStuList(List<City> citys);
<select id="findStuList" resultType="com.xl.entity.Student">
    SELECT * FROM student WHERE city_id IN
    <foreach item="city" collection="list" separator="," open="(" close=")" index="index">
        #{city.id}
    </foreach>
</select>
  •     多參數 List和Array 
/**
 *map.put("clsIdList", clsIds);
 *map.put("cityIdList", cityIds);
 */
public List<Student> findStuList(Map<String, Object> map);
public List<Student> findStuList(@Param("clsIdList") List<String> clsIds, @Param("cityIdList") String[] cityIds);
<select id="findStuList" resultType="com.xl.entity.Student">
    SELECT * FROM student WHERE user_id IN
    <foreach item="clsId" collection="clsIdList" separator="," open="(" close=")" index="index">
        #{clsId}
    </foreach>
    AND city_id IN
    <foreach item="cityId" collection="cityIdList" separator="," open="(" close=")" index="index">
        #{cityId}
    </foreach>
</select>

 

 

發佈了234 篇原創文章 · 獲贊 53 · 訪問量 49萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章