gpt4 book ai didi

java - JDBI 没有映射器注册

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:18:01 28 4
gpt4 key购买 nike

我正在评估 JDBI 作为 Spring JDBC 和 MyBatis 的可能替代品,但遇到了一些问题。我在 Spring Boot 1.2.5 和 Spring 4 中使用 JDBI。

我得到以下堆栈跟踪,如下所示。我究竟做错了什么?文档似乎有点缺乏。

org.skife.jdbi.v2.MappingRegistry$1: No mapper registered for net.jkratz.ams.domain.User
at org.skife.jdbi.v2.MappingRegistry.mapperFor(MappingRegistry.java:78)
at org.skife.jdbi.v2.RegisteredMapper.map(RegisteredMapper.java:37)
at org.skife.jdbi.v2.Query$4.munge(Query.java:183)
at org.skife.jdbi.v2.QueryResultSetMunger.munge(QueryResultSetMunger.java:43)
at org.skife.jdbi.v2.SQLStatement.internalExecute(SQLStatement.java:1340)
at org.skife.jdbi.v2.Query.fold(Query.java:173)
at org.skife.jdbi.v2.Query.list(Query.java:82)
at org.skife.jdbi.v2.Query.list(Query.java:75)
at net.jkratz.ams.dao.UserDao.getUsers(UserDao.java:28)
at net.jkratz.ams.dao.UserDao$$FastClassBySpringCGLIB$$5459a2dd.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at net.jkratz.ams.dao.UserDao$$EnhancerBySpringCGLIB$$91d5254a.getUsers(<generated>)
at net.jkratz.ams.AmsApplicationTests.testJdbi(AmsApplicationTests.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

这是我的Spring配置

@Configuration
@EnableTransactionManagement
public class PersistenceConfig {

@Autowired
Environment environment;

@Bean(name = "datasource")
public ComboPooledDataSource dataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(environment.getRequiredProperty("c3p0.driver"));
dataSource.setJdbcUrl(environment.getRequiredProperty("c3p0.url"));
dataSource.setUser(environment.getRequiredProperty("c3p0.user"));
dataSource.setPassword(environment.getRequiredProperty("c3p0.password"));
dataSource.setInitialPoolSize(environment.getRequiredProperty("c3p0.initialPoolSize", Integer.class));
dataSource.setMaxPoolSize(environment.getRequiredProperty("c3p0.maxPoolSize", Integer.class));
dataSource.setMinPoolSize(environment.getRequiredProperty("c3p0.minPoolSize", Integer.class));
dataSource.setAcquireIncrement(environment.getRequiredProperty("c3p0.acquireIncrement", Integer.class));
dataSource.setMaxStatements(environment.getRequiredProperty("c3p0.maxStatements", Integer.class));
dataSource.setMaxIdleTime(environment.getRequiredProperty("c3p0.maxIdleTime", Integer.class));
return dataSource;
}

@Bean(name = "dbi")
public DBIFactoryBean dbiFactoryBean() throws PropertyVetoException {
DBIFactoryBean dbiFactoryBean = new DBIFactoryBean();
dbiFactoryBean.setDataSource(dataSource());
return dbiFactoryBean;
}
}

这是我的用户类

public class User implements Serializable {

private long id;
private String userName;
private String password;
private String passwordHashed;
private String firstName;
private String lastName;
private boolean locked;
private Date createdAt;
private Date updatedAt;

....
}

我的道

@Repository
@RegisterMapper(UserMapper.class)
public class UserDao {

@Autowired
IDBI dbi;

public UserDao() {

}

public List<User> getUsers() {
Handle h = dbi.open();
List<User> users = h.createQuery("select * from users")
.map(UserMapper.class).mapTo(User.class).list();
h.close();
return users;
}
}

还有我的映射器

public class UserMapper implements ResultSetMapper<User> {

@Override
public User map(int i, ResultSet resultSet, StatementContext statementContext) throws SQLException {
User user = new User();
user.setId(resultSet.getLong("id"));
user.setUserName(resultSet.getString("username"));
user.setPasswordHashed(resultSet.getString("password"));
return user;
}
}

最佳答案

我似乎解决了使用 API 时遇到的问题。这是更正后的 DAO。

public List<User> getUsers() {

return dbi.open().createQuery("select * from users").map(new UserMapper()).list();
}

关于java - JDBI 没有映射器注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32447912/

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