Micronuat R2DBC
1. 简介
本模块提供 Micronaut 和 R2DBC 之间的集成。
2. 发布历史
你可以在这里找到此项目的发布列表(含发布说明):
https://github.com/micronaut-projects/micronaut-r2dbc/releases
3. 快速入门
最快速的入门方法是使用 Micronaut Launch 创建一个新的 Micronaut 应用程序,并选择 data-r2dbc、mysql 和 flywayfeatures。这也可以通过 Micronaut 2.2 及以上版本的 CLI 完成:
使用 CLI 创建应用程序
# For Maven add: --build maven
$ mn create-app --lang java example --features data-r2dbc,flyway,mysql
或通过 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
生成的应用程序将使用 MySQL,因为我们传递了 mysql 功能,增加了对 MySQL 的 R2DBC 驱动的依赖:
- Gradle
- Maven
runtimeOnly("dev.miku:r2dbc-mysql")
<dependency>
<groupId>dev.miku</groupId>
<artifactId>r2dbc-mysql</artifactId>
<scope>runtime</scope>
</dependency>
而对于 flyway 来说,则是 JDBC 驱动:
- Gradle
- Maven
runtimeOnly("mysql:mysql-connector-java")
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
要为其他驱动程序创建配置,可以选择相应的功能:Oracle
、Postgres
、SQLServer
、H2
或 Mariadb
。
现在定义一个 SQL 脚本,在 src/main/resources/db/migration
中创建初始模式。例如:
示例 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));
现在,你可以使用包含应用程序配置的 src/main/resources/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
- Flyway 配置可确保应用模式迁移。更多信息参阅 Micronaut Flyway。
- Flyway 配置需要配置 JDBC 数据源,此设置会配置一个。更多信息参阅 Micronaut JDBC。
r2dbc.datasources.default.url
属性用于配置默认的 R2DBCConnectionFactory
R2DBC ConnectionFactory 对象可以通过依赖注入注入到代码中的任何地方。
现在定义一个 @MappedEntity
,映射到模式中定义的 author
表:
- 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;
}
}
package example
import io.micronaut.data.annotation.*
import io.micronaut.serde.annotation.Serdeable
@Serdeable
@MappedEntity
class Author {
@GeneratedValue
@Id
Long id
final String name
Author(String name) {
this.name = name
}
}
package example
import io.micronaut.data.annotation.GeneratedValue
import io.micronaut.data.annotation.Id
import io.micronaut.data.annotation.MappedEntity
import io.micronaut.serde.annotation.Serdeable
@Serdeable
@MappedEntity
data class Author(val name: String) {
@GeneratedValue
@Id
var id: Long? = null
}
还有一个从 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();
}
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)
interface AuthorRepository extends ReactiveStreamsCrudRepository<Author, Long> {
@NonNull
@Override
Mono<Author> findById(@NonNull @NotNull Long aLong) // (2)
@NonNull
@Override
Flux<Author> findAll()
}
package example
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)
interface AuthorRepository : ReactiveStreamsCrudRepository<Author, Long> {
override fun findById(id: @NotNull Long): Mono<Author> // (2)
override fun findAll(): Flux<Author>
}
- @R2dbcRepository 注解可用于指定数据源和方言
- 你可以覆盖超级接口中的方法,用具体实现来专门化默认 Publisher 返回类型
现在,你可以将此接口注入控制器,并用它来执行 R2DBC 查询:
- 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);
}
}
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")
class AuthorController {
private final AuthorRepository repository
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)
}
}
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")
class AuthorController(private val repository: AuthorRepository) {
@Get
fun all(): Flux<Author> { // (1)
return repository.findAll()
}
@Get("/id")
fun get(id: Long): Mono<Author> { // (2)
return repository.findById(id)
}
}
4 可用驱动
截至本文撰写时,以下驱动可用。
H2
R2DBC 驱动:
- Gradle
- Maven
runtimeOnly("io.r2dbc:r2dbc-h2")
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-h2</artifactId>
<scope>runtime</scope>
</dependency>
对于 Flyway 迁移,则使用 JDBC 驱动:
- Gradle
- Maven
runtimeOnly("com.h2database:h2")
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
MySQL
R2DBC 驱动:
- Gradle
- Maven
runtimeOnly("dev.miku:r2dbc-mysql")
<dependency>
<groupId>dev.miku</groupId>
<artifactId>r2dbc-mysql</artifactId>
<scope>runtime</scope>
</dependency>
对于 Flyway 迁移,则使用 JDBC 驱动:
- Gradle
- Maven
runtimeOnly("mysql:mysql-connector-java")
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
MariaDB
R2DBC 驱动:
- Gradle
- Maven
runtimeOnly("org.mariadb:r2dbc-mariadb:1.0.0")
<dependency>
<groupId>org.mariadb</groupId>
<artifactId>r2dbc-mariadb</artifactId>
<version>1.0.0</version>
<scope>runtime</scope>
</dependency>
对于 Flyway 迁移,则使用 JDBC 驱动:
- Gradle
- Maven
runtimeOnly("org.mariadb.jdbc:mariadb-java-client")
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<scope>runtime</scope>
</dependency>
Postgresql
R2DBC 驱动:
- Gradle
- Maven
runtimeOnly("org.postgresql:r2dbc-postgresql")
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>r2dbc-postgresql</artifactId>
<scope>runtime</scope>
</dependency>
对于 Flyway 迁移,则使用 JDBC 驱动:
- Gradle
- Maven
runtimeOnly("org.postgresql:postgresql")
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
SQL Server
R2DBC 驱动:
- Gradle
- Maven
runtimeOnly("io.r2dbc:r2dbc-mssql")
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-mssql</artifactId>
<scope>runtime</scope>
</dependency>
对于 Flyway 迁移,则使用 JDBC 驱动:
- Gradle
- Maven
runtimeOnly("com.microsoft.sqlserver:mssql-jdbc")
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
5. Micronaut Data R2DBC
Micronaut Data 以该模块为基础,为响应式仓库提供支持。更多信息参阅 Micronaut Data R2DBC 文档,快速入门部分将演示如何创建应用程序。
6. 仓库
你可以在此仓库中找到此项目的源代码:
https://github.com/micronaut-projects/micronaut-r2dbc