#519 - Add example for reactive Querydsl for MongoDB.

closes: #519
Original pull request: #520.
This commit is contained in:
Christoph Strobl
2019-08-09 12:29:16 +02:00
committed by Mark Paluch
parent 31eeeaf0f0
commit 96daedb93f
12 changed files with 439 additions and 0 deletions

View File

@@ -29,6 +29,7 @@ We have separate folders for the samples of individual modules:
* `java8` - Example of how to use Spring Data MongoDB with Java 8 date time types as well as the usage of `Optional` as return type for repository methods. Note, this project requires to be build with JDK 8.
* `kotlin` - Example for using Cassandra with MongoDB.
* `query-by-example` - Example project showing usage of Query by Example with MongoDB.
* `querydsl` - Example project showing sync/reactive [Querydsl](https://github.com/querydsl/querydsl) support for MongoDB.
* `reactive` - Example project to show reactive template and repository support.
* `security` - Example project showing usage of Spring Security with MongoDB.
* `text-search` - Example project showing usage of MongoDB text search feature.

View File

@@ -31,6 +31,7 @@
<module>text-search</module>
<module>transactions</module>
<module>schema-validation</module>
<module>querydsl</module>
<module>util</module>
</modules>

View File

@@ -0,0 +1,34 @@
# Spring Data MongoDB - Querydsl example
This project contains samples of [Querydsl](https://github.com/querydsl/querydsl) usage in Spring Data MongoDB.
Querydsl is a framework which enables the construction of fluent, type-safe queries for multiple backends including MongoDB.
Spring Data integrates with Querydsl via `QuerydslPredicateExecutor` and its reactive counterpart `ReactiveQuerydslPredicateExecutor`.
**NOTE**: You may have to run `mvn compile` to generate the required `Q` classes first.
## Sync
```java
interface SyncCustomerRepository
extends CrudRepository<Customer, String>, QuerydslPredicateExecutor<Customer> { }
@Autowired SyncCustomerRepository repository;
// ...
List<Customer> result = repository.findAll(QCustomer.customer.lastname.eq("Matthews"));
```
## Reactive
```java
interface ReactiveCustomerRepository
extends ReactiveCrudRepository<Customer, String>, ReactiveQuerydslPredicateExecutor<Customer> { }
@Autowired ReactiveCustomerRepository repository;
// ...
Flux<Customer> result = repository.findAll(QCustomer.customer.lastname.eq("Matthews"));
```

59
mongodb/querydsl/pom.xml Normal file
View File

@@ -0,0 +1,59 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-data-mongodb-querydsl-example</artifactId>
<name>Spring Data MongoDB - Querydsl Example</name>
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-mongodb-examples</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>${apt.version}</version>
<dependencies>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/queries</outputDirectory>
<processor>org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor</processor>
<logOnlyOnError>true</logOnlyOnError>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,99 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.mongodb;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.util.Assert;
/**
* An entity to represent a customer.
*
* @author Christoph Strobl
*/
@Document
public class Customer {
private String id, firstname, lastname;
/**
* Creates a new {@link Customer} with the given firstname and lastname.
*
* @param firstname must not be {@literal null} or empty.
* @param lastname must not be {@literal null} or empty.
*/
public Customer(String firstname, String lastname) {
Assert.hasText(firstname, "Firstname must not be null or empty!");
Assert.hasText(lastname, "Lastname must not be null or empty!");
this.firstname = firstname;
this.lastname = lastname;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
@Override
public String toString() {
return "Customer{" + "id='" + id + '\'' + ", firstname='" + firstname + '\'' + ", lastname='" + lastname + '\''
+ '}';
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Customer customer = (Customer) o;
if (id != null ? !id.equals(customer.id) : customer.id != null)
return false;
if (firstname != null ? !firstname.equals(customer.firstname) : customer.firstname != null)
return false;
return lastname != null ? lastname.equals(customer.lastname) : customer.lastname == null;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (firstname != null ? firstname.hashCode() : 0);
result = 31 * result + (lastname != null ? lastname.hashCode() : 0);
return result;
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.mongodb.reactive;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Configuration to connect to MongoDB using a {@link com.mongodb.reactivestreams.client.MongoClient}. <br />
* Enables Spring Data repositories for MongoDB.
*
* @author Christoph Strobl
*/
@SpringBootApplication
class ApplicationConfiguration {}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.mongodb.reactive;
import example.springdata.mongodb.Customer;
import org.springframework.data.querydsl.ReactiveQuerydslPredicateExecutor;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
/**
* Reactive Querydsl supporting repository interface to manage {@link Customer} instances.
*
* @author Christoph Strobl
*/
interface ReactiveCustomerQuerydslRepository
extends ReactiveCrudRepository<Customer, String>, ReactiveQuerydslPredicateExecutor<Customer> {
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.mongodb.sync;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.mongodb.MongoClient;
/**
* Configuration to connect to MongoDB using a {@link MongoClient}. <br />
* Enables Spring Data repositories for MongoDB.
*
* @author Christoph Strobl
*/
@SpringBootApplication
class ApplicationConfiguration {}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.mongodb.sync;
import example.springdata.mongodb.Customer;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.CrudRepository;
/**
* Sync Querydsl supporting repository interface to manage {@link Customer} instances.
*
* @author Christoph Strobl
*/
interface CustomerQuerydslRepository extends CrudRepository<Customer, String>, QuerydslPredicateExecutor<Customer> {
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.mongodb.reactive;
import static org.assertj.core.api.Assertions.*;
import example.springdata.mongodb.Customer;
import example.springdata.mongodb.QCustomer;
import reactor.test.StepVerifier;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author Christoph Strobl
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ReactiveCustomerRepositoryTests {
@Autowired ReactiveCustomerQuerydslRepository repository;
@Autowired MongoOperations operations;
Customer dave, oliver, carter;
@Before
public void setUp() {
repository.deleteAll().as(StepVerifier::create).verifyComplete();
dave = new Customer("Dave", "Matthews");
oliver = new Customer("Oliver August", "Matthews");
carter = new Customer("Carter", "Beauford");
repository.save(dave).then().as(StepVerifier::create).verifyComplete();
repository.save(oliver).then().as(StepVerifier::create).verifyComplete();
repository.save(carter).then().as(StepVerifier::create).verifyComplete();
}
@Test
public void findAllByPredicate() {
repository.findAll(QCustomer.customer.lastname.eq("Matthews")) //
.collectList() //
.as(StepVerifier::create) //
.assertNext(it -> assertThat(it).containsExactlyInAnyOrder(dave, oliver)) //
.verifyComplete();
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.mongodb.sync;
import static org.assertj.core.api.Assertions.*;
import example.springdata.mongodb.Customer;
import example.springdata.mongodb.QCustomer;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author Christoph Strobl
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomerRepositoryTests {
@Autowired CustomerQuerydslRepository repository;
@Autowired MongoOperations operations;
Customer dave, oliver, carter;
@Before
public void setUp() {
repository.deleteAll();
dave = repository.save(new Customer("Dave", "Matthews"));
oliver = repository.save(new Customer("Oliver August", "Matthews"));
carter = repository.save(new Customer("Carter", "Beauford"));
}
@Test
public void findAllByPredicate() {
assertThat(repository.findAll(QCustomer.customer.lastname.eq("Matthews"))).containsExactlyInAnyOrder(dave, oliver);
}
}

View File

@@ -0,0 +1,2 @@
# Random port for embedded MongoDB
spring.data.mongodb.port=0