JDBI没有Mapper注册

时间:2015-09-08 01:20:37

标签: java spring jdbi

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

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

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;

    ....
}

我的DAO

@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;
    }
}

我的Mapper

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;
    }
}

3 个答案:

答案 0 :(得分:1)

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

public List<User> getUsers() {

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

答案 1 :(得分:0)

如果您的映射比较琐碎,则可以跳过映射器而直接执行以下操作:

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

另外,当字段名称不同于列名称时,您始终可以添加@ColumnName批注,例如:

public class User {
    @ColumnName("first_name")
    private String firstName;
}

当然,您可以决定使用DTO而不是污染域对象。

答案 2 :(得分:0)

对我来说,解决方案是在映射器类中添加空构造函数。