Search code examples
javajdbijdbi3jdbi3-core

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?


Solution

  • 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