如何使用Spring JPA 检索某个记录器之前和之后的记录?

How to retrieve records before and after a certain recorder with Spring JPA?

我正在使用 Spring 存储库的自定义查询。我可以像这样创建自定义方法:

List<Order> findTop1ByOrderByCreateDateTimeDesc();

获取最近的订单。

但是如何获取按 CreateDateTime 排序的特定订单的上一个和下一个订单?

以下两个 JPQL 查询将return createDateTime 最接近指定的订单。

上一个订单:

select o from Order o where o.createDateTime = (select max(prev.createDateTime) from Order prev where prev.createDateTime < :specificOrderCreateDateTime)

下一个订单:

select o from Order o where o.createDateTime = (select min(next.createDateTime) from Order next where next.createDateTime > :specificOrderCreateDateTime)