#421 - Elevate R2DBC example to top level project.

Moved r2dbc example to a top-level project. Switched to new Spring Data R2DBC project coordinates.

Original pull request: #422.
This commit is contained in:
Oliver Gierke
2018-10-17 16:57:54 +02:00
committed by Mark Paluch
parent 28dd3d8728
commit d2eb42dad1
10 changed files with 37 additions and 8 deletions

13
r2dbc/example/README.adoc Normal file
View File

@@ -0,0 +1,13 @@
== Spring Data R2DBC basics
This projects shows some sample usage of the work-in-progress R2DBC support for Spring Data.
=== Requirements
- A PostgreSQL database server running on port 5432, a database instance named `postgres` with a user `postgres` and an empty password.
=== Interesting bits to look at
- `InfrastructureConfiguration` - sets up a R2DBC `ConnectionFactory` based on the R2DBC Postgres driver (https://github.com/r2dbc/r2dbc-postgresql[r2dbc-postgresql]), a `DatabaseClient` and a `R2dbcRepositoryFactory` to eventually create a `CustomerRepository`.
- `CustomerRepository` - a standard Spring Data reactive CRUD repository exposing query methods using manually defined queries
- `CustomerRepositoryIntegrationTests` - to initialize the database with some setup SQL and the inserting and reading `Customer` instances.

17
r2dbc/example/pom.xml Normal file
View File

@@ -0,0 +1,17 @@
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-r2dbc-examples</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-data-r2dbc-example</artifactId>
<name>Spring Data R2DBC - Example</name>
</project>

View File

@@ -0,0 +1,32 @@
/*
* Copyright 2018 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
*
* http://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.r2dbc.basics;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Value;
import org.springframework.data.annotation.Id;
/**
* @author Oliver Gierke
*/
@Data
@AllArgsConstructor
class Customer {
@Id Integer id;
String firstname, lastname;
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2018 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
*
* http://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.r2dbc.basics;
import reactor.core.publisher.Flux;
import org.springframework.data.r2dbc.repository.query.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
/**
* @author Oliver Gierke
*/
interface CustomerRepository extends ReactiveCrudRepository<Customer, Long> {
@Query("select id, firstname, lastname from customer c where c.lastname = $1")
Flux<Customer> findByLastnameLike(String lastname);
}

View File

@@ -0,0 +1,96 @@
/*
* Copyright 2018 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
*
* http://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.r2dbc.basics;
import reactor.core.publisher.Hooks;
import reactor.test.StepVerifier;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
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.r2dbc.function.DatabaseClient;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author Oliver Gierke
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = InfrastructureConfiguration.class)
public class CustomerRepositoryIntegrationTests {
@Autowired CustomerRepository customers;
@Autowired DatabaseClient database;
@Before
public void setUp() {
Hooks.onOperatorDebug();
List<String> statements = Arrays.asList(//
"DROP TABLE IF EXISTS customer;",
"CREATE TABLE customer ( id SERIAL PRIMARY KEY, firstname VARCHAR(100) NOT NULL, lastname VARCHAR(100) NOT NULL);");
statements.forEach(it -> database.execute() //
.sql(it) //
.fetch() //
.rowsUpdated() //
.as(StepVerifier::create) //
.verifyComplete());
}
@Test
public void executesFindAll() throws IOException {
Customer dave = new Customer(null, "Dave", "Matthews");
Customer carter = new Customer(null, "Carter", "Beauford");
insertCustomers(dave, carter);
customers.findAll() //
.as(StepVerifier::create) //
.assertNext(dave::equals) //
.assertNext(carter::equals) //
.verifyComplete();
}
@Test
public void executesAnnotatedQuery() throws IOException {
Customer dave = new Customer(null, "Dave", "Matthews");
Customer carter = new Customer(null, "Carter", "Beauford");
insertCustomers(dave, carter);
customers.findByLastnameLike("Matthews") //
.as(StepVerifier::create) //
.assertNext(dave::equals) //
.verifyComplete();
}
private void insertCustomers(Customer... customers) {
this.customers.saveAll(Arrays.asList(customers))//
.as(StepVerifier::create) //
.expectNextCount(2) //
.verifyComplete();
}
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright 2018 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
*
* http://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.r2dbc.basics;
import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
import io.r2dbc.postgresql.PostgresqlConnectionFactory;
import io.r2dbc.spi.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.r2dbc.function.DatabaseClient;
import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.testcontainers.containers.PostgreSQLContainer;
import javax.annotation.PreDestroy;
/**
* @author Oliver Gierke
*/
@Configuration
class InfrastructureConfiguration {
private PostgreSQLContainer postgres = new PostgreSQLContainer();
@Bean
CustomerRepository customerRepository(R2dbcRepositoryFactory factory) {
return factory.getRepository(CustomerRepository.class);
}
@Bean
R2dbcRepositoryFactory repositoryFactory(DatabaseClient client) {
RelationalMappingContext context = new RelationalMappingContext();
context.afterPropertiesSet();
return new R2dbcRepositoryFactory(client, context);
}
@Bean
DatabaseClient databaseClient(ConnectionFactory factory) {
return DatabaseClient.builder() //
.connectionFactory(factory) //
.build();
}
@Bean
PostgresqlConnectionFactory connectionFactory() {
postgres.start();
PostgresqlConnectionConfiguration config = PostgresqlConnectionConfiguration.builder() //
.host(postgres.getContainerIpAddress()) //
.port(postgres.getFirstMappedPort()) //
.database(postgres.getDatabaseName()) //
.username(postgres.getUsername()) //
.password(postgres.getPassword()) //
.build();
return new PostgresqlConnectionFactory(config);
}
@PreDestroy
void shutdown() {
postgres.stop();
}
}