Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 857
  • Last Modified:

spring row mapper

I was reading folowing lines and I did not understand clearly.

The RowMapper interface is responsible for mapping a ResultSet row to an
object. To map a row to Person object, we would create a RowMapper


Any ideas, resources,sample code,links, highly appreciated. thanks in advance.
0
gudii9
Asked:
gudii9
3 Solutions
 
for_yanCommented:
RowMapper is an interface used by JdbcTemplate class for mapping rowsof the ResultSet to fileds of the object.

JdbcTemplate in conjunction with RowMapper allow to produce very compact code which populates the objects from database.
This interface has one method

Object mapRow(Resultset rs, int rowNum)

A good expamle is given in
http://static.springsource.org/spring/docs/2.0.8/reference/jdbc.html

Here JdbcTemplate class together with ActorMapper class implemnting RowMapper interface
allow to populate a Collection of Actor objects form the table in very compact way:


public Collection findAllActors() {
    return this.jdbcTemplate.query( "select first_name, surname from t_actor", new ActorMapper());
}

private static final class ActorMapper implements RowMapper {

    public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
        Actor actor = new Actor();
        actor.setFirstName(rs.getString("first_name"));
        actor.setSurname(rs.getString("surname"));
        return actor;
    }
}

Open in new window


As you see through JdbcTemplate class and  RowMapper interafce
spring releives the programmer form lots of boring and more or less
all the tiem the same details, such as establishing database connection,
iterating through ResultSet, etc. The developer just can concentrate on the query  
and on the result.

Browse through this chapter
http://static.springsource.org/spring/docs/2.0.8/reference/jdbc.html
I think it will become even more understandable.





0
 
Sathish David Kumar NCommented:
An interface used by JdbcTemplate for mapping rows of a ResultSet on a per-row basis. Implementations of this interface perform the actual work of mapping each row to a result object, but don't need to worry about exception handling. SQLExceptions will be caught and handled by the calling JdbcTemplate.

Typically used either for JdbcTemplate's query methods or for out parameters of stored procedures. RowMapper objects are typically stateless and thus reusable; they are an ideal choice for implementing row-mapping logic in a single place.

Alternatively, consider subclassing MappingSqlQuery from the jdbc.object package: Instead of working with separate JdbcTemplate and RowMapper objects, you can build executable query objects (containing row-mapping logic) in that style.
0
 
objectsCommented:
Think about when you do a straight jdbc select. What you typically will have is a loop going through the rows returned by the result set. For each row you get the value of each column and build a bean from these values.  RowMapper is a callback interface that JdbcTemplate call to handle the extracting the values from a row creating a bean representing that row. ie. it maps a row of the result set to an application bean

Have a look at the example here and let me know if you have any questions

http://www.vogella.de/articles/SpringJDBC/article.html#jdbc_usage_dao
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now