spring jdbc源码分析

分别构建三个参数:

PreparedStatementCreator :对PreparedStatement进行创建

PreparedStatementSetter :对PreparedStatement进行赋值

ResultSetExtractor :对ResultSet结果进行处理

1.对PreparedStatement进行创建

private static class SimplePreparedStatementCreator implements PreparedStatementCreator, SqlProvider

SimplePreparedStatementCreator 包装了用户传来的sql语句。

创建PreparedStatement对象的主要方法:

public PreparedStatement createPreparedStatement(Connection con) throws SQLException { 

return con.prepareStatement(this.sql);
}

2.PreparedStatementSetter 的构建,对PreparedStatement进行赋值

class ArgPreparedStatementSetter implements PreparedStatementSetter, ParameterDisposer

ArgPreparedStatementSetter里的核心方法:

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);
}
}
}
}

3.ResultSetExtractor的构建,对ResultSet结果进行处理:

ParameterizedRowMapper<T> extends RowMapper 
RowMapperResultSetExtractor implements ResultSetExtractor

RowMapperResultSetExtractor 对RowMapper进行了包装,RowMapperResultSetExtractor 里的核心方法:

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;
}

从extractData方法里可以看出,最后[color=red]回调[/color]的是RowMapper里的方法mapRow,所以我们可以对这个方法进行实现。

例如:

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;
}
}

三个对象构建出来以后,最后会调用方法:

public Object query( 

PreparedStatementCreator psc, final PreparedStatementSetter pss, final ResultSetExtractor rse)
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();
}
}
}
});
}
    原文作者:Spring MVC
    原文地址: https://blog.csdn.net/ttitfly/article/details/83202860
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