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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章