How is the RowMapper Method working from Spring 1.2?

558 views Asked by At

I am doing a documentation of a Spring 1.2 implemented giant application for my company which wasn't documented while being coded around 8 years ago.

Right now I am on one of the java classes and stuck on a particular method on how it is actually working, specially the part where "new RowMapper()" starts. I have looked at different row mapper examples but still kind of confused on what it is actually doing. Anyone with Spring experience, please kindly help me out.

I just want to know what the method is doing.

These are imports from Java Sql and Spring Framework for the class.

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.rowset.SqlRowSet;

initOrderList Method

private List initOrderList(java.util.Date ShipDate, final String sqlStatement)
{
Calendar cal = Calendar.getInstance();
cal.setTime(ShipDate);
cal.add(5, 1);
java.util.Date ship1 = cal.getTime();

final java.sql.Date sqlToday = new java.sql.Date(ShipDate.getTime());
final java.sql.Date shipDate = new java.sql.Date(ship1.getTime());
final java.sql.Date ESDDate = new java.sql.Date(ShipDate.getTime());

List ret = this.jdbcTemplate.query(new PreparedStatementCreator()

  new RowMapper
  {
    public PreparedStatement createPreparedStatement(Connection conn)
      throws SQLException
    {
      PreparedStatement ps = conn.prepareStatement(sqlStatement);
      ps.setDate(1, sqlToday);
      ps.setDate(2, shipDate);
      ps.setDate(3, ESDDate);
      return ps;
    }
  }, new RowMapper()
  {
    public Object mapRow(ResultSet rs, int rowNum)
      throws SQLException
    {
      VCData vcData = new VCData();
      vcData.setOrderNo(rs.getString(1).trim());
      vcData.setShipUserID(rs.getString(2).trim());
      vcData.setShipDate(rs.getString(3).trim());
      vcData.setVendorNo(rs.getString(4).trim());
      vcData.setNMDiv(rs.getString(5).trim());
      vcData.setESD(rs.getString(6).trim());
      vcData.setOrigESD(vcData.getESD());

      return vcData;
    }
  });
return ret;}

The full java class link is here: https://app.box.com/s/9w30z7aa3ynpk7cg7bucd298qvdarq1q

Please be detailed as much as possible. I'm an intern and learning. Thanks.

1

There are 1 answers

2
jst On BEST ANSWER

The jdbcTemplate is going to take care of doing all the sql stuff for you and gets a java.sql.ResultSet back. The ResultSet is the representation of your table data from the query execution.

If you were writing plain JDBC code yourself and NOT using Spring, you would have to loop through the ResultSet one row at a time (checking that there is a next record) and process it. Plus all the fun stuff like closing the result set and catching SQLException. Spring abstracts that away from you.

Instead you implement RowMapper (in your case as an anonymous class) and it uses that as a callback. Spring is actually looping through the ResultSet and moving the cursor position. You can think of the pseudo code Spring is exectuting like this

while(resultSet has more results){
    move cursor to next row
    VCData row = //calls your mapRow method, passing in the current row #
    add row to List
}
return List<VCData>
close resultSet and cleanup

You just have to tell it what to do on each row. VCData is your representation of the row. The List you get back and store in ret is a list of all the rows as VCData objects.