JPA做分页查询
JpaRepository
有分页查询的函数,按API要求传递对应参数即可分页查询。
分页查询需要传入分页对象Pageable pageable = PageRequest.of(pageNum, pageSize);
关键代码如下:
//Repository
@Repository()
public interface ApplicationRepository extends JpaRepository<ApplicationDTO, Integer>{
Page<ApplicationDTO> findAll(Pageable pageable);
}
//Service
@Service
public ApplicationService {
@Autowired
private ApplicationRepository applicationRepository;
public Page<ApplicationDTO> getApps(Integer pageNum, Integer pageSize) {
if(Objects.isNull(pageNum)){
pageNum = 0;
}
if(Objects.isNull(pageSize)){
pageSize = 10;
}
Pageable pageable = PageRequest.of(pageNum, pageSize);
return applicationRepository.findAll(state,pageable);
}
}
敌人总是会在你最不想它出现的地方出现!