gpt4 book ai didi

java - 如何在 JDBI 3 中使用列表绑定(bind) Bean 参数

转载 作者:行者123 更新时间:2023-11-29 12:49:44 32 4
gpt4 key购买 nike

我正在尝试使用 SqlObject API 在 Postgres 中存储 Bean POJO 列表。我的界面如下所示:

@UseClasspathSqlLocator
@RegisterBeanMapper(SomeBean.class)
public interface LeadStateDao {

@SqlUpdate
@GetGeneratedKeys
SomeBean update(@BindBean SomeBean bean);

@SqlUpdate
@GetGeneratedKeys
SomeBean upsert(@BindBean SomeBean bean);

@SqlUpdate
@GetGeneratedKeys
int[] batchUpsert(@BindList List<SomeBean> beans);

}

我正在使用参数进行编译,这就是我没有明确命名参数的原因(这工作正常)。由于 @RegisterBeanMapperupdateupsert 方法按预期工作。但是 batchUpsert 没有,我得到:

java.lang.UnsupportedOperationException: No argument factory registered for 'com.foo.SomeBean@49d11f58' of type class com.foo.SomeBean

有没有一种方法可以将 bean 绑定(bind)为参数,作为列表的一部分,而不必编写自定义参数工厂? The docs在这里令人沮丧地含糊不清:

java.util.Collection and Java arrays (stored as SQL arrays). Some additional setup may be required depending on the type of array element.

不确定“一些额外的设置” 需要什么。

最佳答案

我从来没有像这样使用过@BindList(用于批量操作),我不知道它是否可以与 bean 一起使用。

作为the docs说:

Binding a list of values is done through the @BindList annotation. This will expand the list in a 'a,b,c,d,…​' form. Note that this annotation requires you to use the <binding> notation, unlike @Bind (which uses :binding)

如果你有

@BindList List<Long> data

作为你必须使用的接口(interface)中的参数

select * from my_data where id in (<data>)

作为查询(注意括号而不是冒号)。我怀疑您是否可以将它与 bean 一起使用!

关于java - 如何在 JDBI 3 中使用列表绑定(bind) Bean 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57065208/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com