MyBatis-循环-Foreach

14次阅读

共计 3289 个字符,预计需要花费 9 分钟才能阅读完成。

Mybatis 之 foreach 用法 —-List、Array、Map 三种类型遍历

原文地址

在 mybatis 的 xml 文件中构建动静 sql 语句时,常常会用到标签遍历查问条件。特此记录下不同状况下书写形式!——- 仅供大家参考 ——

1. foreach 元素的属性

  • collection: 需做 foreach(遍历) 的对象,作为入参时,list、array 对象时,collection 属性值别离默认用 ”list”、”array” 代替,Map 对象没有默认的属性值。然而,在作为入参时能够应用 @Param(“keyName”) 注解来设置自定义 collection 属性值,设置 keyName 后,list、array 会生效;
  • item: 汇合元素迭代时的别名称,该参数为必选项;
  • index: 在 list、array 中,index 为元素的序号索引。然而在 Map 中,index 为遍历元素的 key 值,该参数为可选项;
  • open: 遍历汇合时的开始符号,通常与 close=”)” 搭配应用。应用场景 IN(),values() 时,该参数为可选项;
  • separator: 元素之间的分隔符,类比在 IN() 的时候,separator=”,”, 最终所有遍历的元素将会以设定的(,)逗号符号隔开,该参数为可选项;
  • close: 遍历汇合时的完结符号,通常与 open=”(“ 搭配应用,该参数为可选项;

2.foreach 时,collection 属性值的三种状况:

  • 如果传入的参数类型为 List 时,collection 的默认属性值为 list, 同样能够应用 @Param 注解自定义 keyName;
  • 如果传入的参数类型为 array 时,collection 的默认属性值为 array, 同样能够应用 @Param 注解自定义 keyName;
  • 如果传入的参数类型为 Map 时,collection 的属性值可为三种状况:(1. 遍历 map.keys;2. 遍历 map.values;3. 遍历 map.entrySet()), 稍后会在代码中示例;

3. 代码示例:

3.1 collection 属性值类型为 List:

Mapper 接口定义的办法:UserList 为模仿返回的数据对象

List<UserList> getUserInfo(@Param("userName") List<String> userName);

Mapper.xml 动静 sql 构建,Mapper 接口的办法名和 xml 文件的 id 值,必须一一对应,否则会报错:
—– 倡议做 if test=”xxxx!=null and xxxx.size()>0″ 的校验,比拟谨严。array 为.length();

 <select id="getUserInfo" resultType="com.test.UserList">
          SELECT
            *
          FROM user_info
            where
            <if test="userName!= null and userName.size() >0">
                USERNAME IN
                <foreach collection="userName" item="value" separator="," open="(" close=")">
                    #{value}
                </foreach>
            </if>
</select>

3.2 collection 属性值类型为 Array:
Mapper 接口定义的办法:UserList 为模仿返回的数据对象

List<UserList> getUserInfo(@Param("userName") String[] userName);

Mapper.xml 动静 sql 构建,Mapper 接口的办法名和 xml 文件的 id 值,必须一一对应,否则会报错:
—– 倡议做 if test=”xxxx!=null and xxxx.length()>0″ 的校验,比拟谨严。

     <select id="getUserInfo" resultType="com.test.UserList">
            SELECT
                *
            FROM user_info
            where
            <if test="userName!= null and userName.length() >0">
                USERNAME IN
                <foreach collection="userName" item="value" separator="," open="(" close=")">
                    #{value}
                </foreach>
            </if>
     </select>

3.3 collection 属性值类型为 Map:
近期在我的项目中,遇到这样一个需要,sql 查问的条件之一是两个字段确定惟一的一个人,并且条件可能是多集体或者一个人。以往开发中,咱们可能遇到只有一个字段的汇合或者数组。进入正题:
接管前台传递的 List,组装查问条件。其实也能够间接传一个 List 对象汇合,然而理论开发中,List 中的每一个对象蕴含了几十个字段,而咱们只须要其中的两个,所以我抉择数据组装传递。
实现类解决:

  Map<String, String> patientMap = userList.stream().filter(item -> StringUtils.hasText(item.getUserName()) &&
                    StringUtils.hasText(item.getAge())).collect(Collectors.toMap(UserList::getUserName, UserList::getAge));
                    

Mapper 接口定义的办法:UserList 为模仿返回的数据对象

List<UserList> getUserInfo(@Param("user") Map<String,String> user);

Mapper.xml 动静 sql 构建,Mapper 接口的办法名和 xml 文件的 id 值,必须一一对应,否则会报错:
—– 倡议做 if test=”xxxx!=null and xxxx.size()>0″ 的校验,比拟谨严。
第一种:获取 Map 的键值对,多字段组合条件状况下,肯定要留神书写格局:括号()

eg: SELECT * FROM user_info WHERE (USERNAME,AGE) IN (('张三','26'),('李四','58'),('王五','27'),......);
 <select id="getUserInfo" resultType="com.test.UserList">
            SELECT
                *
            FROM user_info
            where
            <if test="user!= null and user.size() >0">
                (USERNAME,AGE) IN
                <foreach collection="user.entrySet()" item="value" index="key" separator="," open="(" close=")">
                    (#{key},#{value})
                </foreach>
            </if>
</select>

第二种:参数 Map 类型,只须要获取 key 值或者 value 值
key:

 <select id="getUserInfo" resultType="com.test.UserList">
            SELECT
                *
            FROM user_info
            where
            <if test="user!= null and user.size() >0">
                (USERNAME) IN
                <foreach collection="user.keys" item="key"  separator="," open="(" close=")">
                    #{key}
                </foreach>
            </if>
</select>

value:

     <select id="getUserInfo" resultType="com.test.UserList">
            SELECT
                *
            FROM user_info
            where
            <if test="user!= null and user.size() >0">
                (USERNAME) IN
                <foreach collection="user.values" item="value"  separator="," open="(" close=")">
                    #{key}
                </foreach>
            </if>
</select>

正文完
 0