how to map a list of pojos in jdbcTemplate query without using a RowMapper class or BeanRowMapper

12,427

This code is working in one of my projects:

List<Map<String, Object>> rows = administradoresDao.list();
    List<Administrador> usuarios = new ArrayList<>();

    for (Map<String, Object> row : rows) {
        Administrador usuario = new Administrador();
        usuario.setId(Integer.valueOf(row.get("id").toString()));
        usuario.setNombre(row.get("nombre").toString()+ " "+row.get("a_pat").toString()+" "+row.get("a_mat").toString());
        usuario.setDependencia(row.get("dependencia").toString());
        usuario.setEmail(row.get("email").toString());
        usuario.setTelefono(row.get("telefono").toString());
        usuario.setExtension(row.get("extension").toString());
        usuarios.add(usuario);
    }

The DAO:

public List<Map<String, Object>> list() {
    return jdbcTemplate.queryForList("Select * from Administradores");
}
Share:
12,427
stackUser2000
Author by

stackUser2000

Updated on June 04, 2022

Comments

  • stackUser2000
    stackUser2000 almost 2 years

    i'm trying to fill a list pojos using jdbcTemplate but i dont want to create a RowMapper class for every pojo in my domain,also i have less atributes in my domain classes and i have more in my tables in the databases so i can't use BeanRowMapper, i found a example in this website http://www.mkyong.com/spring/spring-jdbctemplate-querying-examples/

    but my problem is that this example didn't worked at firts

    the example is the following one:

    public List<Customer> findAll(){
    
        String sql = "SELECT * FROM CUSTOMER";
    
        List<Customer> customers = new ArrayList<Customer>();
    
        List<Map> rows = getJdbcTemplate().queryForList(sql);
        for (Map row : rows) {
            Customer customer = new Customer();
            customer.setCustId((Long)(row.get("CUST_ID")));
            customer.setName((String)row.get("NAME"));
            customer.setAge((Integer)row.get("AGE"));
            customers.add(customer);
        }
    
        return customers;
    }
    

    but this example was giving me a error in this line

    List<Map> rows = getJdbcTemplate().queryForList(sql);
    

    the error was this:

    Error   incompatible types: java.util.List<java.util.Map<java.lang.String,java.lang.Object>> cannot be converted to java.util.List<java.util.Map>   
    

    so netbeans after i right clicked the line, netbeans changed the line to this

    List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);
    

    so i didn't longer have that error, but now i method returns a list full of null objects, here is my method after the changes

     @Override
    public ArrayList<Rol> findAll() {
    
        ArrayList<User> listOfUsers=  null;
    
        try 
        {
           String sql = "select * from User";
           listOfUsers =  new ArrayList<User>();
    
           List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);
           for (Map row : rows) 
           {
                User user= new User ();
                user.setName((String)(row.get("name")));
                user.setLastName((String)row.get("lastName"));
                user.setType((String)row.get("type"));
    
    
                listOfUsers .add(user);
           }
        } 
        catch (DataAccessException dataAccessException) 
        {
    
            dataAccessException.printStackTrace();
        }
        catch(Exception e) 
        {
    
            e.printStackTrace();
        }
    
        return listOfUsers;
    }
    
  • stackUser2000
    stackUser2000 about 9 years
    i should talk to you in spanish hehe but i don't know if that is agaisnt the rules, i tried your code but it give me a null pointer exception in this line @José Arcángel List<Map<String, Object>> rows = jdbcTemplate.queryForList("Select * from Usuario"); i debug the code and is actually a InvalidTargetException, i dont know why the method queryForList is not returning nothing.
  • José Arcángel
    José Arcángel about 9 years
    That sounds like some spring configuration problem or some injection problem. Did you add the "@Autowire" in the jdbcTemplate? Also, check if your class is included in the injection with "@Component", "@Service" or "@Repository"