Mybatis 带条件的分页查询

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>

  

原文地址:https://www.cnblogs.com/wylwyl/p/13790035.html