📜  Spring – RowMapper 和 ResultSetExtractor 的区别

📅  最后修改于: 2022-05-13 01:55:51.858000             🧑  作者: Mango

Spring – RowMapper 和 ResultSetExtractor 的区别

Spring 是最流行的Java EE 框架之一。它是一个开源轻量级框架,允许Java EE 7 开发人员构建简单、可靠且可扩展的企业应用程序。该框架主要侧重于提供各种方法来帮助您管理业务对象。与Java数据库连接 (JDBC)、JavaServer Pages (JSP) 和Java Servlet 等经典Java框架和应用程序编程接口 (API) 相比,它使 Web 应用程序的开发更加容易。该框架使用各种新技术,如面向方面编程 (AOP)、普通Java对象 (POJO) 和依赖注入 (DI) 来开发企业应用程序。

行映射器

这是一个函数式接口,因此可以用作 lambda 表达式或方法引用的赋值目标。行映射器接口用于通过传递行映射器的实例,使用 JdbcTemplate 类的 query() 方法从任何数据库中获取记录。

结果集提取器

这是一个函数式接口,因此可以用作 lambda 表达式或方法引用的赋值目标。 ResultSetExtractor用于使用 J dbcTemplate 类query()方法从数据库中获取记录,其中我们需要传递 ResultSetExtractor 的实例。

RowMapper 和 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 rm)  Syntax of query method-: public T query(String sql,ResultSetExtractor rse)  
It defines only one method mapRow that accepts ResultSet instance and int as the parameter listIt 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