diff --git a/jdbc/pom.xml b/jdbc/pom.xml index c4a5fa1f..47037038 100644 --- a/jdbc/pom.xml +++ b/jdbc/pom.xml @@ -19,6 +19,7 @@ basics mybatis + r2dbc diff --git a/jdbc/r2dbc/README.adoc b/jdbc/r2dbc/README.adoc new file mode 100644 index 00000000..d37bd9fb --- /dev/null +++ b/jdbc/r2dbc/README.adoc @@ -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. \ No newline at end of file diff --git a/jdbc/r2dbc/pom.xml b/jdbc/r2dbc/pom.xml new file mode 100644 index 00000000..ec853e83 --- /dev/null +++ b/jdbc/r2dbc/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + + + org.springframework.data.examples + spring-data-jdbc-examples + 2.0.0.BUILD-SNAPSHOT + + + spring-data-r2dbc + + + Californium-BUILD-SNAPSHOT + + + + + + org.springframework.data + spring-data-jdbc + 1.0.0.r2dbc-SNAPSHOT + + + + io.r2dbc + r2dbc-postgresql + 1.0.0.BUILD-SNAPSHOT + runtime + + + + io.projectreactor + reactor-test + test + + + + diff --git a/jdbc/r2dbc/src/main/java/example/springdata/r2dbc/basics/Customer.java b/jdbc/r2dbc/src/main/java/example/springdata/r2dbc/basics/Customer.java new file mode 100644 index 00000000..7f79b7b0 --- /dev/null +++ b/jdbc/r2dbc/src/main/java/example/springdata/r2dbc/basics/Customer.java @@ -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; +} diff --git a/jdbc/r2dbc/src/main/java/example/springdata/r2dbc/basics/CustomerRepository.java b/jdbc/r2dbc/src/main/java/example/springdata/r2dbc/basics/CustomerRepository.java new file mode 100644 index 00000000..0a920755 --- /dev/null +++ b/jdbc/r2dbc/src/main/java/example/springdata/r2dbc/basics/CustomerRepository.java @@ -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 { + + @Query("select id, firstname, lastname from customer c where c.lastname = $1") + Flux findByLastnameLike(String lastname); +} diff --git a/jdbc/r2dbc/src/test/java/example/springdata/r2dbc/basics/CustomerRepositoryIntegrationTests.java b/jdbc/r2dbc/src/test/java/example/springdata/r2dbc/basics/CustomerRepositoryIntegrationTests.java new file mode 100644 index 00000000..b8069291 --- /dev/null +++ b/jdbc/r2dbc/src/test/java/example/springdata/r2dbc/basics/CustomerRepositoryIntegrationTests.java @@ -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 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(); + } +} diff --git a/jdbc/r2dbc/src/test/java/example/springdata/r2dbc/basics/InfrastructureConfiguration.java b/jdbc/r2dbc/src/test/java/example/springdata/r2dbc/basics/InfrastructureConfiguration.java new file mode 100644 index 00000000..4c67cada --- /dev/null +++ b/jdbc/r2dbc/src/test/java/example/springdata/r2dbc/basics/InfrastructureConfiguration.java @@ -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); + } +}