How to implement pagination in spring boot with hibernate

后端 未结 5 1058
野的像风
野的像风 2020-12-15 07:21

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 proje

5条回答
  •  星月不相逢
    2020-12-15 08:16

    Check it. Your controller

    @RequestMapping("/top/pages/{pageno}")
        @ResponseBody 
         public List getAllPosts(@PathVariable("pageno") int pageno, HttpServletRequest req, HttpServletResponse res) throws ServletException {
    
    List postobj = postDao.getAllPostsByRank(new PageRequest(pageno,10));
    return postobj;
    }
    

    Your dao

    @Transactional
    public interface PostDao extends CrudRepository{
    
    @Query(getAllPostsByRank)
    List getAllPostsByRank(Pageable pageable);
    
    final String getAllPostsByRank= "from Post order by value DESC";
    }
    

提交回复
热议问题