Skip to content

Commit 27e2aa2

Browse files
committed
Documentation for Query By Example.
Original pull request #1195 See #1192
1 parent 86f0140 commit 27e2aa2

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

src/main/asciidoc/jdbc.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,9 @@ You can specify the following return types:
700700
* `int` (updated record count)
701701
* `boolean`(whether a record was updated)
702702

703+
include::{spring-data-commons-docs}/query-by-example.adoc[leveloffset=+1]
704+
include::query-by-example.adoc[leveloffset=+1]
705+
703706
include::{spring-data-commons-docs}/repository-projections.adoc[leveloffset=+2]
704707

705708
[[jdbc.mybatis]]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
[[query-by-example.running]]
2+
== Running an Example
3+
4+
In Spring Data JDBC, you can use Query by Example with Repositories, as shown in the following example:
5+
6+
.Query by Example using a Repository
7+
====
8+
[source, java]
9+
----
10+
public interface PersonRepository
11+
extends CrudRepository<Person, String>,
12+
QueryByExampleExecutor<Person> { … }
13+
14+
public class PersonService {
15+
16+
@Autowired PersonRepository personRepository;
17+
18+
public List<Person> findPeople(Person probe) {
19+
return personRepository.findAll(Example.of(probe));
20+
}
21+
}
22+
----
23+
====
24+
25+
NOTE: Currently, only `SingularAttribute` properties can be used for property matching.
26+
27+
The property specifier accepts property names (such as `firstname` and `lastname`). You can navigate by chaining properties together with dots (`address.city`). You can also tune it with matching options and case sensitivity.
28+
29+
The following table shows the various `StringMatcher` options that you can use and the result of using them on a field named `firstname`:
30+
31+
[cols="1,2", options="header"]
32+
.`StringMatcher` options
33+
|===
34+
| Matching
35+
| Logical result
36+
37+
| `DEFAULT` (case-sensitive)
38+
| `firstname = ?0`
39+
40+
| `DEFAULT` (case-insensitive)
41+
| `LOWER(firstname) = LOWER(?0)`
42+
43+
| `EXACT` (case-sensitive)
44+
| `firstname = ?0`
45+
46+
| `EXACT` (case-insensitive)
47+
| `LOWER(firstname) = LOWER(?0)`
48+
49+
| `STARTING` (case-sensitive)
50+
| `firstname like ?0 + '%'`
51+
52+
| `STARTING` (case-insensitive)
53+
| `LOWER(firstname) like LOWER(?0) + '%'`
54+
55+
| `ENDING` (case-sensitive)
56+
| `firstname like '%' + ?0`
57+
58+
| `ENDING` (case-insensitive)
59+
| `LOWER(firstname) like '%' + LOWER(?0)`
60+
61+
| `CONTAINING` (case-sensitive)
62+
| `firstname like '%' + ?0 + '%'`
63+
64+
| `CONTAINING` (case-insensitive)
65+
| `LOWER(firstname) like '%' + LOWER(?0) + '%'`
66+
67+
|===

0 commit comments

Comments
 (0)