[英]How to use fixed values in Spring CrudRepository?
Is it possible to add fixed values directly in a CrudRepository
query method?是否可以直接在
CrudRepository
查询方法中添加固定值? Like:喜欢:
findAdults(int age > 17);
findAllByBookingDateAndStatus(LocalDate bookingDate = LocalDate.now().minusDays(1), TypeStatus status = TypeStatus.FAILED);
As an alternative to custom @Query
'es, you may use default methods.作为自定义
@Query
的替代方法,您可以使用默认方法。
This is especially useful if default values are calculated in a "complex" way, eg LocalDate.now().minusDays(1)
.如果以“复杂”的方式计算默认值,这尤其有用,例如
LocalDate.now().minusDays(1)
。
For example:例如:
List<Person> findByAgeGreaterThan(int age);
default List<Person> findAdults() {
return findByAgeGreaterThan(17);
}
---
List<Booking> findAllByBookingDateAndStatus(LocalDate bookingDate, TypeStatus status);
default List<Booking> findAllYesterdaysFailedBookings{
return findAllByBookingDateAndStatus(LocalDate.now().minusDays(1), TypeStatus.FAILED);
}
For the first one simply use:对于第一个,只需使用:
findByAgeGreaterThan(Integer age);
For the second one, just provide the values to match like so:对于第二个,只需提供要匹配的值,如下所示:
findAllByBookingDateAndStatus(LocalDate bookingDate, TypeStatus status);
Spring undestand what you want using the used words "LessThan", "And", "Between" and the like in the name of the queries Spring 使用查询名称中的“LessThan”、“And”、“Between”等词来理解您想要的内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.