From 7eab65b53f3f50de108c98e88a8c4871681d3ca1 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 4 Aug 2015 15:46:27 +0200 Subject: [PATCH] #124 - Added Querydsl integration to Starbucks example. Enabled Querydsl query metadata creation to the Starbucks example and added binding customizations to UserRepository. --- README.md | 1 + rest/starbucks/pom.xml | 25 +++++++++++++++++++ .../rest/stores/StoreRepository.java | 20 ++++++++++++++- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index afbaf6e2..a341eebc 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ We have separate folders for the samples of individual modules: ## Spring Data web support * `web` - Example for Spring Data web integration (binding `Pageable` instances to Spring MVC controller methods, using interfaces to bind Spring MVCrequest payloads). +* `querydsl` - Example for Spring Data Querydsl web integration (creating a `Predicate` from web requests). ## Miscellaneous diff --git a/rest/starbucks/pom.xml b/rest/starbucks/pom.xml index 12c585c9..46e375ef 100644 --- a/rest/starbucks/pom.xml +++ b/rest/starbucks/pom.xml @@ -84,6 +84,31 @@ org.springframework.boot spring-boot-maven-plugin + + com.mysema.maven + apt-maven-plugin + ${apt.version} + + + com.mysema.querydsl + querydsl-apt + ${querydsl.version} + + + + + generate-sources + + process + + + target/generated-sources/annotations + org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor + true + + + + diff --git a/rest/starbucks/src/main/java/example/springdata/rest/stores/StoreRepository.java b/rest/starbucks/src/main/java/example/springdata/rest/stores/StoreRepository.java index 739f3360..3bd713b5 100644 --- a/rest/starbucks/src/main/java/example/springdata/rest/stores/StoreRepository.java +++ b/rest/starbucks/src/main/java/example/springdata/rest/stores/StoreRepository.java @@ -19,17 +19,35 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.geo.Distance; import org.springframework.data.geo.Point; +import org.springframework.data.querydsl.QueryDslPredicateExecutor; +import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer; +import org.springframework.data.querydsl.binding.QuerydslBindings; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.rest.core.annotation.RestResource; +import com.mysema.query.types.path.StringPath; + /** * Repository interface for out-of-the-box paginating access to {@link Store}s and a query method to find stores by * location and distance. * * @author Oliver Gierke */ -public interface StoreRepository extends PagingAndSortingRepository { +public interface StoreRepository extends PagingAndSortingRepository, QueryDslPredicateExecutor, + QuerydslBinderCustomizer { @RestResource(rel = "by-location") Page findByAddressLocationNear(Point location, Distance distance, Pageable pageable); + + /** + * Tweak the Querydsl binding if collection resources are filtered. + * + * @see org.springframework.data.web.querydsl.QuerydslBinderCustomizer#customize(org.springframework.data.web.querydsl.QuerydslBindings, + * com.mysema.query.types.EntityPath) + */ + default void customize(QuerydslBindings bindings, QStore store) { + + bindings.bind(store.address.city).first((path, value) -> path.endsWith(value)); + bindings.bind(String.class).first((StringPath path, String value) -> path.contains(value)); + } }