为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 Mono<Connection> 或 R2dbc 在 Spring 提供的

如何使用 Mono<Connection> 或 R2dbc 在 Spring 提供的

www说 2022-09-07 17:09:39
我不知道如何使用spring-webflux(反应式)在R2dbc(java)中构建有效的查询。使用 R2dbc 提供的 DatabaseClient 对象(或者,连接对象),似乎我只能调用这两种方法之一的不同变体:或 。如果我在Java中有一个模式和一个相应的类,具有多个可为null的字段,那么我应该如何有效地处理这个问题?bind(Object field, Object value)bindNull(Object field, Class<?> type)举个例子:public Flux<Item> saveOrUpdate(Item entity) {   Mono<Connection> connection = this.connection;    Flux<? extends Result> itemFlux = connection       .doOnError(e -> e.printStackTrace())           .flatMapMany(connect  ->  connect.createStatement(INSERT_OR_UPDATE_ITEM)                .bind("itemId", entity.getItemId()).returnGeneratedValues("itemid")                .bind("auditId", entity.getTx().getId())                .bind("itemNum", entity.getItemNum())                .bind("itemCat", entity.getItemCat()) //nullable                 // How would I know when to use this?                .bindNull("sourcedQty", Integer.class) //nullable                .bind("makeQty", entity.getMakeQty())                .bind("nameShown", entity.getNameShown()) //nullable                .bind("price", entity.price())                .bind("dateCreated", entity.getDateCreated()) //nullable                .add()                .execute())...   ...}或public Mono<Item> saveOrUpdate(Item entity){   Mono<Item> itemMono = databaseClient.execute.sql(INSERT_OR_UPDATE_ITEM)      .bind("itemId", entity.getItemId()).returnGeneratedValues("itemid")                .bind("auditId", entity.getTx().getId())                .bind("itemNum", entity.getItemNum())                .bind("itemCat", entity.getItemCat())                .bind("sourcedQty", entity.getSourcedQty())                 .bind("makeQty", entity.getMakeQty())                .bind("nameShown", entity.getNameShown())                .bind("price", entity.price())                .bind("dateCreated", entity.getDateCreated())                .as(Item.class)                .fetch()                .one()...   ...}
查看完整描述

2 回答

?
拉风的咖菲猫

TA贡献1995条经验 获得超2个赞

这是两个问题:

  1. 如何以流畅的风格将潜在的可为空的值绑定到/?StatementDatabaseClient

  2. 如何让数据库计算出其余部分?

R2DBC 和 Spring Data R2DBC 通过要求将值绑定到或绑定 .没有方法可以接受可能为空的参数。这有两个原因:nullStatementnull

  1. 你应该处理可空性,以表明那里发生了什么。这是处理可为 null 的值的好习惯,而不是使处理隐式。的隐式本质是导致最多错误的原因。nullnull

  2. 数据库需要显式。带有占位符的参数化语句位于两个块的执行端:SQL 语句本身和参数绑定(描述符)。参数描述符需要与占位符、类型信息 (、 、 、 ...) 和实际值相关联。使用值进行调用时,驱动程序可以派生类型信息。绑定值时,驱动程序需要其他类型的信息。否则,我们无法执行查询。VARCHARBITINTbind(…)null

话虽如此:

  1. 没有像bindPotentiallyNull("auditId", entity.getTx().getId(), Integer.class)

  2. 您无法在 SQL 查询中执行任何操作,因为绑定参数信息由辅助方法提供。

在谈论存储过程时,我们面临着类似的问题,因为存储过程需要有关输入/输出/输入输出参数的其他详细信息。我们讨论了潜在的包装器类型,例如

Parameters.in(@Nullable T value, Class<? super T> valueType)

所以这些可以用作包装器

bind("auditId", Parameters.in(entity.getTx().getId(), Integer.class))


查看完整回答
反对 回复 2022-09-07
?
守着一只汪

TA贡献1872条经验 获得超4个赞

可以使用如下所示的类设置值或 null:Parameter


import org.springframework.r2dbc.core.Parameter;


// rest of the code


.bind("customerId", Parameter.fromOrEmpty(o.getCustomerId(), UUID.class))

早些时候,它是 ,未被弃用。SettableValue.fromOrEmpty


查看完整回答
反对 回复 2022-09-07
  • 2 回答
  • 0 关注
  • 234 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号