for each

          


                             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>


转载于:https://my.oschina.net/u/2491968/blog/522909

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值