How to paginate a JPA Query

前端 未结 3 606
心在旅途
心在旅途 2021-01-30 11:52

I have a submission table with columns like ID, Name, Code among other properties. My requirement is to search for records based on the me

3条回答
  •  生来不讨喜
    2021-01-30 11:58

    You can use Pageable in the repository method in Spring

    @Repository
    public interface StateRepository extends JpaRepository {
    
    @Query("select state from State state where state.stateId.stateCode = ?1")
    public State findStateByCode(String code, Pageable pageable);
    

    }

    And in the service layer, you can create the Pageable object:

    @Autowire
    StateRepository stateRepository;
    
    public State findStateServiceByCode(String code, int page, int size) {
        Pageable pageable = new PageRequest(page, size);
        Page statePage = stateRepository.findStateByCode(code, pageable);
        return statePage.getContent();
    }
    

提交回复
热议问题