Igor Vinicius
Igor Vinicius

Reputation: 111

JDBI erro No mapper registered for type

I'm trying to create a method that return all users from customer table in my database but this code:

    try {
        List<Customer> customer = jdbi.open().createQuery("SELECT * FROM customer")
        .mapTo(Customer.class).list();
    } catch (Exception e) {
        System.out.println(e);
    }

return error:

org.jdbi.v3.core.mapper.NoSuchMapperException: No mapper registered for type com.customer.Customer

i saw another post of this error and they say that this error happened because of the mapping between the class model and the database table, but my app is built in pure java and JDBI 3 I'm not using Spring so how map the result of this code for convert this result in a List of Customer?

Upvotes: 2

Views: 3205

Answers (2)

Panu Haaramo
Panu Haaramo

Reputation: 2932

For me this usually happens when running tests with IntelliJ. Everything works fine when running the same tests with Maven.

Upvotes: 0

Igor Vinicius
Igor Vinicius

Reputation: 111

I fix this creating this class

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

and where was .mapTo(Customer.class).list(); i put .map(new CustomerMapper()).list(); referencing the map I created

Upvotes: 1

Related Questions