• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

explanation on RowMapper's mapRow method

 
Abdul Rahman
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm unable to comprehend the method mapRow(ResultSet rs, int rownum) in the RowMapper interface. I tried to go through the API but i'm still not clear on this.

Can anyone please explain what this method does and what's it used for.

Thanks
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to post this yesterday


So JDBC queries return ResultSet objects. But your application has Domain objects like Order or Item. You need to convert the ResultSet into a Domain object. This normally tedious pain in the arse work, that is error proned, and in different queries might have duplicate code.

By using a RowMapper you can centralize that code in one class.

So when you run a Query that returns rows into a ResultSet and you pass in a RowMapper to the JdbcTemplate, then the template will do the work of looping through the resultset and passing it row by row to the mapRow method. The return of the mapRow will be an Object of the type that you convert the row to. In the mapRow method you write the code like

Order order = new Order();
order.setOrderDate(rs.getDate(0));
etc.

I forget the exact api method call on result set to get the field value of a date, but that is the general idea.

Mark
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic