基於JPA的分頁/排序實現

Page<ClassOrder> findByMember_MemberID(long id, Pageable pageable);

Controller代碼:
 public ModelAndView showsignupclass(int page){
        ModelAndView mv=new ModelAndView("/user/user-showSignupClass.btl");
        Member member=memberRepository.findAll().get(0);
        int size=5; 
    //按報名時間倒序
     Sort sort = new Sort(Sort.Direction.DESC,"time");
        PageRequest pageable=PageRequest.of(page,size,sort);
        Page<ClassOrder> pageObject=classOrderRepository.findByMember_MemberID(member.getMemberID(),pageable);    //獲取pageObject的內容
        List<ClassOrder> classOrders=pageObject.getContent();
        //getTotalPages()返回總的頁數
        int totalPage=pageObject.getTotalPages();
        //getTotalElements()返回總數
        long count=pageObject.getTotalElements();
        mv.addObject("count", count);
        mv.addObject("totalPage",totalPage);
        mv.addObject("classOrders",classOrders);
        return mv;
    }
相關文章
相關標籤/搜索