callback mechanism

Design pattern


Callback mechanism:

the caller passes in the specific implementation class of the specific calling interface, and then the caller calls this interface



interface

public interface RowMapper<T> {

/**
* Implementations must implement this method to map each row of data
* in the ResultSet. This method should not call {@code next()} on
* the ResultSet; it is only supposed to map values ​​of the current row.
* @param rs the ResultSet to map (pre-initialized for the current row)
* @param rowNum the number of the current row
* @return the result object for the current row
* @throws SQLException if a SQLException is encountered getting
* column values ​​(that is, there's no need to catch SQLException)
*/
T mapRow(ResultSet rs, int rowNum) throws SQLException;

}


调用者
public class RowMapperResultSetExtractor<T> implements ResultSetExtractor<List<T>> {

private final RowMapper<T> rowMapper;

private final int rowsExpected;


/**
* Create a new RowMapperResultSetExtractor.
* @param rowMapper the RowMapper which creates an object for each row
*/
public RowMapperResultSetExtractor(RowMapper<T> rowMapper) {
this(rowMapper, 0);
}

/**
* Create a new RowMapperResultSetExtractor.
* @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 RowMapperResultSetExtractor(RowMapper<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 = (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; //Through the construction method or the set method////The specific type when used, so that the caller can use the public Use the callback function class: }
}





new RowMapperResultSetExtractor<T>(rowMapper)

List<TbSettFund> tb= jdbcTemplate.query("select * from tb_sett_fund t where t.sup_acct_id=?", new Object[]{str},new RowMapper(){

@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
// TODO Auto-generated method stub
TbSettFund tb= new TbSettFund();
tb.setSupAcctId(rs.getString("SUP_ACCT_ID"));
tb.setCustFlag(rs.getInt("CUST_FLAG"));
return tb;
}

});




public <T> List<T> query(String sql, Object[] args, RowMapper<T> rowMapper) throws DataAccessException {
return query(sql, args, new RowMapperResultSetExtractor<T>(rowMapper)); Reference
}






http://www.cnblogs.com/lcngu/p/5111066.html

==================================================================================================



调用者:

public <T> T query(
PreparedStatementCreator psc, final PreparedStatementSetter pss, final ResultSetExtractor<T> rse)
throws DataAccessException {

Assert.notNull(rse, "ResultSetExtractor must not be null");
logger.debug("Executing prepared SQL query");

return execute(psc, new PreparedStatementCallback<T>() {
@Override
public T doInPreparedStatement(PreparedStatement ps) throws SQLException {
ResultSet rs = null;
try {
if (pss != null) {
pss.setValues(ps);
}
rs = ps.executeQuery();
ResultSet rsToUse = rs;
if (nativeJdbcExtractor != null) {
rsToUse = nativeJdbcExtractor.getNativeResultSet(rs);
}
return rse.extractData(rsToUse);
}
finally {
JdbcUtils.closeResultSet(rs);
if (pss instanceof ParameterDisposer) {
((ParameterDisposer) pss).cleanupParameters();
}
}
}
});
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326243131&siteId=291194637