日期:2014-05-16  浏览次数:20359 次

Spring JDBC笔记

Table?12.1.?Spring JDBC - who does what?

Action Spring You
Define connection parameters. X
Open the connection. X ?
Specify the SQL statement. ? X
Declare parameters and provide parameter values ? X
Prepare and execute the statement. X ?
Set up the loop to iterate through the results (if any). X ?
Do the work for each iteration. ? X
Process any exception. X ?
Handle transactions. X ?
Close the connection, statement and resultset. X ?


  • JdbcTemplate is the classic Spring JDBC approach and the most popular. This "lowest level" approach and all others use a JdbcTemplate under the covers, and all are updated with Java 5 support such as generics and varargs.

  • NamedParameterJdbcTemplate wraps a JdbcTemplate to provide named parameters instead of the traditional JDBC "?" placeholders. This approach provides better documentation and ease of use when you have multiple parameters for an SQL statement.

  • SimpleJdbcTemplate combines the most frequently used operations of JdbcTemplate and NamedParameterJdbcTemplate.

  • SimpleJdbcInsert and SimpleJdbcCall optimize database metadata to limit the amount of necessary configuration. This approach simplifies coding so that you only need to provide the name of the table or procedure and provide a map of parameters matching the column names. This only works if the database provides adequate metadata. If the database doesn't provide this metadata, you will have to provide explicit configuration of the parameters.

  • RDBMS Objects including MappingSqlQuery, SqlUpdate and StoredProcedure requires you to create reusable and thread-saf