首页 课程 师资 教程 报名

JPA分页查询的方法

  • 2022-11-22 08:30:47
  • 1012次 星辉

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);
    }
}

 

选你想看

你适合学Java吗?4大专业测评方法

代码逻辑 吸收能力 技术学习能力 综合素质

先测评确定适合在学习

在线申请免费测试名额
价值1998元实验班免费学
姓名
手机
提交