按示例查詢 (Query by Example)
簡介
本章介紹了按示例查詢(Query by Example)並解釋瞭如何使用它。
按示例查詢(Query by Example,簡稱 QBE)是一種使用者友好的查詢技術,具有簡單的介面。它允許動態建立查詢,並且無需您編寫包含欄位名稱的查詢。實際上,按示例查詢根本不需要您使用特定於儲存的查詢語言來編寫查詢。
本章解釋了按示例查詢的核心概念。這些資訊來自 Spring Data Commons 模組。根據您使用的資料庫,字串匹配支援可能有限。 |
用法
按示例查詢 API 由四部分組成
-
探測物件(Probe):填充了欄位的實際域物件示例。
-
ExampleMatcher
:ExampleMatcher
包含如何匹配特定欄位的詳細資訊。它可以在多個 Example 中重用。 -
Example
:一個Example
由探測物件(probe)和ExampleMatcher
組成。它用於建立查詢。 -
FetchableFluentQuery
:FetchableFluentQuery
提供了一個流暢的 API,允許進一步自定義從Example
派生的查詢。使用流暢 API 可以為您的查詢指定排序、投影和結果處理。
按示例查詢非常適合以下幾種用例
-
使用一組靜態或動態約束查詢您的資料儲存。
-
頻繁重構域物件,而無需擔心破壞現有查詢。
-
獨立於底層資料儲存 API 工作。
按示例查詢也有一些限制
-
不支援巢狀或分組的屬性約束,例如
firstname = ?0 or (firstname = ?1 and lastname = ?2)
。 -
特定於儲存的字串匹配支援。根據您的資料庫,字串匹配可能支援字串的 starts/contains/ends/regex。
-
對其他屬性型別進行精確匹配。
在使用按示例查詢之前,您需要有一個域物件。首先,為您的 Repository 建立一個介面,如下例所示
public class Person {
@Id
private String id;
private String firstname;
private String lastname;
private Address address;
// … getters and setters omitted
}
前面的示例展示了一個簡單的域物件。您可以使用它來建立一個 Example
。預設情況下,值為 null
的欄位會被忽略,字串會使用特定於儲存的預設設定進行匹配。
將屬性包含到按示例查詢標準中是基於 null 性的。使用基本型別(int , double 等)的屬性總是會被包含,除非 ExampleMatcher 忽略該屬性路徑。 |
Example 可以使用 of
工廠方法或透過使用 ExampleMatcher
構建。Example
是不可變的。以下列表顯示了一個簡單的 Example
Person person = new Person(); (1)
person.setFirstname("Dave"); (2)
Example<Person> example = Example.of(person); (3)
1 | 建立域物件的新例項。 |
2 | 設定要查詢的屬性。 |
3 | 建立 Example 。 |
您可以使用 Repository 執行示例查詢。為此,讓您的 Repository 介面擴充套件 QueryByExampleExecutor<T>
。以下列表顯示了 QueryByExampleExecutor
介面的摘錄
QueryByExampleExecutor
public interface QueryByExampleExecutor<T> {
<S extends T> S findOne(Example<S> example);
<S extends T> Iterable<S> findAll(Example<S> example);
// … more functionality omitted.
}
Example Matcher
Example 不限於預設設定。您可以使用 ExampleMatcher
指定自己的字串匹配、null 處理和屬性特定設定的預設值,如下例所示
Person person = new Person(); (1)
person.setFirstname("Dave"); (2)
ExampleMatcher matcher = ExampleMatcher.matching() (3)
.withIgnorePaths("lastname") (4)
.withIncludeNullValues() (5)
.withStringMatcher(StringMatcher.ENDING); (6)
Example<Person> example = Example.of(person, matcher); (7)
1 | 建立域物件的新例項。 |
2 | 設定屬性。 |
3 | 建立一個 ExampleMatcher ,期望所有值都匹配。在此階段,即使沒有進一步配置,它也是可用的。 |
4 | 構造一個新的 ExampleMatcher 以忽略 lastname 屬性路徑。 |
5 | 構造一個新的 ExampleMatcher 以忽略 lastname 屬性路徑幷包含 null 值。 |
6 | 構造一個新的 ExampleMatcher 以忽略 lastname 屬性路徑,包含 null 值,並執行字尾字串匹配。 |
7 | 基於域物件和配置的 ExampleMatcher 建立一個新的 Example 。 |
預設情況下,ExampleMatcher
期望探測物件上設定的所有值都匹配。如果您想獲取匹配隱式定義的任何謂詞的結果,請使用 ExampleMatcher.matchingAny()
。
您可以指定單個屬性(例如“firstname”和“lastname”,或對於巢狀屬性,例如“address.city”)的行為。您可以使用匹配選項和大小寫敏感性對其進行調整,如下例所示
ExampleMatcher matcher = ExampleMatcher.matching()
.withMatcher("firstname", endsWith())
.withMatcher("lastname", startsWith().ignoreCase());
}
配置 matcher 選項的另一種方法是使用 lambda 表示式(Java 8 中引入)。這種方法建立了一個回撥,要求實現者修改 matcher。您無需返回 matcher,因為配置選項儲存在 matcher 例項中。以下示例展示了一個使用 lambda 表示式的 matcher
ExampleMatcher matcher = ExampleMatcher.matching()
.withMatcher("firstname", match -> match.endsWith())
.withMatcher("firstname", match -> match.startsWith());
}
由 Example
建立的查詢使用配置的合併檢視。預設匹配設定可以在 ExampleMatcher
級別設定,而單個設定可以應用於特定的屬性路徑。在 ExampleMatcher
上設定的設定會被屬性路徑設定繼承,除非它們被顯式定義。屬性路徑上的設定優先順序高於預設設定。下表描述了各種 ExampleMatcher
設定的作用域
設定 | 作用域 |
---|---|
Null 處理 |
|
字串匹配 |
|
忽略屬性 |
屬性路徑 |
大小寫敏感性 |
|
值轉換 |
屬性路徑 |
流暢 API
QueryByExampleExecutor
還提供了一個我們迄今為止尚未提及的方法:<S extends T, R> R findBy(Example<S> example, Function<FluentQuery.FetchableFluentQuery<S>, R> queryFunction)
。與其他方法一樣,它執行從 Example
派生的查詢。然而,透過第二個引數,您可以控制那些無法動態控制的執行方面。您可以透過呼叫第二個引數中 FetchableFluentQuery
的各種方法來實現這一點。sortBy
讓您指定結果的排序。as
讓您指定希望結果轉換成的型別。project
限制查詢的屬性。first
、firstValue
、one
、oneValue
、all
、page
、stream
、count
和 exists
定義了您獲得的結果型別以及當結果數量超出預期時查詢的行為。
Optional<Person> match = repository.findBy(example,
q -> q
.sortBy(Sort.by("lastname").descending())
.first()
);