如何使用 Hibernate 在 Spring Boot 中实现分页 [英] How to implement pagination in spring boot with hibernate
本文介绍了如何使用 Hibernate 在 Spring Boot 中实现分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将 Spring Boot 与 Hibernate 结合使用,并且我想在我的项目中使用分页.我在谷歌上搜索并看到了很多例子,但我无法在我的项目中实现它.
I am using spring boot with hibernate and I want to use pagination in my project. I have searched on google and saw many examples but I am unable to implement it in my project.
我想,如果我在我的 url 中传递 1,那么应该会出现 10 个结果,如果我传递 2,那么接下来的 10 个结果应该会出现,依此类推.
I want like if I pass 1 in my url then 10 results should come and if I pass 2 then next 10 results should come and so on.
这是我的道
@Transactional
public interface PostDao extends CrudRepository<Post, Long>{
@Query(getAllPostsByRank)
List<Post> getAllPostsByRank();
final String getAllPostsByRank= "from Post order by value DESC";
}
这是我的控制器
@RequestMapping("/top")
@ResponseBody
public List<Post> getAllPosts(HttpServletRequest req, HttpServletResponse res) throws ServletException {
List<Post> postobj = postDao.getAllPostsByRank();
return postobj;
}
这是我的网址:
http://localhost:8888/v1.0/post/top/1
请提出建议.
推荐答案
检查一下.您的控制器
@RequestMapping("/top/pages/{pageno}")
@ResponseBody
public List<Post> getAllPosts(@PathVariable("pageno") int pageno, HttpServletRequest req, HttpServletResponse res) throws ServletException {
List<Post> postobj = postDao.getAllPostsByRank(new PageRequest(pageno,10));
return postobj;
}
你的道
@Transactional
public interface PostDao extends CrudRepository<Post, Long>{
@Query(getAllPostsByRank)
List<Post> getAllPostsByRank(Pageable pageable);
final String getAllPostsByRank= "from Post order by value DESC";
}
这篇关于如何使用 Hibernate 在 Spring Boot 中实现分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文