spring row mapper

Posted on 2011-05-06
Last Modified: 2012-05-11
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.
Question by:gudii9
    LVL 47

    Accepted Solution

    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

    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();
            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
    I think it will become even more understandable.

    LVL 20

    Assisted Solution

    by:Sathish David Kumar N
    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.
    LVL 92

    Assisted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now