Java 需要一个JPA查询,对过去4小时发布的帖子进行排序
@存储库 公共接口PostRepository扩展了JpaRepository{Java 需要一个JPA查询,对过去4小时发布的帖子进行排序,java,spring,spring-boot,jpa,jpql,Java,Spring,Spring Boot,Jpa,Jpql,@存储库 公共接口PostRepository扩展了JpaRepository{ Page<Post> findAllByCategory(Pageable pageable, Category category); LocalDate dateBefore4Hours = LocalDate.now().minus(4, ChronoUnit.HOURS); //tried but couldnt figured it out @Query("SELECT a FROM P
Page<Post> findAllByCategory(Pageable pageable, Category category);
LocalDate dateBefore4Hours = LocalDate.now().minus(4, ChronoUnit.HOURS); //tried but couldnt figured it out
@Query("SELECT a FROM Post a WHERE a.createdDate ") //none of sql keywords i've tried here didnt wor
Page<Post> findAllWithCreationDateTimeInLastFourHours();
Page findAllByCategory(可分页、可分页、类别);
LocalDate dateBefore4Hours=LocalDate.now().minus(4,ChronoUnit.HOURS);//尝试过,但无法解决
@Query(“SELECT a FROM a WHERE a.createdDate”)//我在这里尝试过的sql关键字都不起作用
页面findAllWithCreationDateTimeInLastFourHours();
}如果需要比较时间,可以使用
LocalDateTime
,因为LocalDate
没有时间组件。因此,要获取4小时前的LocalDateTime
,可以执行以下操作:
LocalDateTime CurrentTimeMins4Hours=LocalDateTime.now().MinsHours(4L);
然后,您可以在查询中使用它并对列进行排序,如下所示:
@Query(“从a.createdDate>:currentTimeMinus4Hours ORDER BY a.createdDate中选择一个帖子”)
Page findRecentPosts(LocalDateTime CurrentTime最少4小时);
顺便说一句,我是新手。太多了,我想它奏效了。因此,我假设当我定义一个方法时,我可以使用“:colon:”right?与jpql一起使用它。是的,您可以在jpql查询中使用“:argument”引用方法参数。如果答案解决了问题,请记住将其标记为正确答案。谢谢