mybatis帶條件的分頁查詢
<select id="queryApi" resultMap="ApiAlgorithm"> select id, system, api from config <if test="param.queryForm != null and param.queryForm.size()>0"> where (system, api) in <foreach collection="param.queryForm" item="item" open="(" separator="," close=")"> (#{item.system}, #{item.api}) </foreach> order by create_time desc, id desc limit #{param.from}, #{param.pageSize} </if> </select>
上面的語句中<foreach 中的collection是待遍歷的集合或數組,item對遍歷集合中的每個對象,open表示拼接sql的開始的字符,close表示拼接sql結束的字符,separator表示<foreach循環體內的相鄰(#{item.system}, #{item.api})之間的分隔符。 #{param.from}, #{param.pageSize}是分頁查詢的參數,注意這里的from是從0開始,pageSize是分頁查詢的大小。
當然上面的語句也可以不使用in操作,改成or也可以。
<select id="queryApi" resultMap="ApiAlgorithm"> select id, system, api from config <if test="param.queryForm != null and param.queryForm.size()>0"> where <foreach collection="param.queryForm" item="item" open="" separator=" or " close=""> (system =#{item.system}, api=#{item.api}) </foreach> order by create_time desc, id desc limit #{param.from}, #{param.pageSize} </if> </select>