儲存庫查詢關鍵字

Supported query method subject keywords

The following table lists the subject keywords generally supported by the Spring Data repository query derivation mechanism to express the predicate. Consult the store-specific documentation for the exact list of supported keywords, because some keywords listed here might not be supported in a particular store.

表 1. 查詢主詞關鍵字
關鍵字 描述

find…By, read…By, get…By, query…By, search…By, stream…By

一般查詢方法,通常傳回儲存庫類型、CollectionStreamable 子類型,或結果包裝器,例如 PageGeoResults 或任何其他儲存區特定的結果包裝器。 可以用作 findBy…findMyDomainTypeBy… 或與其他關鍵字組合使用。

exists…By

Exists 投影,通常傳回 boolean 結果。

count…By

Count 投影,傳回數字結果。

delete…By, remove…By

Delete 查詢方法,不傳回任何結果 (void) 或刪除計數。

…First<number>…, …Top<number>…

將查詢結果限制為前 <number> 個結果。 此關鍵字可以出現在主詞中 find (和其他關鍵字) 和 by 之間的任何位置。

…Distinct…

使用 distinct 查詢僅傳回唯一結果。 請查閱特定儲存區的文件,以了解是否支援該功能。 此關鍵字可以出現在主詞中 find (和其他關鍵字) 和 by 之間的任何位置。

Reserved methods

下表列出使用預定義功能(如 CrudRepository 中所定義)的保留方法。 這些方法直接在儲存庫代理的後端(特定於儲存區)實作上調用。 另請參閱「定義查詢方法」。

表 2. 保留方法

deleteAllById(Iterable<ID> identifiers)

deleteById(ID identifier)

existsById(ID identifier)

findAllById(Iterable<ID> identifiers)

findById(ID identifier)

Supported query method predicate keywords and modifiers

下表列出了 Spring Data 儲存庫查詢衍生機制通常支援的謂詞關鍵字。 但是,請查閱特定儲存區的文件以獲取支援關鍵字的確切列表,因為此處列出的一些關鍵字可能在特定儲存區中不受支援。

表 3. 查詢謂詞關鍵字
邏輯關鍵字 關鍵字表達式

AND

And

OR

Or

AFTER

After, IsAfter

BEFORE

Before, IsBefore

CONTAINING

Containing, IsContaining, Contains

BETWEEN

Between, IsBetween

ENDING_WITH

EndingWith, IsEndingWith, EndsWith

EXISTS

Exists

FALSE

False, IsFalse

GREATER_THAN

GreaterThan, IsGreaterThan

GREATER_THAN_EQUALS

GreaterThanEqual, IsGreaterThanEqual

IN

In, IsIn

IS

Is, Equals, (或沒有關鍵字)

IS_EMPTY

IsEmpty, Empty

IS_NOT_EMPTY

IsNotEmpty, NotEmpty

IS_NOT_NULL

NotNull, IsNotNull

IS_NULL

Null, IsNull

LESS_THAN

LessThan, IsLessThan

LESS_THAN_EQUAL

LessThanEqual, IsLessThanEqual

LIKE

Like, IsLike

NEAR

Near, IsNear

NOT

Not, IsNot

NOT_IN

NotIn, IsNotIn

NOT_LIKE

NotLike, IsNotLike

REGEX

Regex, MatchesRegex, Matches

STARTING_WITH

StartingWith, IsStartingWith, StartsWith

TRUE

True, IsTrue

WITHIN

Within, IsWithin

In addition to filter predicates, the following list of modifiers is supported

表 4. 查詢謂詞修飾詞關鍵字
關鍵字 描述

IgnoreCase, IgnoringCase

與謂詞關鍵字一起使用,用於不區分大小寫的比較。

AllIgnoreCase, AllIgnoringCase

忽略所有適用屬性的大小寫。 用於查詢方法謂詞中的某處。

OrderBy…

指定靜態排序順序,後跟屬性路徑和方向(例如 OrderByFirstnameAscLastnameDesc)。