简体   繁体   English

Spring R2DBC DatabaseClient.as(…)

[英]Spring R2DBC DatabaseClient.as(…)

In my spring-boot 2.3 application, I have a simple data method using DatabaseClient :在我的 spring-boot 2.3 应用程序中,我有一个使用DatabaseClient的简单数据方法:

fun getCurrentTime(): Mono<LocalDateTime> =
    databaseClient
        .execute("SELECT NOW()")
        .asType<LocalDateTime>()
        .fetch()
        .first()
}

With spring-boot 2.4 (and spring 5.3 and spring-data-r2dbc 1.2), org.springframework.data.r2dbc.core.DatabaseClient from spring-data-r2dbc is deprecated in favor of org.springframework.r2dbc.core.DatabaseClient of spring-r2dbc - which has a different API.与弹簧引导2.4(和弹簧5.3和弹簧数据r2dbc 1.2), org.springframework.data.r2dbc.core.DatabaseClient从弹簧数据r2dbc是赞成弃用org.springframework.r2dbc.core.DatabaseClient的spring-r2dbc - 具有不同的 API。

Adapting that is pretty much straightforward - with the exception of the kotlin extension asType , which is not a part of the new DatabaseClientExtensions.调整它非常简单 - 除了 kotlin 扩展asType ,它不是新的 DatabaseClientExtensions 的一部分。

fun getCurrentTime(): Mono<LocalDateTime> =
    databaseClient
        .sql("SELECT NOW()")
        .map { row: Row ->
            row.get(0, LocalDateTime::class.java)!!
        }
        .one()

Are those extensions somewhere else or how can I convert using a reified type parameter?这些扩展是其他地方还是我如何使用具体化类型参数进行转换?

TL;DR TL; 博士

There's no as(Class) API after the migration to Spring R2DBC.迁移到 Spring R2DBC 后没有as(Class) API。

A bit of background一点背景

DatabaseClient started its journey in the experimental Spring Data R2DBC project, trying out various approaches. DatabaseClient在实验性 Spring Data R2DBC 项目中开始了它的旅程,尝试了各种方法。 One of them evaluated how close a textual SQL API and an object-mapping API could be brought together.其中一个评估了文本 SQL API 和对象映射 API 可以结合在一起的程度。 DatabaseClient in Spring Data exposed various API methods such as select().from("table").as(targetType) . Spring Data 中的DatabaseClient暴露了各种 API 方法,例如select().from("table").as(targetType)

It turned out that this functionality is useful but draws certain limitations because the more an API goes into an entity or even aggregate-oriented direction, the more complexity the actual API becomes and at some point, boundaries between simple object mapping and entities (for example, entity lifecycle callbacks) blur.事实证明,此功能很有用,但也有一定的局限性,因为 API 进入实体甚至面向聚合的方向越多,实际 API 变得越复杂,并且在某些时候,简单对象映射和实体之间的边界(例如, 实体生命周期回调)模糊。

We decided to introduce R2dbcEntityTemplate as an abstraction for all entity-bound operations to support most common use-cases.我们决定引入R2dbcEntityTemplate作为所有实体绑定操作的抽象,以支持最常见的用例。 Looking at the fluent API that was previously in place, there's still a gap for all use-cases that require ad-hoc SQL queries, aggregations, function calls, etc.看看之前的 fluent API,所有需要即席 SQL 查询、聚合、函数调用等的用例仍然存在差距。

In the meantime, the project proved useful, and we've identified core support classes that could be migrated into Spring Framework 5.3, so Spring Data R2DBC 1.2 could be based on top of Spring R2DBC.与此同时,该项目证明是有用的,我们已经确定了可以迁移到 Spring Framework 5.3 的核心支持类,因此 Spring Data R2DBC 1.2 可以基于 Spring R2DBC。

We weren't able to come up with a proper approach while migrating the code.在迁移代码时,我们无法想出合适的方法。 To be fair, DatabaseClient offers almost the same level of abstraction (except for stored procedures) as NamedParameterJdbcTemplate .公平地说, DatabaseClient提供了与NamedParameterJdbcTemplate几乎相同的抽象级别(存储过程除外)。 Spring JDBC ships clearly with several RowMapper implementations such as SingleColumnRowMapper or DataClassRowMapper that would be useful for Spring R2DBC, too. Spring JDBC 明确附带了几个RowMapper实现,例如SingleColumnRowMapperDataClassRowMapper ,它们对 Spring R2DBC 也很有用。

Final thoughts最后的想法

From a user-perspective, as(…) sees a lot of demand and we should investigate, how this functionality (or a variant of it) could be surfaced.从用户的角度来看, as(…)看到了很多需求,我们应该调查一下,这个功能(或它的一个变体)是如何出现的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM