Spring – Difference Between RowMapper and ResultSetExtractor
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 than 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.
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
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
|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.|
|It is usually a simpler choice for ResultSet processing,||Implementations of Result Extractor interface perform the work of extracting results from a ResultSet.|
|It saves a lot of code because it internally adds the data of ResultSet into the collection.||In the result Extractor interface, we don’t need to worry about exception handling because becauseSQLExceptions will be caught and handled by calling JdbcTemplate.|
|In the row-mapper interface, SQLExceptions will be caught and handled by the calling JdbcTemplate.||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
Please Login to comment...