R2DBC contains a wide range of features:
-
Spring configuration support with Java-based
@Configuration
classes for an R2DBC driver instance. -
A
DatabaseClient
helper class that increases productivity when performing common R2DBC operations with integrated object mapping between rows and POJOs. -
Exception translation into Spring’s portable Data Access Exception hierarchy.
-
Feature-rich object mapping integrated with Spring’s Conversion Service.
-
Annotation-based mapping metadata that is extensible to support other metadata formats.
-
Automatic implementation of Repository interfaces, including support for custom query methods.
For most tasks, you should use DatabaseClient
or the repository support, which both use the rich mapping functionality.
DatabaseClient
is the place to look for accessing functionality such as ad-hoc CRUD operations.
An easy way to set up a working environment is to create a Spring-based project through start.spring.io. To do so:
-
Add the following to the pom.xml files
dependencies
element:<dependencyManagement> <dependencies> <dependency> <groupId>io.r2dbc</groupId> <artifactId>r2dbc-bom</artifactId> <version>${r2dbc-releasetrain.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- other dependency elements omitted --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-r2dbc</artifactId> <version>{version}</version> </dependency> <!-- a R2DBC driver --> <dependency> <groupId>io.r2dbc</groupId> <artifactId>r2dbc-h2</artifactId> <version>{r2dbcVersion}</version> </dependency> </dependencies>
-
Change the version of Spring in the pom.xml to be
<spring.framework.version>{springVersion}</spring.framework.version>
-
Add the following location of the Spring Milestone repository for Maven to your
pom.xml
such that it is at the same level as your<dependencies/>
element:<repositories> <repository> <id>spring-milestone</id> <name>Spring Maven MILESTONE Repository</name> <url>https://repo.spring.io/libs-milestone</url> </repository> </repositories>
The repository is also browseable here.
You may also want to set the logging level to DEBUG
to see some additional information. To do so, edit the application.properties
file to have the following content:
logging.level.org.springframework.data.r2dbc=DEBUG
Then you can, for example, create a Person
class to persist, as follows:
package org.spring.r2dbc.example;
public class Person {
private String id;
private String name;
private int age;
public Person(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
Next, you need to create a table structure in your database, as follows:
CREATE TABLE person
(id VARCHAR(255) PRIMARY KEY,
name VARCHAR(255),
age INT);
You also need a main application to run, as follows:
package org.spring.r2dbc.example;
public class R2dbcApp {
private static final Log log = LogFactory.getLog(R2dbcApp.class);
public static void main(String[] args) throws Exception {
ConnectionFactory connectionFactory = ConnectionFactories.get("r2dbc:h2:mem:///test?options=DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
DatabaseClient client = DatabaseClient.create(connectionFactory);
client.execute("CREATE TABLE person" +
"(id VARCHAR(255) PRIMARY KEY," +
"name VARCHAR(255)," +
"age INT)")
.fetch()
.rowsUpdated()
.as(StepVerifier::create)
.expectNextCount(1)
.verifyComplete();
client.insert()
.into(Person.class)
.using(new Person("joe", "Joe", 34))
.then()
.as(StepVerifier::create)
.verifyComplete();
client.select()
.from(Person.class)
.fetch()
.first()
.doOnNext(it -> log.info(it))
.as(StepVerifier::create)
.expectNextCount(1)
.verifyComplete();
}
}
When you run the main program, the preceding examples produce output similar to the following:
2018-11-28 10:47:03,893 DEBUG ata.r2dbc.function.DefaultDatabaseClient: 310 - Executing SQL statement [CREATE TABLE person
(id VARCHAR(255) PRIMARY KEY,
name VARCHAR(255),
age INT)]
2018-11-28 10:47:04,074 DEBUG ata.r2dbc.function.DefaultDatabaseClient: 908 - Executing SQL statement [INSERT INTO person (id, name, age) VALUES($1, $2, $3)]
2018-11-28 10:47:04,092 DEBUG ata.r2dbc.function.DefaultDatabaseClient: 575 - Executing SQL statement [SELECT id, name, age FROM person]
2018-11-28 10:47:04,436 INFO org.spring.r2dbc.example.R2dbcApp: 43 - Person [id='joe', name='Joe', age=34]
Even in this simple example, there are few things to notice:
-
You can create an instance of the central helper class in Spring Data R2DBC (
DatabaseClient
) by using a standardio.r2dbc.spi.ConnectionFactory
object. -
The mapper works against standard POJO objects without the need for any additional metadata (though you can, optionally, provide that information — see here.).
-
Mapping conventions can use field access. Notice that the
Person
class has only getters. -
If the constructor argument names match the column names of the stored row, they are used to instantiate the object.
There is a GitHub repository with several examples that you can download and play around with to get a feel for how the library works.
One of the first tasks when using relational databases and Spring is to create a io.r2dbc.spi.ConnectionFactory
object by using the IoC container. Make sure to use a supported database and driver.
The following example shows an example of using Java-based bean metadata to register an instance of io.r2dbc.spi.ConnectionFactory
:
io.r2dbc.spi.ConnectionFactory
object using Java-based bean metadata@Configuration
public class ApplicationConfiguration extends AbstractR2dbcConfiguration {
@Override
@Bean
public ConnectionFactory connectionFactory() {
return …;
}
}
This approach lets you use the standard io.r2dbc.spi.ConnectionFactory
instance, with the container using Spring’s AbstractR2dbcConfiguration
. As compared to registering a ConnectionFactory
instance directly, the configuration support has the added advantage of also providing the container with an ExceptionTranslator
implementation that translates R2DBC exceptions to exceptions in Spring’s portable DataAccessException
hierarchy for data access classes annotated with the @Repository
annotation. This hierarchy and the use of @Repository
is described in {spring-framework-ref}/data-access.html[Spring’s DAO support features].
AbstractR2dbcConfiguration
also registers DatabaseClient
, which is required for database interaction and for Repository implementation.
Spring Data R2DBC supports drivers through R2DBC’s pluggable SPI mechanism.
You can use any driver that implements the R2DBC spec with Spring Data R2DBC.
Since Spring Data R2DBC reacts to specific features of each database, it requires a Dialect
implementation otherwise your application won’t start up.
Spring Data R2DBC ships with dialect impelemtations for the following drivers:
-
H2 (
io.r2dbc:r2dbc-h2
) -
MariaDB (
org.mariadb:r2dbc-mariadb
) -
Microsoft SQL Server (
io.r2dbc:r2dbc-mssql
) -
MySQL (
dev.miku:r2dbc-mysql
) -
jasync-sql MySQL (
com.github.jasync-sql:jasync-r2dbc-mysql
) -
Postgres (
io.r2dbc:r2dbc-postgresql
)
Spring Data R2DBC reacts to database specifics by inspecting the ConnectionFactory
and selects the appropriate database dialect accordingly.
You need to configure your own {spring-data-r2dbc-javadoc}/api/org/springframework/data/r2dbc/dialect/R2dbcDialect.html[R2dbcDialect
] if the driver you use is not yet known to Spring Data R2DBC.
Tip
|
Dialects are resolved by {spring-data-r2dbc-javadoc}/org/springframework/data/r2dbc/dialect/DialectResolver.html[DialectResolver ] from a ConnectionFactory , typically by inspecting ConnectionFactoryMetadata .
You can let Spring auto-discover your R2dbcDialect by registering a class that implements org.springframework.data.r2dbc.dialect.DialectResolver$R2dbcDialectProvider through META-INF/spring.factories .
DialectResolver discovers dialect provider implementations from the class path using Spring’s SpringFactoriesLoader .
|