Show how to use Limit with repository queries.

Add methods taking a Limit parameter to various samples.

Original pull request: #672
Closes #671
This commit is contained in:
Christoph Strobl
2023-11-02 08:49:24 +01:00
committed by Mark Paluch
parent e90be0c65b
commit f98c7b9c93
13 changed files with 167 additions and 0 deletions

View File

@@ -18,6 +18,7 @@ package example.springdata.mongodb.customer;
import java.util.List;
import java.util.stream.Stream;
import org.springframework.data.domain.Limit;
import org.springframework.data.domain.Sort;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
@@ -41,6 +42,15 @@ public interface CustomerRepository extends CrudRepository<Customer, String> {
*/
List<Customer> findByLastname(String lastname, Sort sort);
/**
* Derived query reducing result size to a given {@link Limit}.
*
* @param lastname
* @param maxResults the maximum number of results returned.
* @return
*/
List<Customer> findByLastname(String lastname, Limit maxResults);
/**
* Showcase for a repository query using geospatial functionality.
*

View File

@@ -27,6 +27,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.data.domain.Limit;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.geo.Point;
@@ -96,6 +97,17 @@ class CustomerRepositoryIntegrationTest {
assertThat(result.get(1)).isEqualTo(oliver);
}
/**
* Test case to show how to reduce result size with dynamic {@link Limit}.
*/
@Test
void limitResultSize() {
var result = repository.findByLastname("Matthews", Limit.of(1));
assertThat(result).hasSize(1);
}
/**
* Test case to show the usage of Java {@link Stream}.
*/

View File

@@ -15,6 +15,9 @@
*/
package example.springdata.mongodb.people;
import java.util.List;
import org.springframework.data.domain.Limit;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@@ -37,6 +40,17 @@ public interface ReactivePersonRepository extends ReactiveCrudRepository<Person,
*/
Flux<Person> findByLastname(String lastname);
/**
* Find at most the number of users defined via maxResults with the given lastname.
* This method will be translated into a query by constructing it directly from the method name as there is no other
* query declared.
*
* @param lastname
* @param maxResults the maximum number of results returned.
* @return
*/
Flux<Person> findByLastname(String lastname, Limit maxResults);
/**
* String query selecting one entity.
*

View File

@@ -18,6 +18,7 @@ package example.springdata.mongodb.people;
import static org.assertj.core.api.Assertions.*;
import example.springdata.mongodb.util.MongoContainers;
import org.springframework.data.domain.Limit;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@@ -157,6 +158,14 @@ class ReactivePersonRepositoryIntegrationTest {
repository.findByLastname("White").as(StepVerifier::create).expectNextCount(2).verifyComplete();
}
/**
* Limit result size.
*/
@Test
void shouldLimitResultSize() {
repository.findByLastname("White", Limit.of(1)).as(StepVerifier::create).expectNextCount(1).verifyComplete();
}
/**
* Fetch data using a string query.
*/