JPA Repository findAll where creationDate exceeds set time

JPA Repository findAll where creationDate exceeds set time

我正在开发一个应用程序,它使用调度程序来检查数据库中是否存在早于设定天数的任何数据并删除该数据。我 运行 遇到了计划问题,因为我正在努力寻找有关如何以符合良好做法的方式完成此操作的具体信息。我不确定如何使用 JPA 存储库查找超过特定日期的所有内容。

@Repository
public interface MyDataRepository extends JpaRepository<Data, String> {

//how to add: WHERE Date exceeds 10 days
findAll();

}

如有任何帮助/指点,我将不胜感激。

您需要在服务方法中计算出10天,并将计算出的天数作为参数传递:

@Query("select d from Data d where a.createdAt <= :d10DaysAgo")
List<Article> findAllWithCreationDateTimeBefore(LocalDate d10DaysAgo)

此处有更多相关信息:https://www.baeldung.com/spring-data-jpa-query-by-date