Query Methods

你通常在仓库上触发的多数数据访问操作会导致针对 LDAP 目录运行查询。定义这样的查询是声明仓库界面上的一个方法的问题,如下例所示:

PersonRepository with query methods
interface PersonRepository extends PagingAndSortingRepository<Person, String> {

    List<Person> findByLastname(String lastname);                            1

    List<Person> findByLastnameFirstname(String lastname, String firstname); 2
}
1 该方法显示了对具有给定 lastname 的所有人的查询。该查询是通过解析方法名称针对可与 AndOr 连接的约束导出的。因此,方法名称导致 (&amp;(objectclass=person)(lastname=lastname)) 的查询表达式。
2 该方法显示了对具有给定 lastnamefirstname 的所有人的查询。该查询是通过解析方法名称导出的。因此,方法名称导致 (&amp;(objectclass=person)(lastname=lastname)(firstname=firstname)) 的查询表达式。

下表提供了使用查询方法时可用的关键字示例:

Table 1. Supported keywords for query methods
Keyword Sample Logical result

LessThanEqual

findByAgeLessThanEqual(int age)

(attribute⇐age)

GreaterThanEqual

findByAgeGreaterThanEqual(int age)

(attribute>=age)

IsNotNull, NotNull

findByFirstnameNotNull()

(firstname=*)

IsNull, Null

findByFirstnameNull()

(!(firstname=*))

Like

findByFirstnameLike(String name)

(firstname=name)

NotLike, IsNotLike

findByFirstnameNotLike(String name)

(!(firstname=name*))

StartingWith

findByStartingWith(String name)

(firstname=name*)

EndingWith

findByFirstnameLike(String name)

(firstname=*name)

Containing

findByFirstnameLike(String name)

(firstname=name)

(No keyword)

findByFirstname(String name)

(Firstname=name)

Not

findByFirstnameNot(String name)

(!(Firstname=name))