Choosing an Approach for JDBC Database Access
你可以从多种方法中选择,为 JDBC 数据库访问形成基础。除了三种 JdbcTemplate
类型之外,SimpleJdbcInsert
和 SimpleJdbcCall
方法优化了数据库元数据,而 RDBMS 对象样式产生了更面向对象的方法。一旦你开始使用其中一种方法,你仍然可以混合匹配,来包含不同方法中的某个特性。
You can choose among several approaches to form the basis for your JDBC database access.
In addition to three flavors of JdbcTemplate
, a SimpleJdbcInsert
and SimpleJdbcCall
approach optimizes database metadata, and the RDBMS Object style results in a more
object-oriented approach. Once you start using one of these approaches, you can still mix
and match to include a feature from a different approach.
-
JdbcTemplate
is the classic and most popular Spring JDBC approach. This “lowest-level” approach and all others use aJdbcTemplate
under the covers. -
NamedParameterJdbcTemplate
wraps aJdbcTemplate
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. -
SimpleJdbcInsert
andSimpleJdbcCall
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 a map of parameters matching the column names. This works only if the database provides adequate metadata. If the database does not provide this metadata, you have to provide explicit configuration of the parameters. -
RDBMS objects — including
MappingSqlQuery
,SqlUpdate
, andStoredProcedure
— require you to create reusable and thread-safe objects during initialization of your data-access layer. This approach allows you to define your query string, declare parameters, and compile the query. Once you do that,execute(…)
,update(…)
, andfindObject(…)
methods can be called multiple times with various parameter values.