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
是经典且最流行的 Spring JDBC 方法。这种 “lowest-level” 方法和其他所有方法在幕后都使用JdbcTemplate
。 -
JdbcTemplate
is the classic and most popular Spring JDBC approach. This “lowest-level” approach and all others use aJdbcTemplate
under the covers. -
NamedParameterJdbcTemplate
封装JdbcTemplate
,以提供命名参数,而不是传统的 JDBC?
占位符。当 SQL 语句有多个参数时,此方法提供了更好的文档和易用性。 -
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
和SimpleJdbcCall
优化数据库元数据,以限制必要的配置量。此方法简化了编码,以便您只需要提供表或过程名称以及与列名称匹配的参数映射即可。这仅在数据库提供充分元数据的情况下有效。如果数据库未提供此元数据,则您必须提供参数的显式配置。 -
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 对象(包括
MappingSqlQuery
、SqlUpdate
和StoredProcedure
)要求您在数据访问层的初始化过程中创建可重复使用且线程安全的对象。此方法使您能够定义查询字符串、声明参数和编译查询。完成此操作后,可以多次使用各种参数值调用execute(…​)
、update(…​)
和findObject(…​)
方法。 -
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.