登录

  • 登录
  • 忘记密码?点击找回

注册

  • 获取手机验证码 60
  • 注册

找回密码

  • 获取手机验证码60
  • 找回
毕业论文网 > 外文翻译 > 计算机类 > 计算机科学与技术 > 正文

Spring Framework JDBC简介外文翻译资料

 2021-12-18 11:12  

3. Data Access with JDBC

The value provided by the Spring Framework JDBC abstraction is perhaps best shown by the sequence of actions outlined in the following table below. The table shows which actions Spring takes care of and which actions are your responsibility.

Table 4. 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, the statement, and the resultset.

X

The Spring Framework takes care of all the low-level details that can make JDBC such a tedious API.

3.1. Choosing an Approach for JDBC Database Access

You can choose among several approaches to form the basis for your JDBC database access. In addition to three flavors of JdbcTemplate, a new SimpleJdbcInsert and SimpleJdbcCall approach optimizes database metadata, and the RDBMS Object style takes a more object-oriented approach similar to that of JDO Query design. Once you start using one of these approaches, you can still mix and match to include a feature from a different approach. All approaches require a JDBC 2.0-compliant driver, and some advanced features require a JDBC 3.0 driver.

  • JdbcTemplate is the classic and most popular Spring JDBC approach. This “lowest-level” approach and all others use a JdbcTemplate under the covers.
  • 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.
  • SimpleJdbcInsert and SimpleJdbcCall optimize database metadata to limit the amount of necessary configuration. This approach simplifies coding so that you need to provide only the name of the table or procedure and provide 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 and StoredProcedure, require you to create reusable and thread-safe objects during initialization of your data-access layer. This approach is modeled after JDO Query, wherein you define your query string, declare parameters, and compile the query. Once you do that, execute methods can be called multiple times with various parameter values.

3.2. Package Hierarchy

