Spring Data R2DBC
3.1.1Spring Data R2DBC 是 Spring Data 系列的一部分,它可以轻松实现基于 R2DBC 的存储库。 R2DBC 代表响应式 Reactive Relational Database Connectivity,是一种使用响应式驱动程序集成 SQL 数据库的规范。 Spring Data R2DBC 应用了我们熟悉的 Spring 抽象和对 R2DBC 的存储库支持。 它可以更轻松地构建在响应式应用程序技术栈中使用关系数据访问技术的 Spring 支持的应用程序。
Spring Data R2DBC 的目标是在概念上变得简单。 为了实现这一点,它不提供缓存、延迟加载、后写或 ORM 框架的许多其他功能。 这使得 Spring Data R2DBC 成为一个简单、有限、固执己见的对象映射器。
Spring Data R2DBC 允许使用函数式方法与数据库交互,提供 R2dbcEntityTemplate 作为应用程序的入口点。
首先选择数据库驱动程序并创建 R2dbcEntityTemplate 实例:
-
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 示例
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 示例
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);
}
客户端 API 提供了包括以下功能:
-
使用基于 Spring Framework 的 R2DBC
DatabaseClient构建CriteriaAPI ,运行映射实体的语句。 -
使用本地语法进行参数绑定。
-
消耗结果:更新计数、未映射(
Map<String, Object>)、映射到实体、提取函数。 -
使用
@Query注解方法的响应式存储库。
| 分支 | 版本发布日期 | 社区支持结束日期 | 商业支持结束日期 * |
|---|---|---|---|
|
3.1.x
|
2023-05-12 | 2024-05-12 | 2025-09-12 |
|
3.0.x
|
2022-11-18 | 2023-11-24 | 2025-02-24 |
|
1.5.x
|
2022-05-13 | 2023-05-13 | 2024-09-13 |
|
1.4.x
|
2021-11-12 | 2022-11-12 | 2024-03-12 |
|
1.3.x
|
2021-04-14 | 2022-04-14 | 2023-08-14 |
|
1.2.x
|
2020-10-28 | 2021-10-28 | 2023-02-28 |
|
1.1.x
|
2019-09-30 | 2020-09-30 | 2022-01-30 |
社区支持
在 Spring 社区的支持下免费进行安全更新和错误修复。 请参阅 VMware Tanzu OSS 支持策略.
商业支持
在 OSS 期限内由 Spring 专家提供专业业务支持,并在 OSS 生命周期结束后提供扩展支持。
应客户要求公开发布关键错误修复和安全问题。
未来版本
未发布 GA 版本,时间表可能会发生变化。.