Open In App

Spring – Difference Between RowMapper and ResultSetExtractor

Last Updated : 20 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Spring is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. It made the development of Web applications much easier compared to classic Java frameworks and application programming interfaces (APIs), such as Java database connectivity (JDBC), JavaServer Pages(JSP), and Java Servlet. This framework uses various new techniques such as Aspect-Oriented Programming (AOP), Plain Old Java Object (POJO), and dependency injection (DI), to develop enterprise applications.

RowMapper

This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference. The row mapper interface is used to fetch records from any database using the query() method of JdbcTemplate class by passing the instance of the row mapper. 

Read more on RowMapper here: Spring – RowMapper Interface with Example

ResultSetExtractor

This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference. The ResultSetExtractor is used to fetch records from the database using the query() method of JdbcTemplate class where we need to pass the instance of ResultSetExtractor.

Read more on ResultSetExtractor here: Spring – ResultSetExtractor

Difference Between RowMapper and ResultSetExtractor

RowMapper

ResultSetExtractor

It is used to fetch records from the database using the query() method of JdbcTemplate class by passing the instance of row mapper. It is used to fetch records from the database using the query() method of JdbcTemplate class by passing the instance of ResultSetExtractor.
It allows us to map a row of the relations with the instance of a user-defined class. It accepts a ResultSet and returns the list.
Syntax of query method: public T query(String sql, RowMapper<T> rm)   Syntax of query method-: public T query(String sql,ResultSetExtractor<T> rse)  
It defines only one method mapRow that accepts ResultSet instance and int as the parameter list It defines only one method extractData that accepts ResultSet instance as a parameter
Its objects are typically stateless and thus reusable; they are an ideal choice for implementing row-mapping logic in a single place. Its object is typically stateless and thus reusable, as long as it doesn’t access stateful resources or keep the resulting state within the object.
RowMapper is used to map a single row of a ResultSet to a Java object. ResultSetExtractor is used any complex processing is required i.e. when the processing involves more than just mapping each row to an object.
In the row-mapper interface, SQLExceptions will be caught and handled by the calling JdbcTemplate. In ResultSetExtractor interface implementations, the exception function is not used directly in the ResultSetExtractor function because SQLExceptions are usually caught and handled through a call to JdbcTemplate.
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. It is used to extract the whole ResultSet (possibly multiple rows).

All Known Implementing Classes of Row mapper are following: 

BeanPropertyRowMapper, ColumnMapRowMapper, DataClassRowMapper, MappingSqlQueryWithParameters.RowMapperImpl, SingleColumnRowMapper and UpdatableSqlQuery.RowMapperImpl

All Known Implementing Classes of ResultSetExtractor Interface are following: 

AbstractLobStreamingResultSetExtractor, RowMapperResultSetExtractor and SqlRowSetResultSetExtractor


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads