Spring certified China Education Management Center – spring data r2dbc framework tutorial I


Original title: Spring certified China Education Management Center – spring data r2dbc framework tutorial I (spring China Education Management Center)

  1. R2dbc support
    R2dbc includes a wide range of functions:

Spring configuration supports Java based classes of @ configurationr2dbc driver instances.
R2dbcentitytemplate, as the central class of entity binding operations, improves productivity when performing common r2dbc operations with integrated object mapping between rows and POJOs.
The feature rich object mapping is integrated with spring’s transformation services.
Annotation based mapping metadata can be extended to support other metadata formats.
Automatic implementation of repository interface, including support for custom query methods.
For most tasks, you should use r2dbcentytemplate or repository support, both of which use rich mapping capabilities. R2dbcentitytemplate is a place to look for access functions, such as temporary CRUD operations.

13.1. introduction
An easy way to set up your work environment is through start spring. IO create a spring based project. Do it in this way.

Add the following to POM XML File 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>1.4.0</version> </dependency> <!– a R2DBC driver –> <dependency> <groupId>io.r2dbc</groupId> <artifactId>r2dbc-h2</artifactId> <version>Arabba-SR10</version> </dependency> </dependencies>

Spring certified China Education Management Center – spring data r2dbc framework tutorial I
Put POM The version of spring in XML is changed to < spring framework version>5.3.13</spring-framework. version>
Add the following location of Maven’s spring milestone repository to your location POM XML and make it 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-m…</url> </repository> </repositories>
The repository can also be browsed here.

You may also want to set the logging level to debug to see some additional information. To do this, edit application The properties file has the following contents:

Then you can, for example, create a person class to persist, as follows:

public class Person {

private final String id;
private final String name;
private final 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;


public String toString() {

return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";

Spring certified China Education Management Center – spring data r2dbc framework tutorial I
Next, you need to create a table structure in your database, as shown below:

name VARCHAR(255),
age INT);
You also need a main application to run, as follows:

import io.r2dbc.spi.ConnectionFactories;
import io.r2dbc.spi.ConnectionFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import reactor.test.StepVerifier;

import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;

public class R2dbcApp {

private static final Log log = LogFactory.getLog(R2dbcApp.class);

public static void main(String[] args) {

ConnectionFactory connectionFactory = ConnectionFactories.get("r2dbc:h2:mem:///test?options=DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");

R2dbcEntityTemplate template = new R2dbcEntityTemplate(connectionFactory);

template.getDatabaseClient().sql("CREATE TABLE person" +
    "(id VARCHAR(255) PRIMARY KEY," +
    "name VARCHAR(255)," +
    "age INT)")

  .using(new Person("joe", "Joe", 34))

  .doOnNext(it -> log.info(it))

Spring certified China Education Management Center – spring data r2dbc framework tutorial I
When running the main program, the previous example generates output similar to the following:

2018-11-28 10:47:03,893 DEBUG amework.core.r2dbc.DefaultDatabaseClient: 310 – Executing SQL statement [CREATE TABLE person
name VARCHAR(255),
age INT)]
2018-11-28 10:47:04,074 DEBUG amework.core.r2dbc.DefaultDatabaseClient: 908 – Executing SQL statement [INSERT INTO person (id, name, age) VALUES($1, $2, $3)]
2018-11-28 10:47:04,092 DEBUG amework.core.r2dbc.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 several things to note:

You can use the standard IO r2dbc. spi. The connectionfactory object creates an instance of the central helper class in spring data r2dbc().
The mapper works against standard POJO objects without any additional metadata (although you can choose to provide this information – see here).
Mapping conventions can be accessed using fields. Note that the person class has only getters.
If the constructor parameter names match the column names of the storage rows, they are used to instantiate the object.
13.2. Sample library
There is a GitHub repository with multiple samples that you can download and try to understand how the library works.

13.3. Using spring to connect to a relational database
One of the first tasks when working with relational databases and spring is
io. r2dbc. spi. Connectionfactory creates objects using IOC containers. Make sure to use supported databases and drivers.

13.3.1. Connectionfactory registers instances using java based metadata
The following example shows an example of an instance registered using java based bean metadata

54.io. r2dbc. spi. Connectionfactory registers objects using java based bean metadata

public class ApplicationConfiguration extends AbstractR2dbcConfiguration {

public ConnectionFactory connectionFactory() {

return …

This approach allows you to use standards
io. r2dbc. spi. Connectionfactory instance. The container uses spring’s abstractr2dbcconfiguration Compared with connectionfactory directly registering instances, configuration support has additional advantages, that is, it also provides an exceptiontranslator implementation for the container to convert r2dbc exceptions into classes annotated with @ repository annotation for exception access in spring’s portable dataaccessexception data hierarchy. Spring’s Dao support feature @ repository describes this hierarchy and usage.

Abstractr2dbcconfiguration also registers databaseclient, which is necessary for database interaction and repository implementation.

13.3.2. R2dbc driver
Spring data r2dbc supports drivers through r2dbc’s pluggable SPI mechanism. You can use any driver that implements the r2dbc specification through spring data r2dbc. Since spring data r2dbc will respond to the specific functions of each database, it needs a dialect implementation, otherwise your application will not start. Spring data r2dbc comes with dialect implementations of the following drivers:

H2 ( io.r2dbc:r2dbc-h2)
Maria database (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)
Oracle (COM. Oracle. Database. R2dbc: oracle-r2dbc)
Spring data r2dbc responds to database details by checking connectionfactory and selecting the appropriate database dialect. R2dbcdialect if spring data r2dbc does not know the driver you are using, you need to configure it yourself.

Dialects are parsed by dialectresolverfrom a connectionfactory, usually through checking
ConnectionFactoryMetadata. + You can sign up for an org. Org by r2dbcdialect springframework. data. r2dbc. dialect. Dialectresolver $r2dbcdialectprovider via meta-inf / spring factories. Dialectresolver uses spring factoryesloader