如何将多个日期搜索与Spring Data JPA的CrudRepository结合起来?

spring-data提供了一种通过定义方法名称来生成SQL搜索的方法。

以下工作正常:

@Entity public class Book { Date from, to; } //CrudRepository findByFromDateBetween(Date departure, Date arrival); 

但为什么以下不起作用呢?

 findByFromDateBetweenAndToDateBetween(Date departure, Date arrival); 

要连接两个日期搜索,我必须重复日期:

 findByFromDateBetweenAndToDateBetween(Date departure, Date arrival, Date departure, Date arrival); 

问题:是否可以重复使用参数?

Between关键字自然地绑定两个参数。 因此,在绑定from子句之后,参数列表已用完,我们不知道哪个参数用于第二个条件。

手动定义的查询应该可以解决这个问题:

 interface BookRepository extends Repository { @Query("select b from Book b " + "where b.from between ?1 and ?2 and b.to between ?1 and ?2") List findByDatesBetween(Date departure, Date arrival); }