|
| 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