偶然发现在java方法中可以定义类

JdbcTemplate的源码

 

 

注意QueryStatementCallback 是一个类。

public Object query(final String sql, final ResultSetExtractor rse) throws DataAccessException {
        if (sql == null) {
            throw new InvalidDataAccessApiUsageException("SQL must not be null");
        }
        if (JdbcUtils.countParameterPlaceholders(sql, '?', "'\"") > 0) {
            throw new InvalidDataAccessApiUsageException(
                    "Cannot execute [" + sql + "] as a static query: it contains bind variables");
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Executing SQL query [" + sql + "]");
        }
        class QueryStatementCallback implements StatementCallback, SqlProvider {
            public Object doInStatement(Statement stmt) throws SQLException {
                ResultSet rs = null;
                try {
                    rs = stmt.executeQuery(sql);
                    ResultSet rsToUse = rs;
                    if (nativeJdbcExtractor != null) {
                        rsToUse = nativeJdbcExtractor.getNativeResultSet(rs);
                    }
                    return rse.extractData(rsToUse);
                }
                finally {
                    JdbcUtils.closeResultSet(rs);
                }
            }
            public String getSql() {
                return sql;
            }
        }
        return execute(new QueryStatementCallback());
    }

 

posted on 2021-05-04 09:49  坚守梦想  阅读(248)  评论(0编辑  收藏  举报