spring ResultSetExtractor 与 Rowmapper 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10074025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what is difference between ResultSetExtractor vs Rowmapper?
提问by user1127214
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.
1.行映射器可以按行处理。但是结果集提取器我们可以导航所有行并且返回类型是对象。
Is there any difference other than above?.How the works Rowmapper internal and return type is list?.
除了上述之外还有什么区别吗? Rowmapper 内部和返回类型是如何工作的列表?。
采纳答案by Tomasz Nurkiewicz
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.
该接口主要用于 JDBC 框架本身。RowMapper 通常是 ResultSet 处理的一种更简单的选择,它为每一行映射一个结果对象,而不是为整个 ResultSet 映射一个结果对象。
ResultSetExtractoris suppose to extract the whole ResultSet(possibly multiple rows), while RowMapperis feeded with row at a time.
ResultSetExtractor假设提取整个ResultSet(可能是多行),而RowMapper一次输入一行。
Most the time, ResultSetExtractorwill loop the ResultSetand use RowMapper, snippet example of Spring RowMapperResultSetExtractor:
大多数情况下,ResultSetExtractor将循环ResultSet使用RowMapperSpring 的片段示例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.
注意,所有结果都将被转换,这可能会产生 Out Of Memory 异常。
See also
也可以看看
回答by vimal krishna
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
基本区别在于,使用 ResultsetExtractor 时,您需要自己遍历结果集,例如在 while 循环中。此接口为您提供一次对整个 ResultSet 的处理。接口方法 extractData(ResultSet rs) 的实现将包含该手动迭代代码。 查看 ResultsetExtractor 的一种实现
while some callback handlers like RowCallbackHandler, the interface method processRow(ResultSet rs) loops for you.
虽然有些回调处理程序如 RowCallbackHandler,但接口方法 processRow(ResultSet rs) 会为您循环。
RowMapper can be used both was for mapping each row, or entire rows.
RowMapper 既可以用于映射每一行,也可以用于映射整行。
For entire rows Object (by template method jdbcTemplate.query())
对于整行对象(通过模板方法 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())
对于单个对象(使用模板方法 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.
Row Mapper:当一个ResultSet的每一行都映射到一个域对象时,可以实现为私有内部类。
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.
RowCallbackHandler:当每一行的回调方法没有返回值时,例如将行写入文件、将行转换为 XML、在添加到集合之前过滤行。非常高效,因为 ResultSet 到 Object 的映射没有在这里完成。
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.
ResultSetExtractor:当多行 ResultSet 映射到单个 Object 时。就像在查询中进行复杂连接时,可能需要访问整个 ResultSet 而不是单行 rs 来构建复杂的对象,并且您希望完全控制 ResultSet。就像将从 TABLE1 和 TABLE2 的连接返回的行映射到完全重构的 TABLE 聚合一样。
ParameterizedRowMapperis used to create complex objects
ParameterizedRowMapper用于创建复杂对象
回答by PRAKASH KUMAR BEHERA
RowMapper: To process one record of ResultSet at a time.
RowMapper: 一次处理一条ResultSet记录。
ResultSetExtractor: To process multiple records of ResultSet at a time.
ResultSetExtractor: 一次处理多条ResultSet记录。
回答by MMW
I think one place where a ResultSetExtractor could be advantageous is when you have a result set (like from a call to a stored procedure) and a row mapper, and want to process them like is done under the covers in the jdbcTemplate methods, such as query(String sql, RowMapper rowMapper). In this case you can save yourself from having to manually iterate over the result set by using the ResultSetExtractor instead of just the RowMapper.
我认为 ResultSetExtractor 可能有优势的一个地方是当你有一个结果集(比如来自对存储过程的调用)和一个行映射器,并且想要像在 jdbcTemplate 方法中那样处理它们,例如查询(字符串 sql,RowMapper rowMapper)。在这种情况下,您可以通过使用 ResultSetExtractor 而不仅仅是 RowMapper 来避免手动迭代结果集。
For example:
例如:
RowMapper
行映射器
ResultSet resultSet = cs.executeQuery();
int row = 0;
DateRowMapper dateRowMapper = new DateRowMapper();
List<String> dates = new ArrayList<>();
while (resultSet.next()) {
dates.add(dateRowMapper.mapRow(resultSet, ++row));
}
return dates;
ResultSetExtractor
结果集提取器
ResultSet resultSet = callableStatement.executeQuery();
return new RowMapperResultSetExtractor<>(new DateRowMapper()).extractData(resultSet);

