📄 jdbcoperations.java
字号:
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jdbc.core;
import java.util.List;
import java.util.Map;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.jdbc.support.rowset.SqlRowSet;
/**
* Interface specifying a basic set of JDBC operations.
* Implemented by {@link JdbcTemplate}. Not often used directly, but a useful
* option to enhance testability, as it can easily be mocked or stubbed.
*
* <p>Alternatively, the standard JDBC infrastructure can be mocked.
* However, mocking this interface constitutes significantly less work.
* As an alternative to a mock objects approach to testing data access code,
* consider the powerful integration testing support provided in the
* <code>org.springframework.test</code> package, shipped in
* <code>spring-mock.jar</code>.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @see JdbcTemplate
*/
public interface JdbcOperations {
//-------------------------------------------------------------------------
// Methods dealing with a plain java.sql.Connection
//-------------------------------------------------------------------------
/**
* Execute a JDBC data access operation, implemented as callback action
* working on a JDBC Connection. This allows for implementing arbitrary
* data access operations, within Spring's managed JDBC environment:
* that is, participating in Spring-managed transactions and converting
* JDBC SQLExceptions into Spring's DataAccessException hierarchy.
* <p>The callback action can return a result object, for example a
* domain object or a collection of domain objects.
* @param action the callback object that specifies the action
* @return a result object returned by the action, or <code>null</code>
* @throws DataAccessException if there is any problem
*/
Object execute(ConnectionCallback action) throws DataAccessException;
//-------------------------------------------------------------------------
// Methods dealing with static SQL (java.sql.Statement)
//-------------------------------------------------------------------------
/**
* Execute a JDBC data access operation, implemented as callback action
* working on a JDBC Statement. This allows for implementing arbitrary data
* access operations on a single Statement, within Spring's managed JDBC
* environment: that is, participating in Spring-managed transactions and
* converting JDBC SQLExceptions into Spring's DataAccessException hierarchy.
* <p>The callback action can return a result object, for example a
* domain object or a collection of domain objects.
* @param action callback object that specifies the action
* @return a result object returned by the action, or <code>null</code>
* @throws DataAccessException if there is any problem
*/
Object execute(StatementCallback action) throws DataAccessException;
/**
* Issue a single SQL execute, typically a DDL statement.
* @param sql static SQL to execute
* @throws DataAccessException if there is any problem
*/
void execute(String sql) throws DataAccessException;
/**
* Execute a query given static SQL, reading the ResultSet with a
* ResultSetExtractor.
* <p>Uses a JDBC Statement, not a PreparedStatement. If you want to
* execute a static query with a PreparedStatement, use the overloaded
* <code>query</code> method with <code>null</code> as argument array.
* @param sql SQL query to execute
* @param rse object that will extract all rows of results
* @return an arbitrary result object, as returned by the ResultSetExtractor
* @throws DataAccessException if there is any problem executing the query
* @see #query(String, Object[], ResultSetExtractor)
*/
Object query(String sql, ResultSetExtractor rse) throws DataAccessException;
/**
* Execute a query given static SQL, reading the ResultSet on a per-row
* basis with a RowCallbackHandler.
* <p>Uses a JDBC Statement, not a PreparedStatement. If you want to
* execute a static query with a PreparedStatement, use the overloaded
* <code>query</code> method with <code>null</code> as argument array.
* @param sql SQL query to execute
* @param rch object that will extract results, one row at a time
* @throws DataAccessException if there is any problem executing the query
* @see #query(String, Object[], RowCallbackHandler)
*/
void query(String sql, RowCallbackHandler rch) throws DataAccessException;
/**
* Execute a query given static SQL, mapping each row to a Java object
* via a RowMapper.
* <p>Uses a JDBC Statement, not a PreparedStatement. If you want to
* execute a static query with a PreparedStatement, use the overloaded
* <code>query</code> method with <code>null</code> as argument array.
* @param sql SQL query to execute
* @param rowMapper object that will map one object per row
* @return the result List, containing mapped objects
* @throws DataAccessException if there is any problem executing the query
* @see #query(String, Object[], RowMapper)
*/
List query(String sql, RowMapper rowMapper) throws DataAccessException;
/**
* Execute a query given static SQL, mapping a single result row to a Java
* object via a RowMapper.
* <p>Uses a JDBC Statement, not a PreparedStatement. If you want to
* execute a static query with a PreparedStatement, use the overloaded
* <code>queryForObject</code> method with <code>null</code> as argument array.
* @param sql SQL query to execute
* @param rowMapper object that will map one object per row
* @return the single mapped object
* @throws IncorrectResultSizeDataAccessException if the query does not
* return exactly one row
* @throws DataAccessException if there is any problem executing the query
* @see #queryForObject(String, Object[], RowMapper)
*/
Object queryForObject(String sql, RowMapper rowMapper) throws DataAccessException;
/**
* Execute a query for a result object, given static SQL.
* <p>Uses a JDBC Statement, not a PreparedStatement. If you want to
* execute a static query with a PreparedStatement, use the overloaded
* <code>queryForObject</code> method with <code>null</code> as argument array.
* <p>This method is useful for running static SQL with a known outcome.
* The query is expected to be a single row/single column query; the returned
* result will be directly mapped to the corresponding object type.
* @param sql SQL query to execute
* @param requiredType the type that the result object is expected to match
* @return the result object of the required type, or <code>null</code> in case of SQL NULL
* @throws IncorrectResultSizeDataAccessException if the query does not return
* exactly one row, or does not return exactly one column in that row
* @throws DataAccessException if there is any problem executing the query
* @see #queryForObject(String, Object[], Class)
*/
Object queryForObject(String sql, Class requiredType) throws DataAccessException;
/**
* Execute a query for a result Map, given static SQL.
* <p>Uses a JDBC Statement, not a PreparedStatement. If you want to
* execute a static query with a PreparedStatement, use the overloaded
* <code>queryForMap</code> method with <code>null</code> as argument array.
* <p>The query is expected to be a single row query; the result row will be
* mapped to a Map (one entry for each column, using the column name as the key).
* @param sql SQL query to execute
* @return the result Map (one entry for each column, using the
* column name as the key)
* @throws IncorrectResultSizeDataAccessException if the query does not
* return exactly one row
* @throws DataAccessException if there is any problem executing the query
* @see #queryForMap(String, Object[])
* @see ColumnMapRowMapper
*/
Map queryForMap(String sql) throws DataAccessException;
/**
* Execute a query that results in a long value, given static SQL.
* <p>Uses a JDBC Statement, not a PreparedStatement. If you want to
* execute a static query with a PreparedStatement, use the overloaded
* <code>queryForLong</code> method with <code>null</code> as argument array.
* <p>This method is useful for running static SQL with a known outcome.
* The query is expected to be a single row/single column query that results
* in a long value.
* @param sql SQL query to execute
* @return the long value, or 0 in case of SQL NULL
* @throws IncorrectResultSizeDataAccessException if the query does not return
* exactly one row, or does not return exactly one column in that row
* @throws DataAccessException if there is any problem executing the query
* @see #queryForLong(String, Object[])
*/
long queryForLong(String sql) throws DataAccessException;
/**
* Execute a query that results in an int value, given static SQL.
* <p>Uses a JDBC Statement, not a PreparedStatement. If you want to
* execute a static query with a PreparedStatement, use the overloaded
* <code>queryForInt</code> method with <code>null</code> as argument array.
* <p>This method is useful for running static SQL with a known outcome.
* The query is expected to be a single row/single column query that results
* in an int value.
* @param sql SQL query to execute
* @return the int value, or 0 in case of SQL NULL
* @throws IncorrectResultSizeDataAccessException if the query does not return
* exactly one row, or does not return exactly one column in that row
* @throws DataAccessException if there is any problem executing the query
* @see #queryForInt(String, Object[])
*/
int queryForInt(String sql) throws DataAccessException;
/**
* Execute a query for a result list, given static SQL.
* <p>Uses a JDBC Statement, not a PreparedStatement. If you want to
* execute a static query with a PreparedStatement, use the overloaded
* <code>queryForList</code> method with <code>null</code> as argument array.
* <p>The results will be mapped to a List (one entry for each row) of
* result objects, each of them matching the specified element type.
* @param sql SQL query to execute
* @param elementType the required type of element in the result list
* (for example, <code>Integer.class</code>)
* @return a List of objects that match the specified element type
* @throws DataAccessException if there is any problem executing the query
* @see #queryForList(String, Object[], Class)
* @see SingleColumnRowMapper
*/
List queryForList(String sql, Class elementType) throws DataAccessException;
/**
* Execute a query for a result list, given static SQL.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -