SELECT * FROM USER WHERE id=1 OR id=10 OR id=16java
SELECT * FROM USER WHERE id IN(1,10,27)sql
<!-- 定義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>