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}.
*/