The Spring Frameworkrsquo;s JDBC abstraction framework consists of four different packages:

  • core: The org.springframework.jdbc.core package contains the JdbcTemplate class and its various callback interfaces, plus a variety of related classes. A subpackage named org.springframework.jdbc.core.simple contains the SimpleJdbcInsertand SimpleJdbcCall classes. Another subpackage named org.springframework.jdbc.core.namedparam contains the NamedParameterJdbcTemplate class and the related support classes. See Using the JDBC Core Classes to Control Basic JDBC Processing and Error Handling, JDBC Batch Operations, and Simplifying JDBC Operations with the SimpleJdbc Classes.
  • datasource: The org.springframework.jdbc.datasource package contains a utility class for easy DataSource access and various simple DataSource implementations that you can use for testing and running unmodified JDBC code outside of a Java EE container. A subpackage named org.springfamework.jdbc.datasource.embedded provides support for creating embedded databases by using Java database engines, such as HSQL, H2, and Derby. See Controlling Database Connectionsand Embedded Database Support.
  • object: The org.springframework.jdbc.object package contains classes that represent RDBMS queries, updates, and stored procedures as thread-safe, reusable objects. See Modeling JDBC Operations as Java Objects. This approach is modeled by JDO, although objects returned by queries are naturally disconnected from the database. This higher-level of JDBC abstraction depends on the lower-level abstraction in the org.springframework.jdbc.core package.
  • support: The org.springframework.jdbc.support package provides SQLException translation functionality and some utility classes. Exceptions thrown during JDBC processing are translated to exceptions defined in the org.springframework.daopackage. This means th

    3.使用JDBC进行数据访问

    3.1。Spring Framework JDBC简介

    Spring Framework JDBC抽象提供的增值可能最好通过下表中列出的操作顺序显示出来。该表显示了Spring将采取哪些操作以及哪些操作是您(应用程序开发人员)的责任。

    表4. Spring JDBC - 谁做了什么?

    行动

    弹簧

    定义连接参数。

    X

    打开连接。

    X

    指定SQL语句。

    X

    声明参数并提供参数值

    X

    准备并执行声明。

    X

    设置循环以迭代结果(如果有)。

    X

    为每次迭代完成工作。

    X

    处理任何异常。

    X

    处理交易。

    X

    关闭连接,语句和结果集。

    X

    Spring Framework负责处理所有可以使JDBC成为一个繁琐的API的低级细节。

    3.1.1。选择JDBC数据库访问方法

    您可以选择多种方法来构成JDBC数据库访问的基础。除了三种类型的JdbcTemplate之外,新的SimpleJdbcInsert和SimplejdbcCall方法优化了数据库元数据,而RDBMS Object样式采用了类似于JDO Query设计的面向对象方法。一旦开始使用这些方法之一,您仍然可以混合和匹配以包含来自不同方法的功能。所有方法都需要JDBC 2.0兼容的驱动程序,而某些高级功能需要JDBC 3.0驱动程序。

    • JdbcTemplate是经典的Spring JDBC方法,也是最受欢迎的方法。这种“最低级别”方法和所有其他方法都使用了JdbcTemplate。
    • NamedParameterJdbcTemplate包装a JdbcTemplate来提供命名参数而不是传统的JDBC“?” 占位符。当您有多个SQL语句参数时,此方法可提供更好的文档和易用性。
    • SimpleJdbcInsert和SimpleJdbcCall优化数据库元数据以限制必要的配置量。此方法简化了编码,因此您只需提供表或过程的名称,并提供与列名匹配的参数映射。这仅在数据库提供足够的元数据时有效。如果数据库未提供此元数据,则必须提供参数的显式配置。
    • RDBMS对象(包括MappingSqlQuery,SqlUpdate和StoredProcedure)要求您在数据访问层初始化期间创建可重用且线程安全的对象。此方法在JDO Query之后建模,其中您定义查询字符串,声明参数和编译查询。执行此操作后,可以多次调用execute方法,并传入各种参数值。

    3.1.2。包层次结构

    Spring框架的JDBC抽象框架由四个不同的包,即core,datasource,object,和support。

    该org.springframework.jdbc.core包包含JdbcTemplate类及其各种回调接口,以及各种相关类。名为subpackage的org.springframework.jdbc.core.simple包含SimpleJdbcInsert和 SimpleJdbcCall类。另一个名为subpackage的包org.springframework.jdbc.core.namedparam包含NamedParameterJdbcTemplate 类和相关的支持类。请参阅使用JDBC核心类来控制基本JDBC处理和错误处理JDBC批处理操作以及 使用SimpleJdbc类简化JDBC操作

    该org.springframework.jdbc.datasource软件包包含一个易于DataSource访问的实用程序类 ,以及DataSource可用于在Java EE容器外测试和运行未修改的JDBC代码的各种简单实现。名为subpackage的程序包括org.springfamework.jdbc.datasource.embedded使用Java数据库引擎(如HSQL,H2和Derby)创建嵌入式数据库的支持。请参阅 控制数据库连接嵌入式数据库支持

    该org.springframework.jdbc.object包包含将RDBMS查询,更新和存储过程表示为线程安全,可重用对象的类。请参阅将 JDBC操作建模为Java对象。这种方法由JDO建模,尽管查询返回的对象自然数据库断开连接。这种更高级别的JDBC抽象取决于包中的低级抽象org.springframework.jdbc.core。

    该org.springframework.jdbc.support包提供SQLException翻译功能和一些实用程序类。JDBC处理期间抛出的异常将转换为org.springframework.dao包中定义的异常。这意味着使用Spring JDBC抽象层的代码不需要实现JDBC或RDBMS特定的错误处理。所有已翻译的异常都是未选中的,这使您可以选择捕获可以恢复的异常,同时允许将其他异常传播给调用方。请参见SQLExceptionTranslator

    3.2。使用JDBC核心类来控制基本的JDBC处理和错误处理

    3.2.1。的JdbcTemplate

    的JdbcTemplate类是在JDBC核心包的核心类。它处理资源的创建和释放,帮助您避免常见错误,例如忘记关闭连接。它执行核心JDBC工作流的基本任务,例如语句创建和执行,使应用程序代码提供SQL并提取结果。在JdbcTemplate完成SQL查询,更新语句和存储过程调用,执行了迭代ResultSetS和返回的参数值的提取。它还捕获JDBC异常并将它们转换为org.springframework.dao包中定义的通用的,信息更丰富的异常层次结构 。

    当您使用JdbcTemplate代码时,您只需要实现回调接口,为它们提供明确定义的合同。该PreparedStatementCreator 回调接口创建给予了一份准备好的声明Connection该类提供,SQL和任何必要的参数。对于CallableStatementCreator创建可调用语句的接口也是如此。所述 RowCallbackHandler接口从一个每一行中提取的值ResultSet。

    的JdbcTemplate可以在DAO实现内通过直接实例化与使用DataSource参考,或在一个Spring IoC容器被配置和给予的DAO作为豆参考。

    该DataSource应始终被配置为Spring IoC容器的bean。在第一种情况下,

    bean直接提供给服务; 在第二种情况下,它被给予准备好的模板。

    此类发出的所有SQL都记录在与DEBUG模板实例的完全限定类名对应的类别下的级别(通常 JdbcTemplate,但如果您使用JdbcTemplate该类的自定义子类,则可能会有所不同 )。

    JdbcTemplate类用法的示例

    本节提供了一些JdbcTemplate类使用示例。这些示例并非详尽列出了所有功能JdbcTemplate; 看到服务员javadocs。

    查询(SELECT)

    这是一个简单的查询,用于获取关系中的行数:

    int rowCount = this.jdbcTemplate.queryForObject('select count(*) from t_actor', Integer.class);

    使用绑定变量的简单查询:

    int countOfActorsNamedJoe = this.jdbcTemplate.queryForObject(

    'select count(*) from t_actor where first_name = ?', Integer.class, 'Joe');

    查询String:

    String lastName = this.jdbcTemplate.queryForObject(

    'select last_name from t_actor where id = ?',

    new Object[]{1212L}, String.class);

    查询和填充单个域对象:

    Actor actor = this.jdbcTemplate.queryForObject(

    'select first_name, last_name from t_actor where id = ?',

    new Object[]{1212L},

    new RowMapperlt;Actorgt;() {

    public Actor mapRow(ResultSet rs, int rowNum) throws SQLException {

    Actor actor = new Actor();

    actor.setFirstName(rs.getString('first_name'));

    actor.setLastName(rs.getString('last_name'));

    ret

    资料编号:[4505]

您需要先支付 30元 才能查看全部内容!立即支付

企业微信

Copyright © 2010-2022 毕业论文网 站点地图