Java Pageable - 每个页面从今天开始的日期不同

时间:2017-07-22 11:46:21

标签: java spring-mvc jpa

所以我想返回一个包含约会的分页。可分页中的每个页面应仅包含一天发生的约会。第0页应该是今天的日期,下一页将是明天发生的所有约会,并且会根据请求的页数继续。

如果您要求3页,您将获得从今天到后天的所有约会,每天都在自己的页面中。

我正在使用以下内容:

  • Spring Boot应用程序
  • JPARepository
  • MySQL数据库

这是我的服务工具的​​外观:

public Page<Appointment> PageAppointments(int page, int size) throws RestException{

    Pageable limit = new PageRequest(page, size);
    return appointmentRepository.findAllByOrderByAppointmentDateDesc(limit);

}

这就是我的存储库的外观:

public interface AppointmentRepository extends JpaRepository<Appointment, Long> {

    Page<Appointment> findAllByOrderByAppointmentDateDesc(Pageable pageable);

}

我正在寻找的结果是这样的:(概念)

//All Appointments taking place today
Page 1: {
    Appointment Entity with date 2017/07/22 11:19
    Appointment Entity with date 2017/07/22 14:08
    Appointment Entity with date 2017/07/22 15:21
    Appointment Entity with date 2017/07/22 16:44
}

//All Appointments taking place tomorrow
Page 2: {
    Appointment Entity with date 2017/07/23 11:19
    Appointment Entity with date 2017/07/23 14:08
    Appointment Entity with date 2017/07/23 15:21
    Appointment Entity with date 2017/07/23 15:34
    Appointment Entity with date 2017/07/23 16:11
    Appointment Entity with date 2017/07/23 16:44
}

//All Appointments taking place the day after tomorrow
Page 3: {
    Appointment Entity with date 2017/07/24 11:19
    Appointment Entity with date 2017/07/24 14:08
}

然后在这种情况下,如果我要求第4页,我应该从今天开始3天的所有约会,即2017/07/25。

非常感谢任何帮助。

提前致谢。

1 个答案:

答案 0 :(得分:2)

您不能将Pageable用作标准页面谓词,因为您需要一个非常自定义的寻呼机。 您可以使用Pageable和Page only作为Web级别的包装器。

主要关注点: 可分页用作额外谓词,其中section作为limit和offset。在你的情况下,你没有限制,因为你想在第N页显示日期的所有数据。此外,您没有偏移量,如您的情况offset = date。

您需要将几个查询包装到PageImpl。

列出内容= dao.loadAllAppointmentsForDate(date); Pageable pageable = new CustomePageRequest(.....); long total = dao.countUniqueDateForAppointments(); Page page = new PageImpl(内容,可分页,总计);

你需要创建CustomePageRequest(实现Pageable接口)并覆盖next(),previous(),first() , getPageNumber(),getPageSize().....,因为默认情况下他们使用页面大小进行calulation curent,prev和下一页,在你需要的情况下,可以做一些像今天这样的逻辑映射= 0页,明天=后天1页= 2 ....

相关问题