检查其他两个日期之间的日期 spring data jpa
Check date between two other dates spring data jpa
我有这个型号:
public class Event {
private String name;
private Date start;
private Date end;
}
存储库为
@Repository
public interface EventRepository extends JpaRepository<Event, Long> {
List<Event> findByEventTypeAccount(Account account);
}
我想做的是,我将传递一个日期并需要检查该日期是否在 start
和 end
之间,例如(我将 9 月 30 日作为日期,并且需要找到所有在 start
和 end
之间有 9 月 30 日的条目)
类似于 findDateisBetweenStartAndEnd(Date date)
?
你应该看看the reference documentation。解释的很好。
在你的情况下,我认为你不能使用 between 因为你需要传递两个参数
Between - findByStartDateBetween … where x.startDate between ?1 and ?2
在你的情况下,看看使用 LessThan
或 LessThanEqual
与 GreaterThan
或 GreaterThanEqual
的组合
- LessThan/LessThanEqual
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
您可以使用运算符 And
和 Or
来组合两者。
我确实使用了以下解决方案:
findAllByStartDateLessThanEqualAndEndDateGreaterThanEqual(OffsetDateTime endDate, OffsetDateTime startDate);
您还可以使用 @Query
编写自定义查询
@Query(value = "from EntityClassTable t where yourDate BETWEEN :startDate AND :endDate")
public List<EntityClassTable> getAllBetweenDates(@Param("startDate")Date startDate,@Param("endDate")Date endDate);
编辑:对于 LocalDateTime,我刚刚尝试了这个基于日期过滤记录的解决方案并在 spring JPA 中输入:
Page<MyEntity> findMyEntityByEntityDateBetweenAndType(
@Param("startDate") LocalDateTime startDate,
@Param("endDate") LocalDateTime endDate, @Param("type") String type, Pageable pageable);
这里EntityDate可以是CreatedDate,ModifiedDate等
也许你可以试试
List<Article> findAllByPublicationDate(Date publicationDate);
详情可查看这篇文章:
你可以使用 JpaRepository
List<Entity> findByColumnDateBetween(LocalDateTime to, LocalDateTime from);
我有这个型号:
public class Event {
private String name;
private Date start;
private Date end;
}
存储库为
@Repository
public interface EventRepository extends JpaRepository<Event, Long> {
List<Event> findByEventTypeAccount(Account account);
}
我想做的是,我将传递一个日期并需要检查该日期是否在 start
和 end
之间,例如(我将 9 月 30 日作为日期,并且需要找到所有在 start
和 end
之间有 9 月 30 日的条目)
类似于 findDateisBetweenStartAndEnd(Date date)
?
你应该看看the reference documentation。解释的很好。
在你的情况下,我认为你不能使用 between 因为你需要传递两个参数
Between - findByStartDateBetween … where x.startDate between ?1 and ?2
在你的情况下,看看使用 LessThan
或 LessThanEqual
与 GreaterThan
或 GreaterThanEqual
- LessThan/LessThanEqual
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
您可以使用运算符 And
和 Or
来组合两者。
我确实使用了以下解决方案:
findAllByStartDateLessThanEqualAndEndDateGreaterThanEqual(OffsetDateTime endDate, OffsetDateTime startDate);
您还可以使用 @Query
@Query(value = "from EntityClassTable t where yourDate BETWEEN :startDate AND :endDate")
public List<EntityClassTable> getAllBetweenDates(@Param("startDate")Date startDate,@Param("endDate")Date endDate);
编辑:对于 LocalDateTime,我刚刚尝试了这个基于日期过滤记录的解决方案并在 spring JPA 中输入:
Page<MyEntity> findMyEntityByEntityDateBetweenAndType(
@Param("startDate") LocalDateTime startDate,
@Param("endDate") LocalDateTime endDate, @Param("type") String type, Pageable pageable);
这里EntityDate可以是CreatedDate,ModifiedDate等
也许你可以试试
List<Article> findAllByPublicationDate(Date publicationDate);
详情可查看这篇文章:
你可以使用 JpaRepository
List<Entity> findByColumnDateBetween(LocalDateTime to, LocalDateTime from);