# For Maven add: --build maven
$ mn create-app --lang java example --features data-r2dbc,flyway,mysql
Table of Contents
Micronaut R2DBC
Integration with Micronaut and R2DBC
Version: 6.0.0
1 Introduction
This module provides integration between Micronaut and R2DBC.
2 Release History
For this project, you can find a list of releases (with release notes) here:
3 Quick Start
The quickest way to get started is to create a new Micronaut application with Micronaut Launch and choose the data-r2dbc
, mysql
and flyway
features. This can also be done via the Micronaut 2.2 and above CLI:
Or via curl
:
curl
# For Maven add to the URL: &build=maven
$ curl https://launch.micronaut.io/demo.zip?lang=java&features=data-r2dbc,flyway,mysql -o demo.zip && unzip demo.zip -d demo && cd demo
The generated application will use MySQL since we passed the mysql
feature adding dependency on the R2DBC driver for MySQL:
Gradle
Maven
runtimeOnly("dev.miku:r2dbc-mysql")
And for flyway the JDBC driver:
Gradle
Maven
runtimeOnly("mysql:mysql-connector-java")
To create configurations for other drivers you can select the appropriate feature: oracle , postgres , sqlserver , h2 or mariadb .
|
Now define a SQL script that creates your initial schema in src/main/resources/db/migration
. For example:
V1__create-schema.sql
CREATE TABLE book(id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255), pages INT, author_id BIGINT NOT NULL);
CREATE TABLE author(id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255));
You can now configure your application to connect to the database using src/main/resources/application.yml
which contains the application configuration:
application.yml
flyway: (1)
datasources:
default:
enabled: true
datasources:
default: (2)
url: jdbc:mysql://localhost:3306/mydatabase
r2dbc:
datasources:
default: (3)
url: r2dbc:mysql:///mydatabase
1 | The Flyway configuration ensures the schema migration is applied. See Micronaut Flyway for more information. |
2 | The Flyway configuration needs a JDBC datasource configured, this setting configures one. See Micronaut JDBC for more information. |
3 | The property r2dbc.datasources.default.url is used to configure the default R2DBC ConnectionFactory |
The R2DBC ConnectionFactory object can be injected anywhere in your code with dependency injection.
|
Now define a @MappedEntity
that maps to the author
table defined in the schema:
Java
Groovy
Kotlin
package example;
import io.micronaut.data.annotation.*;
import io.micronaut.serde.annotation.Serdeable;
@Serdeable
@MappedEntity
public class Author {
@GeneratedValue
@Id
private Long id;
private final String name;
public Author(String name) {
this.name = name;
}
public String getName() {
return name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
And a repository interface to access the database that extends from ReactiveStreamsRepository
:
Java
Groovy
Kotlin
package example;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.data.model.query.builder.sql.Dialect;
import io.micronaut.data.r2dbc.annotation.R2dbcRepository;
import io.micronaut.data.repository.reactive.ReactiveStreamsCrudRepository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import jakarta.validation.constraints.NotNull;
@R2dbcRepository(dialect = Dialect.MYSQL) // (1)
public interface AuthorRepository extends ReactiveStreamsCrudRepository<Author, Long> {
@NonNull
@Override
Mono<Author> findById(@NonNull @NotNull Long aLong); // (2)
@NonNull
@Override
Flux<Author> findAll();
}
1 | The @R2dbcRepository annotation can be used to specify the datasource and dialect |
2 | You can override methods from the super interface to specialize the default Publisher return type with a concrete implementation |
You can now inject this interface into controllers and use it to perform R2DBC queries:
Java
Groovy
Kotlin
package example;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@Controller("/authors")
public class AuthorController {
private final AuthorRepository repository;
public AuthorController(AuthorRepository repository) {
this.repository = repository;
}
@Get
Flux<Author> all() { // (1)
return repository.findAll();
}
@Get("/id")
Mono<Author> get(Long id) { // (2)
return repository.findById(id);
}
}
1 | By returning a reactive type that emits many items you can stream data (either Flowable or Flux ) |
2 | By returning a reactive type that emits a single item you return the entire response (either Single or Mono ) |
4 Available Drivers
The following drivers are available as of this writing.
H2
R2DBC Driver:
Gradle
Maven
runtimeOnly("io.r2dbc:r2dbc-h2")
And for Flyway migrations the JDBC driver:
Gradle
Maven
runtimeOnly("com.h2database:h2")
MySQL
R2DBC Driver:
Gradle
Maven
runtimeOnly("dev.miku:r2dbc-mysql")
And for Flyway migrations the JDBC driver:
Gradle
Maven
runtimeOnly("mysql:mysql-connector-java")
MariaDB
R2DBC Driver:
Gradle
Maven
runtimeOnly("org.mariadb:r2dbc-mariadb")
And for Flyway migrations the JDBC driver:
Gradle
Maven
runtimeOnly("org.mariadb.jdbc:mariadb-java-client")
Postgresql
R2DBC Driver:
Gradle
Maven
runtimeOnly("org.postgresql:r2dbc-postgresql")
And for Flyway migrations the JDBC driver:
Gradle
Maven
runtimeOnly("org.postgresql:postgresql")
SQL Server
R2DBC Driver:
Gradle
Maven
runtimeOnly("io.r2dbc:r2dbc-mssql")
And for Flyway migrations the JDBC driver:
Gradle
Maven
runtimeOnly("com.microsoft.sqlserver:mssql-jdbc")
5 Micronaut Data R2DBC
Micronaut Data builds on this module and provides support for Reactive repositories. See the documentation on Micronaut Data R2DBC for more information and the Quick Start section for a demonstration on how to create an application.
6 Repository
You can find the source code of this project in this repository: