问题描述
我正在尝试创建一个方法,该方法从我的数据库中的客户表中返回所有用户,但此代码:
try {
List<Customer> customer = jdbi.open().createquery("SELECT * FROM customer")
.mapTo(Customer.class).list();
} catch (Exception e) {
System.out.println(e);
}
返回错误:
org.jdbi.v3.core.mapper.NoSuchMapperException: No mapper registered for type com.customer.Customer
我看到另一个关于这个错误的帖子,他们说这个错误是由于类模型和数据库表之间的映射而发生的,但是我的应用程序是用纯 Java 和 jdbi 3 构建的我没有使用 Spring 所以如何映射此代码的结果将此结果转换为客户列表?
解决方法
我修复了这个创建这个类
public class CustomerMapper implements RowMapper<Customer>{
@Override
public Customer map(ResultSet rs,StatementContext ctx) throws SQLException {
return new Customer(rs.getInt("id"),rs.getString("uuid"),rs.getString("name"),rs.getString("email"),rs.getString("birthDate"),rs.getString("cpf"),rs.getString("gender"),rs.getDate("createdAt"),rs.getDate("updateAt"));
}
}
.mapTo(Customer.class).list();
在哪里,我把 .map(new CustomerMapper()).list();
引用我创建的地图