所以我想返回一个包含约会的可分页。页面中的每个页面应该只包含在一天内发生的约会。第0页应为今天的日期,下一页将是明天举行的所有约会,并根据所要求的页数继续下去。
如果你要求3页,你将得到所有的约会从今天到后天,每一天在它自己的网页。
我使用的方法如下:
以下是我的服务实现的外观:
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号。
任何帮助都将不胜感激。
提前谢谢。
发布于 2017-07-22 13:20:37
您不能使用Pageable作为标准页谓词,因为您需要一个非常定制的寻呼机。您只能使用Pageable和Page作为web级别的包装器。
主要注意事项:在where节中使用Pageable作为附加谓词作为限制,在offset.In中,您没有限制,因为您希望在N页中显示所有日期数据。此外,您也没有偏移量,在您的情况下,偏移量=日期。
您需要将几个查询打包到PageImpl中。
列表内容=dao.loadAllAppointmentsForDate(日期);可页可分页=新CustomePageRequest(.);长总计= dao.countUniqueDateForAppointments();页面页=新PageImpl(内容,可分页,总计);
您需要创建CustomePageRequest (实现可访问接口)和覆盖next(),previous(),first() , getPageNumber(),getPageSize().....,因为在默认情况下,它们使用页面大小来计算当前、prev和下一页,在您的情况下,您可能需要做一些逻辑映射,比如today=0页面,后天=2.
https://stackoverflow.com/questions/45254184
复制相似问题