#377 - Added example for Spring Data R2DBC.

This commit is contained in:
Oliver Gierke
2018-06-22 11:12:33 +02:00
parent 0e0de619fa
commit b13fad2649
7 changed files with 277 additions and 0 deletions

View File

@@ -19,6 +19,7 @@
<modules>
<module>basics</module>
<module>mybatis</module>
<module>r2dbc</module>
</modules>
<properties>

13
jdbc/r2dbc/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.

39
jdbc/r2dbc/pom.xml Normal file
View File

@@ -0,0 +1,39 @@
<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-jdbc-examples</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-data-r2dbc</artifactId>
<properties>
<reactor-bom.version>Californium-BUILD-SNAPSHOT</reactor-bom.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
<version>1.0.0.r2dbc-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-postgresql</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,29 @@
/*
* 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.Value;
import org.springframework.data.annotation.Id;
/**
* @author Oliver Gierke
*/
@Value
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.jdbc.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,69 @@
/*
* 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.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.r2dbc.function.DatabaseClient;
import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory;
/**
* @author Oliver Gierke
*/
@Configuration
class InfrastructureConfiguration {
@Bean
CustomerRepository customerRepository(R2dbcRepositoryFactory factory) {
return factory.getRepository(CustomerRepository.class);
}
@Bean
R2dbcRepositoryFactory repositoryFactory(DatabaseClient client) {
JdbcMappingContext context = new JdbcMappingContext();
context.afterPropertiesSet();
return new R2dbcRepositoryFactory(client, context);
}
@Bean
DatabaseClient databaseClient(ConnectionFactory factory) {
return DatabaseClient.builder() //
.connectionFactory(factory) //
.build();
}
@Bean
PostgresqlConnectionFactory connectionFactory() {
PostgresqlConnectionConfiguration config = PostgresqlConnectionConfiguration.builder() //
.host("localhost") //
.port(5432) //
.database("postgres") //
.username("postgres") //
.password("") //
.build();
return new PostgresqlConnectionFactory(config);
}
}