日期:2014-05-16 浏览次数:20656 次
jdbcTemplate整理
为了实现基本的CRUD操作,spring给我们提供了jdbcTemplate这个模板类.实现最常用的CRUD操作。
先看jdbcTemplate的定义
?
public class JdbcTemplate extends JdbcAccessor implements JdbcOperations { /** Custom NativeJdbcExtractor */ private NativeJdbcExtractor nativeJdbcExtractor; /** If this variable is false, we will throw exceptions on SQL warnings */ private boolean ignoreWarnings = true; /** * If this variable is set to a non-zero value, it will be used for setting the * fetchSize property on statements used for query processing. */ private int fetchSize = 0; /** * If this variable is set to a non-zero value, it will be used for setting the * maxRows property on statements used for query processing. */ private int maxRows = 0; /** * If this variable is set to true then all results checking will be bypassed for any * callable statement processing. This can be used to avoid a bug in some older Oracle * JDBC drivers like 10.1.0.2. */ private boolean skipResultsProcessing = false; }
?
?
JdbcTemplate继承自JdbcAccessor,同时实现了JdbcOperations接口,在整个框架中这个JdbcOperations接口只让
JdbcTemplate实现了。JdbcOperations定义了CRUD的操作。
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[], ResultS