【问题标题】:Spring data findByStartDateBetween doesn't work春季数据 findByStartDateBetween 不起作用
【发布时间】:2015-02-27 01:33:40
【问题描述】:

我编写了一个 Spring MVC 应用程序。现在我尝试实现spring数据存储库,它有一个方法可以通过日期参数找到所有Deal对象。

我试图实现这一点,就像在 Spring Data 网站 (findByStartDateBetween) 上一样:

 @Transactional(readOnly = true)
 public interface DealRepository extends JpaRepository <Deal, Long> {    
    List<Deal> findByStartDateBetween(Date from, Date to);

但它不起作用。它无法创建DealRepository bean。

我也尝试写一些自定义的@Query,但也失败了。

你能给我一些建议吗?

【问题讨论】:

  • 您需要包含更多实现 findByStartDateBetween 的代码。

标签: java spring spring-mvc spring-data


【解决方案1】:

您需要有@Repository 表示法,它理想地在应用程序开始时创建 bean。

【讨论】:

    【解决方案2】:

    这应该可以工作

    List<Deal> findByCreatedDateBetween(Date start, Date end);
    

    【讨论】:

      【解决方案3】:

      解决了这个问题。我写了自定义查询,重要的是不要忘记使用@Param。这就是我得到的

       @Transactional(readOnly = true)
      public interface DealRepository extends JpaRepository<Deal, Long> {
      
          @Modifying
          @Transactional
          @Query("select d from Deal d where d.createdDate >= :from and d.createdDate <= :to ")
          List<Deal> findByCreatedDateBetween(@Param("from") Date from, @Param("to") Date to);
      

      【讨论】:

      • 不是你问的。这是一种解决方法,但不是答案。
      猜你喜欢
      • 2022-01-12
      • 2017-12-13
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-22
      • 2016-08-20
      相关资源
      最近更新 更多