查询关键字
-and
And 例如:findByUsernameAndPassword(String user, Striang pwd);
-or
Or 例如:findByUsernameOrAddress(String user, String addr);
-between
Between 例如:SalaryBetween(int max, int min);
-"<"
LessThan 例如: findBySalaryLessThan(int max);
-">"
GreaterThan 例如: findBySalaryGreaterThan(int min);
-is null
IsNull 例如: findByUsernameIsNull();
-is not null
IsNotNull NotNull 与 IsNotNull 等价 例如: findByUsernameIsNotNull();
-like
Like 例如: findByUsernameLike(String user);
-not like
NotLike 例如: findByUsernameNotLike(String user);
-order by
OrderBy 例如: findByUsernameOrderByNameAsc(String user);直接通过name正序排序
-"!="
Not 例如: findByUsernameNot(String user);
-in
In 例如: findByUsernameIn(Collection<String> userList) ,方法的参数可以是 Collection 类型,也可以是数组或者不定长参数;
-not in
NotIn 例如: findByUsernameNotIn(Collection<String> userList) ,方法的参数可以是 Collection 类型,也可以是数组或者不定长参数;
-Top/Limit
查询方法结果的数量可以通过关键字来限制,first 或者 top都可以使用。top/first加数字可以指定要返回最大结果的大小 默认为1
例如:
-
User findFirstByOrderByLastnameAsc();
-
User findTopByOrderByAgeDesc();
-
Page<User> queryFirst10ByLastname(String lastname, Pageable pageable);
-
Slice<User> findTop3ByLastname(String lastname, Pageable pageable);
-
List<User> findFirst10ByLastname(String lastname, Sort sort);
-
List<User> findTop10ByLastname(String lastname, Pageable pageable);
关键词 |
示例 |
对应的sql片段 |
And |
findByLastnameAndFirstname |
… where x.lastname = ?1 and x.firstname = ?2 |
Or |
findByLastnameOrFirstname |
… where x.lastname = ?1 or x.firstname = ?2 |
Is,Equals |
findByFirstname ,findByFirstnameIs ,findByFirstnameEquals |
… where x.firstname = ?1 |
Between |
findByStartDateBetween |
… where x.startDate between ?1 and ?2 |
LessThan |
findByAgeLessThan |
… where x.age < ?1 |
LessThanEqual |
findByAgeLessThanEqual |
… where x.age <= ?1 |
GreaterThan |
findByAgeGreaterThan |
… where x.age > ?1 |
GreaterThanEqual |
findByAgeGreaterThanEqual |
… where x.age >= ?1 |
After |
findByStartDateAfter |
… where x.startDate > ?1 |
Before |
findByStartDateBefore |
… where x.startDate < ?1 |
IsNull |
findByAgeIsNull |
… where x.age is null |
IsNotNull,NotNull |
findByAge(Is)NotNull |
… where x.age not null |
Like |
findByFirstnameLike |
… where x.firstname like ?1 |
NotLike |
findByFirstnameNotLike |
… where x.firstname not like ?1 |
StartingWith |
findByFirstnameStartingWith |
… where x.firstname like ?1 (parameter bound with appended % ) |
EndingWith |
findByFirstnameEndingWith |
… where x.firstname like ?1 (parameter bound with prepended % ) |
Containing |
findByFirstnameContaining |
… where x.firstname like ?1 (parameter bound wrapped in % ) |
OrderBy |
findByAgeOrderByLastnameDesc |
… where x.age = ?1 order by x.lastname desc |
Not |
findByLastnameNot |
… where x.lastname <> ?1 |
In |
findByAgeIn(Collection<Age> ages) |
… where x.age in ?1 |
NotIn |
findByAgeNotIn(Collection<Age> ages) |
… where x.age not in ?1 |
True |
findByActiveTrue() |
… where x.active = true |
False |
findByActiveFalse() |
… where x.active = false |
IgnoreCase |
findByFirstnameIgnoreCase |
… where UPPER(x.firstame) = UPPER(?1) |