Merge Java 8 samples into basic one.

This commit is contained in:
Mark Paluch
2021-04-28 10:57:59 +02:00
parent 20dfd398c8
commit b44dc3dea1
24 changed files with 44 additions and 286 deletions

View File

@@ -16,11 +16,13 @@
package example.springdata.mongodb.customer;
import java.util.List;
import java.util.stream.Stream;
import org.springframework.data.domain.Sort;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.CrudRepository;
/**
@@ -47,4 +49,7 @@ public interface CustomerRepository extends CrudRepository<Customer, String> {
* @return
*/
GeoResults<Customer> findByAddressLocationNear(Point point, Distance distance);
@Query("{}")
Stream<Customer> findAllByCustomQueryWithStream();
}

View File

@@ -19,6 +19,7 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.util.List;
import java.util.stream.Stream;
import org.junit.Before;
import org.junit.Test;
@@ -82,6 +83,17 @@ public class CustomerRepositoryIntegrationTest {
assertThat(result.get(1), is(oliver));
}
/**
* Test case to show the usage of Java {@link Stream}.
*/
@Test
public void findCustomersAsStream() {
try (Stream<Customer> result = repository.findAllByCustomQueryWithStream()) {
result.forEach(System.out::println);
}
}
/**
* Test case to show the usage of the geo-spatial APIs to lookup people within a given distance of a reference point.
*/

View File

@@ -1,21 +0,0 @@
# Spring Data MongoDB - Java 8 examples
This project contains samples of Java 8 specific features of Spring Data (MongoDB).
## Support for JDK 8's `Stream` for repository methods
Repository methods can use a Java 8 `Stream` as a return type which will cause the reading of the results and the to-object-conversion of the `DBObject` to happen while iterating over the stream.
```java
public interface PersonRepository extends CrudRepository<Person, String> {
@Override
List<Person> findAll();
//Custom Query method returning a Java 8 Stream
@Query("{}")
Stream<Person> findAllByCustomQueryWithStream();
}
```
The test cases in `PersonRepositoryIntegrationTest` oppose a plain `List` based query method with one that uses a `Stream` and shows how the former pulls all data into memory first and the iteration is done over the pre-populated list. The execution of the `Stream`-based method in contrast shows that the individual elements are read and converted while iterating the stream.

View File

@@ -1,14 +0,0 @@
<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>
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-mongodb-examples</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-data-mongodb-java8</artifactId>
<name>Spring Data MongoDB - Java 8 specific features</name>
</project>

View File

@@ -1,34 +0,0 @@
/*
* Copyright 2015-2021 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.people;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.core.mapping.event.LoggingEventListener;
/**
* Simple configuration that registers a {@link LoggingEventListener} to demonstrate mapping behaviour when Java 8
* Streams are used.
*
* @author Thomas Darimont
*/
@SpringBootApplication
class ApplicationConfiguration {
public @Bean LoggingEventListener mongoEventListener() {
return new LoggingEventListener();
}
}

View File

@@ -1,35 +0,0 @@
/*
* Copyright 2015-2021 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.people;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.data.annotation.Id;
/**
* An entity to represent a Person.
*
* @author Thomas Darimont
*/
@Data
@RequiredArgsConstructor
public class Person {
private @Id String id;
private final String firstname;
private final String lastname;
}

View File

@@ -1,35 +0,0 @@
/*
* Copyright 2015-2021 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.people;
import java.util.List;
import java.util.stream.Stream;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.CrudRepository;
/**
* Repository interface to manage {@link Person} instances.
*
* @author Thomas Darimont
*/
public interface PersonRepository extends CrudRepository<Person, String> {
List<Person> findAll();
@Query("{}")
Stream<Person> findAllByCustomQueryWithStream();
}

View File

@@ -1,73 +0,0 @@
/*
* Copyright 2015-2021 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.people;
import java.util.stream.Stream;
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.data.mongodb.core.mapping.event.LoggingEventListener;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Integration test for {@link PersonRepository}.
*
* @author Thomas Darimont
* @author Oliver Gierke
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonRepositoryIntegrationTest {
@Autowired PersonRepository repository;
@Autowired MongoOperations operations;
Person dave, oliver, carter;
@Before
public void setUp() {
repository.deleteAll();
dave = repository.save(new Person("Dave", "Matthews"));
oliver = repository.save(new Person("Oliver August", "Matthews"));
carter = repository.save(new Person("Carter", "Beauford"));
}
/**
* Note that the all object conversions are preformed before the results are printed to the console.
*/
@Test
public void shouldPerformConversionBeforeResultProcessing() {
repository.findAll().forEach(System.out::println);
}
/**
* Note that the object conversions are preformed during stream processing as one can see from the
* {@link LoggingEventListener} output that is printed to the console.
*/
@Test
public void shouldPerformConversionDuringJava8StreamProcessing() {
try (Stream<Person> result = repository.findAllByCustomQueryWithStream()) {
result.forEach(System.out::println);
}
}
}

View File

@@ -1,5 +0,0 @@
/**
* Package showing usage of Spring Data MongoDB Repositories with Java 8.
*/
package example.springdata.mongodb.people;

View File

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

View File

@@ -23,7 +23,6 @@
<module>fluent-api</module>
<module>geo-json</module>
<module>gridfs</module>
<module>java8</module>
<module>jmolecules</module>
<module>kotlin</module>
<module>query-by-example</module>