springdata jpa 关于分页@Query问题
关于springdata jpa 分页问题相信很多小伙伴都遇到过,只要表中数量到达分页条件就会报错
废话少说直接上代码:
@Query(nativeQuery = true,
value = "select id, company_id,address_name,address_detail,phone_num, regist_date,update_date,delete_flag,update_id FROM delivery_address WHERE delete_flag='0' AND IF ( ?1!=1, company_id = ?1, 1=1) AND IF ( ?2 is null,1=1,address_name LIKE CONCAT('%',?2,'%'))",
countQuery = "select count(id) FROM delivery_address WHERE delete_flag='0' AND IF ( ?1!=1, company_id = ?1, 1=1) AND IF ( ?2 is null,1=1,address_name LIKE CONCAT('%',?2,'%'))")
public Page<DeliveryAddress> findAllByCompanyId(Integer companyId,String addressName,Pageable pageable);
其中
1.@Query 这个是jpa自定定义sql语句时用到的注解
2.nativeQuery代表value中的语句为 sql语句,而非hql语句
3. countQuery代表当前分页的总页数,如果不设置这个参数相信你的分页一定不顺利。
4.该语句中还有一些条件判断等,要学习的小伙伴自行研究
转载需表明来源,谢谢配合,侵权必究