gpt4 book ai didi

java - JDBI 可选注册

转载 作者:行者123 更新时间:2023-12-02 09:40:12 28 4
gpt4 key购买 nike

我收到没有可用于 com.google.common.base.Optional 的容器构建器错误。

这是更完整的堆栈跟踪:

java.lang.IllegalStateException: No container builder available for com.google.common.base.Optional
at org.skife.jdbi.v2.ContainerFactoryRegistry.createBuilderFor(ContainerFactoryRegistry.java:75)
at org.skife.jdbi.v2.Query.first(Query.java:271)
at org.skife.jdbi.v2.sqlobject.ResultReturnThing$SingleValueResultReturnThing.result(ResultReturnThing.java:112)
at org.skife.jdbi.v2.sqlobject.ResultReturnThing.map(ResultReturnThing.java:48)
at org.skife.jdbi.v2.sqlobject.QueryHandler.invoke(QueryHandler.java:45)
at org.skife.jdbi.v2.sqlobject.SqlObject.invoke(SqlObject.java:175)
at org.skife.jdbi.v2.sqlobject.SqlObject$1.intercept(SqlObject.java:75)
at org.skife.jdbi.v2.sqlobject.CloseInternalDoNotUseThisClass$$EnhancerByCGLIB$$b270edb1.select(<generated>)

我有一个 DAO,其界面如下:

import com.google.common.base.Optional;

public interface MyDAO {
@SqlQuery("something")
Optional<Data> select();
}

这是我的数据访问层单元测试:

public class MyDAOTest {
@Test
public void shouldSelect() {
DBI dbi = new DBI("jdbc:(something)", "something", "something");
MyDAO myDAO = dbi.onDemand(MyDAO.class);
Optional<Data> data = myDAO.select();
assertFalse(data.absent());
}
}

最佳答案

注册 io.dropwizard.jdbi.OptionalContainerFactory如果您使用 com.google.common.base.Optional 并收到错误没有可用于 com.google.common.base.Optional 的容器构建器

注册 io.dropwizard.java8.jdbi.OptionalContainerFactory如果您使用 java.util.Optional 并收到错误 No container builder available for java.util.Optional

我按照指示 here 进行操作:

public class MyDAOTest {
@Test
public void shouldSelect() {
DBI dbi = new DBI("jdbc:(something)", "something", "something");
dbi.registerContainerFactory(new OptionalContainerFactory());
...
}

关于java - JDBI 可选注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31395271/

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