Get ahead
VMware offers training and certification to turbo-charge your progress.
Learn moreSpring Data R2DBC, part of the larger Spring Data family, makes it easy to implement R2DBC based repositories. R2DBC stands for Reactive Relational Database Connectivity, a specification to integrate SQL databases using reactive drivers. Spring Data R2DBC applies familiar Spring abstractions and repository support for R2DBC. It makes it easier to build Spring-powered applications that use relational data access technologies in a reactive application stack.
Spring Data R2DBC is co-hosted with Spring Data JDBC in the Spring Data Relational module providing infrastructure for direct SQL database access.
Spring Data R2DBC aims at being conceptually easy. In order to achieve this, it does NOT offer caching, lazy loading, write-behind, or many other features of ORM frameworks. This makes Spring Data R2DBC a simple, limited, opinionated object mapper.
Spring Data R2DBC allows a functional approach to interact with your database providing R2dbcEntityTemplate
as the entry point for applications.
Get started by picking a database driver and create a R2dbcEntityTemplate
instance:
H2 (io.r2dbc:r2dbc-h2
)
MariaDB (org.mariadb:r2dbc-mariadb
)
Microsoft SQL Server (io.r2dbc:r2dbc-mssql
)
MySQL (io.asyncer:r2dbc-mysql
)
jasync-sql MySQL (com.github.jasync-sql:jasync-r2dbc-mysql
)
Postgres (io.r2dbc:r2dbc-postgresql
)
Oracle (com.oracle.database.r2dbc:oracle-r2dbc
)
PostgreSQL Example
PostgresqlConnectionFactory connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
.host(…)
.database(…)
.username(…)
.password(…).build());
R2dbcEntityTemplate template = new R2dbcEntityTemplate(connectionFactory);
Mono<Integer> update = template.update(Person.class)
.inTable("person_table")
.matching(query(where("firstname").is("John")))
.apply(update("age", 42));
Flux<Person> all = template.select(Person.class)
.matching(query(where("firstname").is("John")
.and("lastname").in("Doe", "White"))
.sort(by(desc("id"))))
.all();
Repository Example
interface PersonRepository extends ReactiveCrudRepository<Person, String> {
Flux<Person> findByFirstname(String firstname);
@Modifying
@Query("UPDATE person SET firstname = :firstname where lastname = :lastname")
Mono<Integer> setFixedFirstnameFor(String firstname, String lastname);
@Query("SELECT * FROM person WHERE lastname = :#{[0]}")
Flux<Person> findByQueryWithExpression(String lastname);
}
The client API provides covers the following features:
Running statements for mapped entities using the Criteria API built on top of Spring Framework’s R2DBC DatabaseClient
.
Parameter binding using the native syntax.
Result consumption: Update count, unmapped (Map<String, Object>
), mapped to entities, extraction function.
Reactive repositories using @Query
annotated methods.