gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:03:38 24 4
gpt4 key购买 nike

所以我想返回一个包含约会的可分页。 pageable 中的每一页都应该只包含发生在某一天的约会。第 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 。

    任何帮助将不胜感激。

    提前致谢。

    最佳答案

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

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

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

    列表内容 = dao.loadAllAppointmentsForDate(date);
    Pageable pageable = new CustomePageRequest(.....);
    long total = dao.countUniqueDateForAppointments();
    页面 page = new PageImpl(content ,pageable ,total );

    您需要创建CustomePageRequest(实现Pageable 接口(interface))并覆盖next(),previous(),first() , getPageNumber(),getPageSize().....默认情况下,他们使用页面大小计算当前、上一页和下一页,在您的情况下,您可能需要做一些逻辑映射,例如今天=0 页,明天=1 页后天= 2 ...。

    关于Java Pageable - 每页从今天开始的不同日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45254184/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com