Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is difference between ResultSetExtractor vs Rowmapper?

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?.

like image 491
user1127214 Avatar asked Apr 09 '12 13:04

user1127214


People also ask

What is ResultSetExtractor?

ResultSetExtractor interface is a callback interface used by JdbcTemplate's query methods. Implementations of this interface perform the actual work of extracting results from a ResultSet, but don't need to worry about exception handling. SQLExceptions will be caught and handled by the calling JdbcTemplate.

What is a RowMapper?

Interface RowMapper<T>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.

What is Bean property RowMapper?

BeanPropertyRowMapper is a RowMapper implementation that converts a table row into a new instance of the specified mapped target class. The mapped target class must be a top-level class and it must have a default or no-arg constructor.

How do you write a RowMapper?

Step 1 − Create a JdbcTemplate object using a configured datasource. Step 2 − Create a StudentMapper object implementing RowMapper interface. Step 3 − Use JdbcTemplate object methods to make database operations while using StudentMapper object.


1 Answers

Basic difference is with ResultsetExtractor you will need to iterate through the result set yourself, say in while loop. This interface provides you processing of the entire ResultSet at once. The implemetation of Interface method extractData(ResultSet rs) will contain that manual iteration code. See one implementation of ResultsetExtractor

while some callback handlers like RowCallbackHandler, the interface method processRow(ResultSet rs) loops for you.

RowMapper can be used both was for mapping each row, or entire rows.

For entire rows Object (by template method jdbcTemplate.query())

 public List findAll() {         String sql = "SELECT * FROM EMPLOYEE";     return jdbcTemplate.query(sql, new EmployeeRowMapper()); }  without casting will work 

For individual object (with Template method jdbcTemplate.queryForObject())

@SuppressWarnings({ "unchecked", "rawtypes" }) public Employee findById(int id) {     String sql = "SELECT * FROM EMPLOYEE WHERE ID = ?"; //  jdbcTemplate = new JdbcTemplate(dataSource);      Employee employee = (Employee) jdbcTemplate.queryForObject(sql,  new EmployeeRowMapper(), id );      // Method 2 very easy     //  Employee employee = (Employee) jdbcTemplate.queryForObject(sql, new Object[] { id }, new BeanPropertyRowMapper(Employee.class));      return employee; } 

@SuppressWarnings("rawtypes") public class EmployeeRowMapper implements RowMapper {  public Object mapRow(ResultSet rs, int rowNum) throws SQLException {     Employee employee = new Employee();     employee.setId(rs.getInt("ID"));     employee.setName(rs.getString("NAME"));     employee.setAge(rs.getInt("AGE"));     return employee; } 

}

Best Use cases:

Row Mapper: When each row of a ResultSet maps to a domain Object, can be implemented as private inner class.

RowCallbackHandler: When no value is being returned from callback method for each row, e.g. writing row to a file, converting rows to a XML, Filtering rows before adding to collection. Very efficient as ResultSet to Object mapping is not done here.

ResultSetExtractor: When multiple rows of ResultSet map to a single Object. Like when doing complex joins in a query one may need to have access to entire ResultSet instead of single row of rs to build complex Object and you want to take full control of ResultSet. Like Mapping the rows returned from the join of TABLE1 and TABLE2 to an fully-reconstituted TABLE aggregate.

ParameterizedRowMapper is used to create complex objects

like image 185
vimal krishna Avatar answered Sep 25 '22 03:09

vimal krishna