Java 检查其他两个日期之间的日期 spring 数据 jpa

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39784344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:27:02  来源:igfitidea点击:

Check date between two other dates spring data jpa

javaspringspring-data-jpaspring-repositories

提问by doN

I have this model:

我有这个模型:

public class Event {
    private String name;
    private Date start;
    private Date end;
}

and repository as

和存储库作为

@Repository
public interface EventRepository extends JpaRepository<Event, Long> {
    List<Event> findByEventTypeAccount(Account account);
}

What I want to do is, I will pass one date and need to check that date is between startand endeg(i will pass sept 30 as date and need to find all entries which have sept 30 between their startand end)

我想要做的是,我将通过一个日期并需要检查该日期是否介于startend例如(我将通过 9 月 30 日作为日期,并且需要找到在他们start和之间 9 月 30 日的所有条目end

Something like findDateisBetweenStartAndEnd(Date date)?

findDateisBetweenStartAndEnd(Date date)什么?

采纳答案by Pau

You should take a look the reference documentation. It's well explained.

您应该查看参考文档。这很好解释。

In your case, I think you cannot use between because you need to pass two parameters

在你的情况下,我认为你不能使用 between 因为你需要传递两个参数

Between- findByStartDateBetween … where x.startDate between ?1 and ?2

之间- findByStartDateBetween ... 其中 x.startDate 介于 ?1 和 ?2 之间

In your case take a look to use a combination of LessThanor LessThanEqualwith GreaterThanor GreaterThanEqual

在您的情况下,请查看使用LessThanLessThanEqualGreaterThan或的组合GreaterThanEqual

  • LessThan/LessThanEqual
  • 小于/小于等于

LessThan - findByEndLessThan … where x.start< ?1

LessThanEqual findByEndLessThanEqual … where x.start <= ?1

LessThan - findByEndLessThan … where x.start< ?1

LessThanEqual findByEndLessThanEqual ... where x.start <= ?1

  • GreaterThan/GreaterThanEqual
  • 大于/大于等于

GreaterThan - findByStartGreaterThan … where x.end> ?1

GreaterThanEqual - findByStartGreaterThanEqual … where x.end>= ?1

GreaterThan - findByStartGreaterThan ... 其中 x.end> ?1

GreaterThanEqual - findByStartGreaterThanEqual ... 其中 x.end>= ?1

You can use the operator Andand Orto combine both.

您可以使用运算符AndandOr将两者结合起来。

回答by Micha?l Cuypers

I did use following solution to this:

我确实使用了以下解决方案:

findAllByStartDateLessThanEqualAndEndDateGreaterThanEqual(OffsetDateTime endDate, OffsetDateTime startDate);

回答by Vikash Kumar

You can also write a custom query using @Query

您还可以使用编写自定义查询 @Query

@Query(value = "from EntityClassTable t where yourDate BETWEEN :startDate AND :endDate")
public List<EntityClassTable> getAllBetweenDates(@Param("startDate")Date startDate,@Param("endDate")Date endDate);