mybatis带条件的分页查询

<select >
	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 >
	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>

  

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2021-09-16
  • 2022-12-23
  • 2022-01-01
猜你喜欢
  • 2022-12-23
  • 2021-10-07
  • 2021-06-19
  • 2021-08-02
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案