分别构建三个参数:
PreparedStatementCreator :对PreparedStatement进行创建
PreparedStatementSetter :对PreparedStatement进行赋值
ResultSetExtractor :对ResultSet结果进行处理
1.对PreparedStatement进行创建
[code]private static class SimplePreparedStatementCreator implements PreparedStatementCreator, SqlProvider[/code]
SimplePreparedStatementCreator 包装了用户传来的sql语句。
创建PreparedStatement对象的主要方法:
[code]public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
return con.prepareStatement(this.sql);
}[/code]
2.PreparedStatementSetter 的构建,对PreparedStatement进行赋值
[code]class ArgPreparedStatementSetter implements PreparedStatementSetter, ParameterDisposer[/code]
ArgPreparedStatementSetter里的核心方法:
[code]public void setValues(PreparedStatement ps) throws SQLException {
if (this.args != null) {
for (int i = 0; i < this.args.length; i++) {
Object arg = this.args[i];
if (arg instanceof SqlParameterValue) {
SqlParameterValue paramValue = (SqlParameterValue) arg;
StatementCreatorUtils.setParameterValue(ps, i + 1, paramValue, paramValue.getValue());
}
else {
StatementCreatorUtils.setParameterValue(ps, i + 1, SqlTypeValue.TYPE_UNKNOWN, arg);
}
}
}
}[/code]
3.ResultSetExtractor的构建,对ResultSet结果进行处理:
[code]ParameterizedRowMapper<T> extends RowMapper [/code]
[code]RowMapperResultSetExtractor implements ResultSetExtractor[/code]
RowMapperResultSetExtractor 对RowMapper进行了包装,RowMapperResultSetExtractor 里的核心方法:
[code]public Object extractData(ResultSet rs) throws SQLException {
List results = (this.rowsExpected > 0 ? new ArrayList(this.rowsExpected) : new ArrayList());
int rowNum = 0;
while (rs.next()) {
results.add(this.rowMapper.mapRow(rs, rowNum++));
}
return results;
}[/code]
从extractData方法里可以看出,最后[color=red]回调[/color]的是RowMapper里的方法mapRow,所以我们可以对这个方法进行实现。
例如:
[code]class MyRowMapper implements ParameterizedRowMapper<Child> {
public Child mapRow(ResultSet resultSet, int i) throws SQLException {
ParentManager mgr = (ParentManager) Platform.getInstance().getBean(ParentManager.BEAN_ID);
Child child = new Child();
child.setId(resultSet.getString("id"));
child.setName(resultSet.getString("name"));
child.setContent(resultSet.getString("content"));
child.setParent(mgr.get(resultSet.getLong("parentid")));
return child;
}
}[/code]
三个对象构建出来以后,最后会调用方法:
[code]public Object query(
PreparedStatementCreator psc, final PreparedStatementSetter pss, final ResultSetExtractor rse)[/code]
[code]public Object query(
PreparedStatementCreator psc, final PreparedStatementSetter pss, final ResultSetExtractor rse)
throws DataAccessException {
Assert.notNull(rse, "ResultSetExtractor must not be null");
logger.debug("Executing prepared SQL query");
return execute(psc, new PreparedStatementCallback() {
public Object doInPreparedStatement(PreparedStatement ps) throws SQLException {
ResultSet rs = null;
try {
if (pss != null) {
//对PreparedStatement 的参数进行设置
pss.setValues(ps);
}
rs = ps.executeQuery();
ResultSet rsToUse = rs;
if (nativeJdbcExtractor != null) {
rsToUse = nativeJdbcExtractor.getNativeResultSet(rs);
}
//对ResultSet结果进行处理,最终返回一个List集合对象
return rse.extractData(rsToUse);
}
finally {
JdbcUtils.closeResultSet(rs);
if (pss instanceof ParameterDisposer) {
((ParameterDisposer) pss).cleanupParameters();
}
}
}
});
}[/code]
PreparedStatementCreator :对PreparedStatement进行创建
PreparedStatementSetter :对PreparedStatement进行赋值
ResultSetExtractor :对ResultSet结果进行处理
1.对PreparedStatement进行创建
[code]private static class SimplePreparedStatementCreator implements PreparedStatementCreator, SqlProvider[/code]
SimplePreparedStatementCreator 包装了用户传来的sql语句。
创建PreparedStatement对象的主要方法:
[code]public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
return con.prepareStatement(this.sql);
}[/code]
2.PreparedStatementSetter 的构建,对PreparedStatement进行赋值
[code]class ArgPreparedStatementSetter implements PreparedStatementSetter, ParameterDisposer[/code]
ArgPreparedStatementSetter里的核心方法:
[code]public void setValues(PreparedStatement ps) throws SQLException {
if (this.args != null) {
for (int i = 0; i < this.args.length; i++) {
Object arg = this.args[i];
if (arg instanceof SqlParameterValue) {
SqlParameterValue paramValue = (SqlParameterValue) arg;
StatementCreatorUtils.setParameterValue(ps, i + 1, paramValue, paramValue.getValue());
}
else {
StatementCreatorUtils.setParameterValue(ps, i + 1, SqlTypeValue.TYPE_UNKNOWN, arg);
}
}
}
}[/code]
3.ResultSetExtractor的构建,对ResultSet结果进行处理:
[code]ParameterizedRowMapper<T> extends RowMapper [/code]
[code]RowMapperResultSetExtractor implements ResultSetExtractor[/code]
RowMapperResultSetExtractor 对RowMapper进行了包装,RowMapperResultSetExtractor 里的核心方法:
[code]public Object extractData(ResultSet rs) throws SQLException {
List results = (this.rowsExpected > 0 ? new ArrayList(this.rowsExpected) : new ArrayList());
int rowNum = 0;
while (rs.next()) {
results.add(this.rowMapper.mapRow(rs, rowNum++));
}
return results;
}[/code]
从extractData方法里可以看出,最后[color=red]回调[/color]的是RowMapper里的方法mapRow,所以我们可以对这个方法进行实现。
例如:
[code]class MyRowMapper implements ParameterizedRowMapper<Child> {
public Child mapRow(ResultSet resultSet, int i) throws SQLException {
ParentManager mgr = (ParentManager) Platform.getInstance().getBean(ParentManager.BEAN_ID);
Child child = new Child();
child.setId(resultSet.getString("id"));
child.setName(resultSet.getString("name"));
child.setContent(resultSet.getString("content"));
child.setParent(mgr.get(resultSet.getLong("parentid")));
return child;
}
}[/code]
三个对象构建出来以后,最后会调用方法:
[code]public Object query(
PreparedStatementCreator psc, final PreparedStatementSetter pss, final ResultSetExtractor rse)[/code]
[code]public Object query(
PreparedStatementCreator psc, final PreparedStatementSetter pss, final ResultSetExtractor rse)
throws DataAccessException {
Assert.notNull(rse, "ResultSetExtractor must not be null");
logger.debug("Executing prepared SQL query");
return execute(psc, new PreparedStatementCallback() {
public Object doInPreparedStatement(PreparedStatement ps) throws SQLException {
ResultSet rs = null;
try {
if (pss != null) {
//对PreparedStatement 的参数进行设置
pss.setValues(ps);
}
rs = ps.executeQuery();
ResultSet rsToUse = rs;
if (nativeJdbcExtractor != null) {
rsToUse = nativeJdbcExtractor.getNativeResultSet(rs);
}
//对ResultSet结果进行处理,最终返回一个List集合对象
return rse.extractData(rsToUse);
}
finally {
JdbcUtils.closeResultSet(rs);
if (pss instanceof ParameterDisposer) {
((ParameterDisposer) pss).cleanupParameters();
}
}
}
});
}[/code]