what is difference between ResultSetExtractor vs Rowmapper?

jdbctemplate, spring

Solution

JavaDoc of `ResultSetExtractor`:

This interface is mainly used within the JDBC framework itself. A RowMapper is usually a simpler choice for ResultSet processing, mapping one result object per row instead of one result object for the entire ResultSet.

`ResultSetExtractor` is suppose to extract the whole `ResultSet` (possibly multiple rows), while `RowMapper` is feeded with row at a time.

Most the time, `ResultSetExtractor` will loop the `ResultSet` and use `RowMapper`, snippet example of Spring `RowMapperResultSetExtractor`:

List<T> results = (this.rowsExpected > 0 ? new ArrayList<T>(this.rowsExpected) : new ArrayList<T>());
int rowNum = 0;
while (rs.next()) {
    results.add(this.rowMapper.mapRow(rs, rowNum++));
}
return results;

Pay attention, ALL results will be transformed, this can create Out Of Memory exception.

See also

- `RowMapperResultSetExtractor`

Problem

I worked on both row mapper and resultset extractor call back interfaces.I found difference i.e., 1.Row mapper can be processing per row basis.But Resultset extractor we can naviagte all rows and return type is object. Is there any difference other than above?.How the works Rowmapper internal and return type is list?.

Original source