- foreach
输入的列表、数组、map转化为SQL语句中的用括号括起来的字符串
例子:
Mapper接口定义的方法:UserList为模拟返回的数据对象
List<UserList> getUserInfo(@Param("userName") List<String> userName);
Mapper.xml动态sql构建,下面foreach标签内的值可以将数组转化为类似(1,2,3)的字符串
<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>
下面SQL转化的例子
SELECT * FROM user_info WHERE (USERNAME,AGE) IN (('张三','26'),('李四','58'),('王五','27'),......);
实际的foreach标签
<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>
网友评论