Chr*_*yer 6 java spring-data spring-webflux spring-data-r2dbc r2dbc
我不知道如何使用 spring-webflux (反应式)在 R2dbc (java)中构造有效的查询。使用 R2dbc 提供的 DatabaseClient 对象(或者 Connection 对象),我似乎只能调用这两种方法之一的不同变体:bind(Object field, Object value)或bindNull(Object field, Class<?> type)。如果我有一个模式,以及 Java 中的相应类,具有多个可为 null 的字段,那么我应该如何[某种程度上]有效地处理这个问题?
举个例子:
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())...
...
}
Run Code Online (Sandbox Code Playgroud)
或者
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()...
...
}
Run Code Online (Sandbox Code Playgroud)
对于可为空的字段,我当然可以用 .bindNull 替换 .bind 。问题是,如果我调用bind,该值不能为空。如果我调用bindNull,该值必须为null。我如何根据我的值是否实际上为空来调用其中一个?我已经知道我可以为每个场景创建一堆方法或调用类似 retryOnError 的方法。但如果我想做的话,insertOrUpdate(List<Item> items)这会浪费大量的时间/资源。理想情况下,我想做一些类似于if (field == null) ? bindNull("field", field.class) : bind("field", myObj.field)某处的事情。如果这显然是不可能的,我仍然有兴趣找出一种方法来尽可能有效地实现这一点,因为我正在处理的工作。感谢任何反馈。
可以使用Parameter类来设置值或 null,如下所示:
import org.springframework.r2dbc.core.Parameter;
// rest of the code
.bind("customerId", Parameter.fromOrEmpty(o.getCustomerId(), UUID.class))
Run Code Online (Sandbox Code Playgroud)
早些时候,它是SettableValue.fromOrEmpty,但并未弃用。
这是两个问题:
\n\nStatement/ ?DatabaseClientR2DBC 和 Spring Data R2DBCnull通过要求将值绑定到您的值Statement或绑定null. 没有方法接受可能为空的参数。原因有二:
null。隐含的性质null是导致最多错误的原因。VARCHAR参数描述符需要与占位符、类型信息( 、BIT、INT、 \xe2\x80\xa6)和实际值关联。通过bind(\xe2\x80\xa6)使用值进行调用,驱动程序可以导出类型信息。绑定null值时,驱动程序需要附加类型的信息。否则,我们无法执行查询。话虽如此:
\n\nbindPotentiallyNull("auditId", entity.getTx().getId(), Integer.class)在讨论存储过程时,我们面临类似的问题,因为存储过程需要有关输入/输出/输入输出参数的附加详细信息。我们讨论了潜在的包装类型,例如
\n\nParameters.in(@Nullable T value, Class<? super T> valueType)\nRun Code Online (Sandbox Code Playgroud)\n\n所以这些可以用作包装
\n\nbind("auditId", Parameters.in(entity.getTx().getId(), Integer.class))\nRun Code Online (Sandbox Code Playgroud)\n\n更多细节:
\n\n\n| 归档时间: |
|
| 查看次数: |
5409 次 |
| 最近记录: |