About spring Rowmapper , mapRow

11,687

The RowMapper is used to map a single row to a single domain object, not a bunch of rows results to a single domain object. Also the RowMapper isn't a Dao type object. It is to be used with some query method, like JdbcTemplate.query(sql,args,rowMapper)

But in your case, you don't want a RowMapper. You should instead just use a JdbcTemplate.queryForList. See the JdbcTemplate API for more query method. A simple example would be something like:

public class YourDaoImpl extends JdbcTemplate implements YourDao {
    private static final String SQL = 
            "select SOME_FIELD from SOME_TABLE where SOMETHING = ?";

    @Override
    public List<String> getSomeFieldBySomething(String something) {
        return (List<String>)queryForList( SQL, 
                                           new Object[] { something }, 
                                           String.class);
    }
}

You use the dao for your services.


UPDATE

Because of your help, I can get a one column from my DB. but I got a problems. my db table is made of multiple columns. and i must receive all of them. and.. how can i do it? plz help me~!!!

You posted question in no points that out. In this case you need to make a List<DomainObject>. Not a List<String>. List<String> only allows for one value. If you have a List<DomainObject>, then the class DomainObject can have all your fields. Then that's when you use the RowMapper. And you can still use queryForList that uses the RowMapper variant

public class Table {
    private String field1;
    private String field2;
    private String field3;
    // getters and setters
}

public class YourDaoImpl extends JdbcTemplate implements YourDao {
    private static final String SQL = 
            "select * from SOME_TABLE where SOMETHING = ?";

    @Override
    public List<Table> getTableBySomething(String something) {
        return (List<Table>)queryForList( SQL, 
                                          new Object[] { something }, 
                                          new RowMapper<Table>(){
            @Override
            public Table mapRow(ResultSet rs, int rowNumber) {
                Table table = new Table();
                table.setField1(rs.getString("feild1"));
                // set the others
                return table;
            }
        });
    }
}

An aside, if I were you, I would forget the jdbc and go for an ORM framework like JPA. If you want entire domain objects, this is the way to go.

Share:
11,687
Double J
Author by

Double J

Updated on July 11, 2022

Comments

  • Double J
    Double J almost 2 years

    I have a some questions about Spring rowmapper. I'm going to receive data from my DB using rowmapper.But my command object 'Table' only have List variable. is Rowmapper automatically map each record to List ? is it posibble? i know spring bind tag is automatically bind value to list.

    right this.

    Table.java

    public class Table implements Serializable{
        private List<String> tableNum = new ArrayList<String>();
    
    // setter and getter
    }
    

    Dao

    private class TableRowMapper implements RowMapper {
             public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                    Table table = new Table();
                table.setTableNum(rs.getString("TABLE_LOCATION"));
                return table;
            }
        }
    
  • Double J
    Double J almost 10 years
    Oh peeskillet. Thanks you! that's awesome. good! queryForList!
  • Double J
    Double J almost 10 years
    Because of your help, I can get a one column from my DB. but I got a problems. my db table is made of multiple columns. and i must receive all of them. and.. how can i do it? plz help me~!!!