计算机专业文献翻译.doc_第1页
计算机专业文献翻译.doc_第2页
计算机专业文献翻译.doc_第3页
计算机专业文献翻译.doc_第4页
计算机专业文献翻译.doc_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

单位代码 学号 070112003 分 类 号 密 级 文献翻译iBATIS SQL Maps指导院(系)名称信息工程学院专 业 名 称软件工程学 生 姓 名指 导 教 师2011 年 3 月 2 日英文译文iBATIS SQL Maps指导Clinton Begin 简介这个简短的教程将带领你通过一个典型的SQL Map演练,教程中的每一个主题的细节部分您都可以在官方网站中的SQL Map开发者指南中找到。准备使用SQL Map SQL Map 架构能应用于设计不好的数据库模型甚至是设计不好的对象模型。尽管如此,您在设计数据库模型和对象模型时,还是应该遵循最佳的设计原则。这样,您会获得更好的性能和更简洁清晰的设计方案。 设计最容易开始的地方是分析应用的业务逻辑。分析什么是应用的业务对象,什么是数据模型以及两者之间的关系。作为快速入门第一个例子,我们使用一个简单的 JavaBean Person 类。 Person.java package examples.domain; /imports implied. public class Person private int id; private String firstName; private String lastName; private Date birthDate; private double weightInKilograms; private double heightInMeters; public int getId () return id; public void setId (int id) this.id = id; /lets assume we have the other getters and setters to save space 如何将Person 类映射成数据表呢?SQL Map 对Java Bean 和数据表之间的关系没有限制,如一个数据表映射成一个Java Bean,或多个表映射成一个Java Bean,或多个Java Bean 映射成一个数据表等。因为使用SQL Map 您可以充分发挥SQL 语句的全部潜力而很少限制。下面这个例子,我们使用一个简单的表,让Java Bean 和表是一对一的关系。 Person.sql CREATE TABLE PERSON( PER_ID NUMBER(5, 0) NOT NULL, PER_FIRST_NAME VARCHAR(40) NOT NULL, PER_LAST_NAME VARCHAR(40) NOT NULL, PER_BIRTH_DATE DATETIME , PER_WEIGHT_KG NUMBER(4, 2) NOT NULL, PER_HEIGHT_M NUMBER(4, 2) NOT NULL, PRIMARY KEY (PER_ID) ) SQL Map 的配置文件 一旦我们准备好了学习所需要的类和表,那么我们就从SQLMap的配置文件开始吧,配置文件是SQL MAP的配置信息统一设置的地方。 SQL Map 配置文件是XML 文件,我们可以它设置各种属性,JDBC DataSource 和SQL Map。在配置文件中,可以方便地统一配置DataSource 不同的实现。SQL Map框架包括 DataSource的iBATIS实现:SimpleDataSource 类、Jakarta DBCP (Commons)和可通过JNDI 上下文查找的DataSource(即应用服务器中的DataSource)。详细的使用方法在以后的章节讨论。在本例中,我们使用Jakarta DBCP。对于上面的例子,配置非常简单,如下所示: SqlMapConfigExample.xml SqlMapConf igEperties driver=oracle.jdbc.driver.OracleDriver url=jdbc :oracle:thin:localhost:1521:oracle1 username=jsmith password=test SQL Map 的映射文件 现在DataSource 已经配置好了,并且有了统一的SQL Map 配置文件,我们还需要SQL Map 的映射文件。映射文件包括SQL 语句和参数对象和结果对象的映射。继续上面的例子,我们从一个简单的查询语句开始,为Person 类和PERSON 表之间创建一个SQL Map 映射文件。 Person.xml SELECT PER_ID as id, PER_FIRST_NAME as firstName, PER_LAST_NAME as lastName, PER_BIRTH_DATE as birthDate, PER_WEIGHT_KG as weightInKilograms, PER_HEIGHT_M as heightInMeters FROM PERSON WHERE PER_ID = #value# 上面的例子是SQL Map 最简单的形式。它使用了SQL Map 框架中一个特性,根据匹配的名字将ResultSet 的列映射成Java Bean 的属性(或Map 的key 值)。#value#符号是输入参数,该符号表示使用了基本类型的包装类作为输入参数(即Integer,但不仅限于此类型)。以上的方法虽然很简单,但有一些限制,无法指定输出字段的数据类型,无法自动地在结果对象中载入相关的信息(即JavaBean无法使用复杂的属性);以上的方法对性能还有轻微的不利影响,因为需要读取ResultSetMetaData 的信息。使用resultMap,可以克服以上的不足,但现在只需要一个简单的例子,以后我们再转向其他不同的方法(无须修改Java 代码)。大多数的应用不仅需要从数据库中读取数据,还需要修改数据。我们已有了一个 SELECT 查询语句的mapped statement 简单例子,下面看看INSERT,UPDATE 和DELETE的mapped statement 什么样子。幸运的是,它们其实没什么区别。接下来,我们完成Person SQL Map 其他部分,以实现修改数据的功能。 Person.xml SELECT PER_ID as id, PER_FIRST_NAME as firstName, PER_LAST_NAME as lastName, PER_BIRTH_DATE as birthDate, PER_WEIGHT_KG as weightInKilograms, PER_HEIGHT_M as heightInMeters FROM PERSON WHERE PER_ID = #value# INSERT INTO PERSON (PER_ID, PER_FIRST_NAME, PER_LAST_NAME, PER_BIRTH_DATE,PER_WEIGHT_KG, PER_HEIGHT_M) VALUES(#id#, #firstName#, #lastName#, #birthDate#, #weightInKilograms#, #heightInMeters#) UPDATE PERSON SET PER_FIRST_NAME = #firstName#, PER_LAST_NAME = #lastName#, PER_BIRTH_DATE = #birthDate#, PER_WEIGHT_KG = #weightInKilograms#, PER_HEIGHT_M = #heightInMeters# WHERE PER_ID = #id# DELETE PERSON WHERE PER_ID = #id# /sqlMap使用SQL Map 框架编程 好了,我们完成了所有的配置文件和映射文件,就剩下的应用的编码工作了。首先要设置SQL Map,读入刚创建好的SQL Map XML 配置文件。为简化这个工作,可以使用SQL Map 架构中提供的Resources 类。 String resource = “com/ibatis/example/sql-map-config.xml”; Reader reader = Resources.getResourceAsReader (resource); SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader); 以上的SqlMapClient对象是线程安全,并且应持久生存。对于一个特定的应用,只需 进行一次SqlMap 配置。因此,它可以作为基类的一个静态对象(即DAO 对象的基类),或者,如果您想让它有更大的作用范围,可以把它封装在自己需要的类中。这样你就可以这样写: public MyAppSqlConfig private static final SqlMapClient sqlMap; static try String resource = “com/ibatis/example/sqlMap-config.xml”; Reader reader = Resources.getResourceAsReader (resource); sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader); catch (Exception e) throw new RuntimeException (“Error initializing MyAppSqlConfig class. Cause: ”+ e); public static SqlMapClient getSqlMapInstance () return sqlMap; 从数据库读取对象 既然SqlMap 对象已完成初始化,就可以方便地使用它了。首先,我们用它从数据库中读取一个Person 对象。(在本例中,假设PERSON 表中已存在10 条记录,PER_ID 从1 到10)。要从数据库中得到一个Person对象,只需要SqlMap 实例,mapped statement 的名字和 一个Person ID 号。让我们读入PER_ID 是5 的Person 对象。 SqlMapClient sqlMap = MyAppSqlMapConfig.getSqlMapInstance(); / as coded above Integer personPk = new Integer(5); Person person = (Person) sqlMap.queryForObject (“getPerson”, personPk); 把对象写入数据库 现在已有了一个从数据库中读出的Person 对象,接着修改Person 对象的height 和weight属性,并将它写入数据库。 person.setHeightInMeters(1.83); / person as read from the database above person.setWeightInKilograms(86.36); sqlMap.update(“updatePerson”, person); 要删除这个Person 对象,也很容易。 sqlMap.delete(“deletePerson”, person); 类似地,也可以创建一个新的Person 对象。 Person newPerson = new Person(); newPerson.setId(11); / you would normally get the ID from a sequence or custom table newPerson.setFirstName(“Clinton”); newPerson.setLastName(“Begin”); newPerson.setBirthDate (null); newPerson.setHeightInMeters(1.83); newPerson.setWeightInKilograms(86.36); sqlMap.insert (“insertPerson”, newPerson); 这就是本书的所有内容了。 下一步 至此本教程结束了。请访问 网站下载完整的iBatis SQL Maps 开发指南,还有JPetStore 4,它是一个完整的Web 应用例子,基于Jakarta Struts,iBatis DAO 2.0 和SQL Maps 2.0 。 Clinton Begin对本文档不做任何担保、明示或暗示。 2004 Clinton Begin. 版权所有. 附:英文原文iBATIS SQL Maps TutorialClinton Begin Introuction This brief tutorial will take you through a walkthrough of a typical use of SQL Maps. The details of each of the topics below can be found in the SQL Maps developer guide available from Preparing to Use SQL Maps The SQL Maps framework is very tolerant of bad database models and even bad object models. Despite this, it is recommended that you use best practices when designing your database (proper normalization) and your object model. By doing so, you will get good performance and a clean design. The easiest place to start is to analyze what youre working with. What are your business objects? What are your database tables? How do they relate to each other? For the first example, consider the following simple Person class that conforms to the typical JavaBeans pattern. Person.java package examples.domain; /imports implied. public class Person private int id; private String firstName; private String lastName; private Date birthDate; private double weightInKilograms; private double heightInMeters; public int getId () return id; public void setId (int id) this.id = id; /lets assume we have the other getters and setters to save space How does this Person class map to our database? SQL Maps doesnt restrict you from having relationships such as table-per-class or multiple-tables-per-class or multiple-classes-per-table. Because you have the full power of SQL available to you, there are very few restrictions. For this example, lets use the following simple table which would be suitable for a table-per-class relationship: Person.sql CREATE TABLE PERSON( PER_ID NUMBER(5, 0) NOT NULL, PER_FIRST_NAME VARCHAR(40) NOT NULL, PER_LAST_NAME VARCHAR(40) NOT NULL, PER_BIRTH_DATE DATETIME , PER_WEIGHT_KG NUMBER(4, 2) NOT NULL, PER_HEIGHT_M NUMBER(4, 2) NOT NULL, PRIMARY KEY (PER_ID) ) The SQL Map Configuration File Once were comfortable with the classes and tables were working with, the best place to start is the SQL Map configuration file. This file will act as the root configuration for our SQL Map implementation. The configuration file is an XML file. Within it we will configure properties, JDBC DataSources and SQL Maps.It gives you a nice convenient location to centrally configure your DataSource which can be any number of different implementations. The framework can handle a number of DataSource implementations including iBATIS SimpleDataSource, Jakarta DBCP (Commons), and any DataSource that can be looked up via a JNDI context (e.g. from within an app server). These are described in more detail in the Developers Guide. The structure is simple and for the example above, might look like this: SqlMapConfigExample.xml SqlMapConf igEperties # This is just a simple properties file that simplifies automated configuration # of the SQL Maps configuration file (e.g. by Ant builds or continuous # integration tools for different environments etc.) # These values can be used in any property value in the file above (e.g. “$driver”) # Using a properties file such as this is completely optional. driver=oracle.jdbc.driver.OracleDriver url=jdbc :oracle:thin:localhost:1521:oracle1 username=jsmith password=test The SQL Map File(s) Now that we have a DataSource configured and our central configuration file is ready to go, we will need to provide the actual SQL Map file which contains our SQL code and the mappings for parameter objects and result objects (input and output respectively). Continuing with our example above, lets build an SQL Map file for the Person class and the PERSON table. Well start with the general structure of an SQL document, and a simple select statement: Person.xml SELECT PER_ID as id, PER_FIRST_NAME as firstName, PER_LAST_NAME as lastName, PER_BIRTH_DATE as birthDate, PER_WEIGHT_KG as weightInKilograms, PER_HEIGHT_M as heightInMeters FROM PERSON WHERE PER_ID = #value# The above example shows the simplest form of SQL Map. It uses a feature of the SQL Maps framework that automatically maps the columns of a ResultSet to JavaBeans properties (or Map keys etc.) based on name matching. The #value# token is an input parameter. More specifically, the use of “value” implies that we are using a simple primitive wrapper type (e.g. Integer; but were not limited to this). Although very simple, there are some limitations of using the auto-result mapping approach. There is no way to specify the types of the output columns (if necessary) or to automatically load related data (complex properties), and there is also a slight performance implication in that this approach requires accessing the ResultSetMetaData. By using a resultMap, we can overcome all of these limitations. But, for now simplicity is our goal, and we can always change to a different approach later (without changing the Java source code). Most database applications dont simply read from the database, they also have to modify data in the database. Weve already seen an example of how a simple SELECT looks in a mapped statement, but what about INSERT, UPDATE and DELETE? The good news is that it

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论