如果利用@Query就行分页主要用的属性有
nativeQuery
value
countQuery
@Query(nativeQuery = true
,value = "你的sql"
, countQuery = "你的sql ")
Page<实体类> queryUser(@Param("vo") VO vo, @Param("pageable") Pageable pageable);
这里需要注意 nativeQuery countQuery 中的sql条件需要一样
看下简单的例子
@Query(nativeQuery = true
,value = "select u.id,user.name,g.goodsName from user u left join goods g on g.user_id = u.id where g.name=:#{#vo.goodsName}"
, countQuery = "select count(u.id) from user u left join goods g on g.user_id = u.id where g.name=:#{#vo.goodsName}")
Page<实体类> queryUser(@Param("vo") VO vo, @Param("pageable") Pageable pageable);
如果你需要动态查询条件使用sql的 if 函数 if(condition, value_if_true, value_if_false)
例如: 可以像下面这么写条件
where 1=1 and if(:#{#vo.name !='',g.name=:#{#vo.goodsName},1=1)