Skip to content

Commit dc50706

Browse files
mp911deodrotbohm
authored andcommitted
spring-projects#160 - Add excerpts for Query by Example.
1 parent f8bfe5c commit dc50706

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ We have separate folders for the samples of individual modules:
1414
* `interceptors` - Example of how to enrich the repositories with AOP.
1515
* `security` - Example of how to integrate Spring Data JPA Repositories with Spring Security.
1616
* `multiple-datasources` - Examples of how to use Spring Data JPA with multiple `DataSource`s.
17+
* `query-by-example` - Example project showing usage of Query by Example with MongoDB.
1718

1819
## Spring Data MongoDB
1920

2021
* `example` - Example project for general repository functionality (including geo-spatial functionality), Querydsl integration and advanced topics.
2122
* `aggregation` - Example project to showcase the MongoDB aggregation framework support.
2223
* `text-search` - Example project showing usage of MongoDB text search feature.
2324
* `geo-json` - Example project showing usage of [GeoJSON](http://geojson.org) with MongoDB.
25+
* `query-by-example` - Example project showing usage of Query by Example with MongoDB.
2426

2527
## Spring Data REST
2628

jpa/query-by-example/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,22 @@ Query by Example (QBE) is a user-friendly querying technique with a simple inter
88

99
An `Example` takes a data object (usually the entity object or a subtype of it) and a specification how to match properties. You can use Query by Example with JPA Repositories.
1010

11+
```java
12+
public interface PersonRepository extends CrudRepository<Person, String>, QueryByExampleExecutor<Person> {
13+
}
14+
```
15+
16+
```java
17+
Example<Person> example = Example.of(new Person("Jon", "Snow"));
18+
repo.findAll(example);
19+
20+
21+
ExampleMatcher matcher = ExampleMatcher.matching().
22+
.withMatcher("firstname", endsWith())
23+
.withMatcher("lastname", startsWith().ignoreCase());
24+
25+
Example<Person> example = Example.of(new Person("Jon", "Snow"), matcher);
26+
repo.count(example);
27+
```
28+
1129
This example contains a test class to illustrate Query-by-Example with a Repository in `UserRepositoryIntegrationTests`.

mongodb/query-by-example/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,23 @@ Query by Example (QBE) is a user-friendly querying technique with a simple inter
88

99
An `Example` takes a data object (usually the entity object or a subtype of it) and a specification how to match properties. You can use Query by Example with `MongoOperations` and Repositories.
1010

11+
```java
12+
public interface PersonRepository extends CrudRepository<Person, String>, QueryByExampleExecutor {
13+
}
14+
```
15+
16+
```java
17+
Example<Person> example = Example.of(new Person("Jon", "Snow"));
18+
repo.findAll(example);
19+
20+
21+
ExampleMatcher matcher = ExampleMatcher.matching().
22+
.withMatcher("firstname", endsWith())
23+
.withMatcher("lastname", startsWith().ignoreCase());
24+
25+
Example<Person> example = Example.of(new Person("Jon", "Snow"), matcher);
26+
repo.count(example);
27+
```
28+
1129
This example contains two test classes to illustrate Query-by-Example with `MongoOperations` in `MongoOperationsIntegrationTests` and the usage with a Repository in `UserRepositoryIntegrationTests`.
1230

0 commit comments

Comments
 (0)