PreparedStatementHandler就是調用PreparedStatement來執行SQL語句,這樣在第一次執行sql語句時會進行預編譯,在接下來執行相同的SQL語句時會提高資料庫性能
/**
* @author Clinton Begin
*/
/* 使用PrepareStatement**/
public class PreparedStatementHandler extends BaseStatementHandler {
public PreparedStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
super(executor, mappedStatement, parameter, rowBounds, resultHandler, boundSql);
}
//
public int update(Statement statement) throws SQLException {
//使用PrepareStatement
PreparedStatement ps = (PreparedStatement) statement;
ps.execute();
int rows = ps.getUpdateCount();
Object parameterObject = boundSql.getParameterObject();
KeyGenerator keyGenerator = mappedStatement.getKeyGenerator();
//keyGenerator在執行之後運作
keyGenerator.processAfter(executor, mappedStatement, ps, parameterObject);
return rows;
}
//使用PrepareStatement的批處理
public void batch(Statement statement) throws SQLException {
PreparedStatement ps = (PreparedStatement) statement;
ps.addBatch();
}
//使用PrepareStatement的execute操作
public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
PreparedStatement ps = (PreparedStatement) statement;
ps.execute();
//操作結果在ResultHandler中處理
return resultSetHandler.<E> handleResultSets(ps);
}
//獲得Statement
protected Statement instantiateStatement(Connection connection) throws SQLException {
String sql = boundSql.getSql();
//根據KeyGenerator設定值的傳回key的名稱
if (mappedStatement.getKeyGenerator() instanceof Jdbc3KeyGenerator) {
String[] keyColumnNames = mappedStatement.getKeyColumns();
if (keyColumnNames == null) {
return connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
} else {
return connection.prepareStatement(sql, keyColumnNames);
}
} else if (mappedStatement.getResultSetType() != null) {
return connection.prepareStatement(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY);
} else {
return connection.prepareStatement(sql);
}
}
public void parameterize(Statement statement) throws SQLException {
parameterHandler.setParameters((PreparedStatement) statement);
}
}