Skip to content

Commit 7eab65b

Browse files
committed
spring-projects#124 - Added Querydsl integration to Starbucks example.
Enabled Querydsl query metadata creation to the Starbucks example and added binding customizations to UserRepository.
1 parent bbaa53c commit 7eab65b

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ We have separate folders for the samples of individual modules:
4242
## Spring Data web support
4343

4444
* `web` - Example for Spring Data web integration (binding `Pageable` instances to Spring MVC controller methods, using interfaces to bind Spring MVCrequest payloads).
45+
* `querydsl` - Example for Spring Data Querydsl web integration (creating a `Predicate` from web requests).
4546

4647
## Miscellaneous
4748

rest/starbucks/pom.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,31 @@
8484
<groupId>org.springframework.boot</groupId>
8585
<artifactId>spring-boot-maven-plugin</artifactId>
8686
</plugin>
87+
<plugin>
88+
<groupId>com.mysema.maven</groupId>
89+
<artifactId>apt-maven-plugin</artifactId>
90+
<version>${apt.version}</version>
91+
<dependencies>
92+
<dependency>
93+
<groupId>com.mysema.querydsl</groupId>
94+
<artifactId>querydsl-apt</artifactId>
95+
<version>${querydsl.version}</version>
96+
</dependency>
97+
</dependencies>
98+
<executions>
99+
<execution>
100+
<phase>generate-sources</phase>
101+
<goals>
102+
<goal>process</goal>
103+
</goals>
104+
<configuration>
105+
<outputDirectory>target/generated-sources/annotations</outputDirectory>
106+
<processor>org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor</processor>
107+
<logOnlyOnError>true</logOnlyOnError>
108+
</configuration>
109+
</execution>
110+
</executions>
111+
</plugin>
87112
</plugins>
88113
</build>
89114

rest/starbucks/src/main/java/example/springdata/rest/stores/StoreRepository.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,35 @@
1919
import org.springframework.data.domain.Pageable;
2020
import org.springframework.data.geo.Distance;
2121
import org.springframework.data.geo.Point;
22+
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
23+
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
24+
import org.springframework.data.querydsl.binding.QuerydslBindings;
2225
import org.springframework.data.repository.PagingAndSortingRepository;
2326
import org.springframework.data.rest.core.annotation.RestResource;
2427

28+
import com.mysema.query.types.path.StringPath;
29+
2530
/**
2631
* Repository interface for out-of-the-box paginating access to {@link Store}s and a query method to find stores by
2732
* location and distance.
2833
*
2934
* @author Oliver Gierke
3035
*/
31-
public interface StoreRepository extends PagingAndSortingRepository<Store, String> {
36+
public interface StoreRepository extends PagingAndSortingRepository<Store, String>, QueryDslPredicateExecutor<Store>,
37+
QuerydslBinderCustomizer<QStore> {
3238

3339
@RestResource(rel = "by-location")
3440
Page<Store> findByAddressLocationNear(Point location, Distance distance, Pageable pageable);
41+
42+
/**
43+
* Tweak the Querydsl binding if collection resources are filtered.
44+
*
45+
* @see org.springframework.data.web.querydsl.QuerydslBinderCustomizer#customize(org.springframework.data.web.querydsl.QuerydslBindings,
46+
* com.mysema.query.types.EntityPath)
47+
*/
48+
default void customize(QuerydslBindings bindings, QStore store) {
49+
50+
bindings.bind(store.address.city).first((path, value) -> path.endsWith(value));
51+
bindings.bind(String.class).first((StringPath path, String value) -> path.contains(value));
52+
}
3553
}

0 commit comments

Comments
 (0)