您的位置:首页 > 其它

for each

2015-10-28 00:00 134 查看
SELECT * FROM USER WHERE id=1 OR id=10 OR id=16

SELECT * FROM USER WHERE id IN(1,10,27)

<!-- 定义sql片段 查询条件 -->
<sql id="user_query_where">
<if test="userCustom!=null">
<if test="userCustom.sex!=null and userCustom.sex!=''">
user.sex=#{userCustom.sex}
</if>

<if test="userCustom.username!=null and userCustom.username!=''">

user.username LIKE '%${userCustom.username}%'
</if>

<if test="ids!=null">
<!-- 使用foreach遍历传入的ids -->
<!--collection 指定输入对象的集合属性 item 每个遍历生成的对象 open 开始遍历时拼接的串 close 结束遍历时拼接的串
separator 遍历时两个对象需要拼接的串 -->
<foreach collection="ids" item="id" open="  and (" close=")" separator="or">

id=#{id}

</foreach>

</if>
</if>

</sql>

另外一种方法

<select id="selectUserByList" parameterType="java.util.List" resultType="user">
select * from user
<where>
<!-- 传递List,List中是pojo -->
<if test="list!=null">
<foreach collection="list" item="item" open="and id in("separator=","close=")">
#{item.id}
</foreach>
</if>
</where>
</select>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: