公认的基于 的解决方案ResultSetExtractor
可以变得更加模块化和可重用:在我的应用程序中,我创建了一个CollectingRowMapper
接口和一个抽象实现。请参阅下面的代码,它包含 Javadoc 注释。
CollectingRowMapper 接口:
import org.springframework.jdbc.core.RowMapper;
/**
* A RowMapper that collects data from more than one row to generate one result object.
* This means that, unlike normal RowMapper, a CollectingRowMapper will call
* <code>next()</code> on the given ResultSet until it finds a row that is not related
* to previous ones. Rows <b>must be sorted</b> so that related rows are adjacent.
* Tipically the T object will contain some single-value property (an id common
* to all collected rows) and a Collection property.
* <p/>
* NOTE. Implementations will be stateful (to save the result of the last call
* to <code>ResultSet.next()</code>), so <b>they cannot have singleton scope</b>.
*
* @see AbstractCollectingRowMapper
*
* @author Pino Navato
**/
public interface CollectingRowMapper<T> extends RowMapper<T> {
/**
* Returns the same result of the last call to <code>ResultSet.next()</code> made by <code>RowMapper.mapRow(ResultSet, int)</code>.
* If <code>next()</code> has not been called yet, the result is meaningless.
**/
public boolean hasNext();
}
抽象实现类:
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Basic implementation of {@link CollectingRowMapper}.
*
* @author Pino Navato
**/
public abstract class AbstractCollectingRowMapper<T> implements CollectingRowMapper<T> {
private boolean lastNextResult;
@Override
public T mapRow(ResultSet rs, int rowNum) throws SQLException {
T result = mapRow(rs, null, rowNum);
while (nextRow(rs) && isRelated(rs, result)) {
result = mapRow(rs, result, ++rowNum);
}
return result;
}
/**
* Collects the current row into the given partial result.
* On the first call partialResult will be null, so this method must create
* an instance of T and map the row on it, on subsequent calls this method updates
* the previous partial result with data from the new row.
*
* @return The newly created (on the first call) or modified (on subsequent calls) partialResult.
**/
protected abstract T mapRow(ResultSet rs, T partialResult, int rowNum) throws SQLException;
/**
* Analyzes the current row to decide if it is related to previous ones.
* Tipically it will compare some id on the current row with the one stored in the partialResult.
**/
protected abstract boolean isRelated(ResultSet rs, T partialResult) throws SQLException;
@Override
public boolean hasNext() {
return lastNextResult;
}
protected boolean nextRow(ResultSet rs) throws SQLException {
lastNextResult = rs.next();
return lastNextResult;
}
}
ResultSetExtractor 实现:
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.util.Assert;
/**
* A ResultSetExtractor that uses a CollectingRowMapper.
* This class has been derived from the source code of Spring's RowMapperResultSetExtractor.
*
* @author Pino Navato
**/
public class CollectingRowMapperResultSetExtractor<T> implements ResultSetExtractor<List<T>> {
private final CollectingRowMapper<T> rowMapper;
private final int rowsExpected;
/**
* Create a new CollectingRowMapperResultSetExtractor.
* @param rowMapper the RowMapper which creates an object for each row
**/
public CollectingRowMapperResultSetExtractor(CollectingRowMapper<T> rowMapper) {
this(rowMapper, 0);
}
/**
* Create a new CollectingRowMapperResultSetExtractor.
* @param rowMapper the RowMapper which creates an object for each row
* @param rowsExpected the number of expected rows (just used for optimized collection handling)
**/
public CollectingRowMapperResultSetExtractor(CollectingRowMapper<T> rowMapper, int rowsExpected) {
Assert.notNull(rowMapper, "RowMapper is required");
this.rowMapper = rowMapper;
this.rowsExpected = rowsExpected;
}
@Override
public List<T> extractData(ResultSet rs) throws SQLException {
List<T> results = (rowsExpected > 0 ? new ArrayList<>(rowsExpected) : new ArrayList<>());
int rowNum = 0;
if (rs.next()) {
do {
results.add(rowMapper.mapRow(rs, rowNum++));
} while (rowMapper.hasNext());
}
return results;
}
}
上面的所有代码都可以作为库重用。您只需子类AbstractCollectingRowMapper
化并实现这两个抽象方法。
使用示例:
给定如下查询:
SELECT * FROM INVOICE inv
JOIN INVOICELINES lines
on inv.INVID = lines.INVOICE_ID
order by inv.INVID
您可以只为两个连接的表编写一个映射器:
public class InvoiceRowMapper extends AbstractCollectingRowMapper<Invoice> {
@Override
protected Invoice mapRow(ResultSet rs, Invoice partialResult, int rowNum) throws SQLException {
if (partialResult == null) {
partialResult = new Invoice();
partialResult.setInvId(rs.getBigDecimal("INVID"));
partialResult.setInvDate(rs.getDate("INVDATE"));
partialResult.setLines(new ArrayList<>());
}
InvoiceLine line = new InvoiceLine();
line.setOrder(rs.getInt("ORDER"));
line.setPrice(rs.getBigDecimal("PRICE"));
line.setQuantity(rs.getBigDecimal("QUANTITY"));
partialResult.getLines().add(line);
return partialResult;
}
/** Returns true if the current record has the same invoice ID of the previous ones. **/
@Override
protected boolean isRelated(ResultSet rs, Invoice partialResult) throws SQLException {
return partialResult.getInvId().equals(rs.getBigDecimal("INVID"));
}
}
最后说明:我主要使用 Spring Batch,在一个自定义子类中CollectingRowMapper
:我在另一个答案中描述了这个解决方案。使用 Spring Batch,您可以在获取下一组相关行之前处理每组相关行,因此您可以避免加载可能很大的整个查询结果。AbstractCollectingRowMapper
JdbcCursorItemReader