📄 ibatis开发人员指南(翻译自ibatis_db_guide-1-2-8)2.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0058)http://champion.ewuxi.com/old/opensource/ibatis/guide2.htm -->
<HTML><HEAD><TITLE>蓝色天空(原创作品)</TITLE><!-- InstanceBegin template="/Templates/文章.dwt" codeOutsideHTMLIsLocked="false" --><!-- InstanceBeginEditable name="doctitle" --><!-- InstanceEndEditable -->
<META http-equiv=Content-Type content="text/html; charset=gb2312"><LINK
href="ibatis开发人员指南(翻译自ibatis_db_guide-1-2-8)2_files/FORUM.css" type=text/css
rel=stylesheet><!-- InstanceBeginEditable name="head" -->
<STYLE type=text/css>.style1 {
FONT-WEIGHT: bold; FONT-SIZE: 12pt
}
</STYLE>
<!-- InstanceEndEditable --><!-- InstanceParam name="OptionalRegion1" type="boolean" value="true" -->
<META content="MSHTML 6.00.3790.0" name=GENERATOR></HEAD>
<BODY bgColor=#adced8>
<H4
align=center><!-- InstanceBeginEditable name="标题" -->ibatis开发人员指南(翻译自ibatis_db_guide-1-2-8)(2)<!-- InstanceEndEditable --></H4><!-- InstanceBeginEditable name="EditRegion3" -->
<P class=style1 align=center>Fast Track</P>
<P>本篇文章的第一部分将带你走过一系列的“fash Track”,带你浏览一遍SQL
maps的简单应用。在walkthrough之后,将有详细的论述。</P>
<P><STRONG>Fast Track: Preparing to Use SQL Maps</STRONG></P>
<P> SQL
Maps对不好的数据库模型甚至对象模型都有很强的容忍度。尽管如此,还是推荐你使用最佳实践来设计你的的数据库模型和对象模型。通过这样,你将得到更干净的设计和更好的性能。</P>
<P> 最简单的开始就是分析你在做的内容,商业模型是什么样的,表结构是什么样的,它们怎么样互相发生关系。第一个例子,我们就简单的实现一个典型的Persion类。</P>
<TABLE width="100%" bgColor=#cccccc border=1>
<TBODY>
<TR>
<TD class=smallFont>Person.java<BR>package examples.domain;<BR>//imports
implied….<BR>public class Person {<BR>private int id;<BR>private String
firstName;<BR>private String lastName;<BR>private Date
birthDate;<BR>private double weightInKilograms;<BR>private double
heightInMeters;<BR>public int getId () {<BR>return id;<BR>}<BR>public void
setId (int id) {<BR>this.id = id;<BR>}<BR>//…let’s assume we have the
other getters and setters to save space…<BR>}</TD></TR></TBODY></TABLE>
<P>现在persion对象怎么映射到数据库?SQL
Maps并不约束你必须要一个表一个对象或者多个表一个对象这种映射关系。因为你可以自由使用SQL语句,所以约束很小。在这个例子里,我们使用下面简单的表,实现一个表对象一个对象的映射关系。</P>
<TABLE width="100%" border=1>
<TBODY>
<TR>
<TD class=smallFont bgColor=#cccccc>Person.sql<BR>CREATE TABLE
PERSON(<BR>PER_ID NUMBER (5, 0) NOT NULL,<BR>PER_FIRST_NAME VARCHAR (40)
NOT NULL,<BR>PER_LAST_NAME VARCHAR (40) NOT NULL,<BR>PER_BIRTH_DATE
DATETIME ,<BR>PER_WEIGHT_KG NUMBER (4, 2) NOT NULL,<BR>PER_HEIGHT_M NUMBER
(4, 2) NOT NULL,<BR>PRIMARY KEY (PER_ID)<BR>)</TD></TR></TBODY></TABLE>
<P><STRONG>Fast Track: The SQL Map Configuration File</STRONG></P>
<P>当我们对我们的工作感到很舒适时,最好的开始就是SQL Map的配置文件。这个文件是SQL Map实现的根配置。</P>
<P>配置文件是XML文件,我们用它来配置属性,JDBC DataSources 和 SQL
Maps。它给我们一个便利的地方可以集中配置不同的DataSource。这个框架支持iBATIS SimpleDataSource, Jakarta DBCP
(Commons),以及其他任何可以通过JNDI context来访问的DataSource。我们在以后将详细讨论这个问题。现在我们用Jakarta
DBCP,结构很简单,象上面这个例子,它的配置文件如下。</P>
<P><SPAN class=smallFont>SqlMapConfigExample.xml</SPAN></P>
<TABLE width="100%" border=1>
<TBODY>
<TR>
<TD class=smallFont bgColor=#cccccc>
<P><?xml version="1.0" encoding="UTF-8"?><BR><BR><!DOCTYPE
sql-map-config<BR>PUBLIC "-//iBATIS.com//DTD SQL Map Config
1.0//EN"<BR>"http://www.ibatis.com/dtd/sql-map-config.dtd"><BR><BR><BR><!--
Always ensure to use the correct XML header as above!
--><BR><BR><sql-map-config><BR><BR><!-- The properties
(name=value) in the file specified here can be used placeholders in this
config<BR>file (e.g. “${driver}”. The file is relative to the classpath
and is completely optional. --><BR><BR><properties
resource="examples/sqlmap/maps/SqlMapConfigExample.properties"
/><BR><BR><!-- These settings control SqlMap configuration details,
primarily to do with transaction<BR>management. They are all optional
(more detail later in this document). --><BR><BR><settings
maxExecute="300"<BR> maxExecutePerConnection="1"<BR> maxTransactions="10"<BR> statementCacheSize="75"<BR> useGlobalTransactions="false"<BR> useBeansMetaClasses=”true”/><BR><!--
Configure a datasource to use with this SQL Map using Jakarta
DBCP.<BR>Notice the use of the properties from the above resource
--><BR><datasource name="basic" default =
"true"<BR>factory-class="com.ibatis.db.sqlmap.datasource.DbcpDataSourceFactory"><BR> <property
name="JDBC.Driver"
value="${driver}"/><BR> <property
name="JDBC.ConnectionURL"
value="${url}"/><BR> <property
name="JDBC.Username"
value="${username}"/><BR> <property
name="JDBC.Password"
value="${password}"/><BR> <property
name="Pool.MaximumActiveConnections"
value="10"/><BR> <property
name="Pool.MaximumIdleConnections"
value="5"/><BR> <property
name="Pool.MaximumWait"
value="60000"/><BR></datasource><BR><BR><!-- Identify all SQL
Map XML files to be loaded by this SQL map. Notice the paths<BR>are
relative to the classpath. For now, we only have one…
--><BR><BR><sql-map resource="examples/sqlmap/maps/Person.xml"
/><BR><BR></sql-map-config></P></TD></TR></TBODY></TABLE>
<P>SqlMapConfigExample.properties</P>
<TABLE width="100%" border=1>
<TBODY>
<TR>
<TD class=smallFont bgColor=#cccccc>
<P># This is just a simple properties file that simplifies automated
configuration<BR># of the SQL Maps configuration file (e.g. by Ant builds
or continuous<BR># integration tools for different environments…
etc.)<BR># These values can be used in any property value in the file
above (e.g. “${driver}”)<BR># Using a properties file such as this is
completely optional.</P>
<P><BR>driver=oracle.jdbc.driver.OracleDriver<BR>url=jdbc:oracle:thin:@localhost:1521:oracle1<BR>username=jsmith<BR>password=test</P></TD></TR></TBODY></TABLE>
<P><STRONG>Fast Track: The SQL Map File(s)</STRONG></P>
<P> 现在我们已经配置好DataSource了,然后就要准备核心配置文件了。我们需要准备一个实际的SQL
Map文件来存放SQL语句和以及用作映射的参数对象和结果对象(分别是输入和输出)。</P>
<P>继续我们上面的示例。让我们为Person类和Person表建立映射关系。我们先建立一个标准结构,和一个简单的select说明。</P>
<P>Person.xml</P>
<TABLE width="100%" border=1>
<TBODY>
<TR>
<TD class=smallFont bgColor=#cccccc><?xml version="1.0"
encoding="UTF-8"?><BR><BR><!DOCTYPE sql-map<BR>PUBLIC
"-//iBATIS.com//DTD SQL Map
1.0//EN"<BR>"http://www.ibatis.com/dtd/sql-map.dtd"><BR><BR><sql-map
name="Person"><BR><BR> <mapped-statement
name="getPerson"
result-class="examples.domain.Person"><BR> SELECT<BR> PER_ID
as id,<BR> PER_FIRST_NAME
as
firstName,<BR> PER_LAST_NAME
as
lastName,<BR> PER_BIRTH_DATE
as
birthDate,<BR> PER_WEIGHT_KG
as
weightInKilograms,<BR> PER_HEIGHT_M
as heightInMeters<BR> FROM
PERSON<BR> WHERE PER_ID =
#value#<BR> </mapped-statement><BR></sql-map></TD></TR></TBODY></TABLE>
<P>上面的示例显示了一个SQL map的一个最简单的组成。它使用了SQL
Maps的一个特性,就是自动根据字段名和JAVABean属性(Map的主键)名建立对应关系。#value#象征着一个输入参数,多情况下,使用"value"意味着我们使用一个基本类型
(e.g. Integer; but we’re not limited to this).</P>
<P>因为非常简单,所以使用这种方法有一些限制。首先不能明确指定每个字段的输入类型。没有办法自动加载相关数据(复杂类型),同时有一些性能影响,因为它使用了ResultSetMetaData。通过使用result-map,我们可以克服所有这些限制。但是现在,简单是我们的目标。同是,以后我们可以随便修改成其他方式(不需要修改java代码)。</P>
<P>多数JAVA程序不仅读取数据,还要更改数据。我们已经看到怎样在Map-statement里使用select
了,那Update,delete和Insert是什么样的?一个好消息,跟select没有什么区别。下面我们就完成一个我们的Person Sql
Map,包括一系列的statement用来操作和修改数据。</P>
<P>Person.xml</P>
<TABLE width="100%" border=1>
<TBODY>
<TR>
<TD bgColor=#cccccc>
<P class=smallFont style="TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><B><SPAN lang=EN-US
style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt"><?xml</SPAN><SPAN
lang=EN-US
style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt">
version="1.0" encoding="UTF-8"?><O:P></O:P></SPAN></B></P>
<P class=smallFont style="TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><B><SPAN lang=EN-US
style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt"><!DOCTYPE</SPAN><SPAN
lang=EN-US
style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt">
sql-map<O:P></O:P></SPAN></B></P>
<P class=smallFont style="TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><B><SPAN lang=EN-US
style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt">PUBLIC
"-//iBATIS.com//DTD SQL Map 1.0//EN"<O:P></O:P></SPAN></B></P>
<P class=smallFont style="TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><B><SPAN lang=EN-US
style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt">"http://www.ibatis.com/dtd/sql-map.dtd"><O:P></O:P></SPAN></B></P>
<P class=smallFont style="TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><B><SPAN lang=EN-US
style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt"><O:P></O:P></SPAN></B></P>
<P class=smallFont style="TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><B><SPAN lang=EN-US
style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt"><sql-map
name="Person"><O:P></O:P></SPAN></B></P>
<P class=smallFont style="TEXT-ALIGN: left; mso-layout-grid-align: none"
align=left><B><SPAN lang=EN-US
style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt"><O:P></O:P></SPAN></B></P>
<P class=smallFont
style="MARGIN-LEFT: 21pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 2.0gd"
align=left><B><SPAN lang=EN-US
style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial; mso-font-kerning: 0pt"><!--</SPAN><SPAN
lang=EN-US
style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial; mso-font-kerning: 0pt">
Use primitive wrapper type (e.g. Integer) as parameter and allow results
to<O:P></O:P></SPAN></B></P>
<P class=smallFont
style="MARGIN-LEFT: 21pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 2.0gd"
align=left><B><SPAN lang=EN-US
style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial; mso-font-kerning: 0pt">be</SPAN><SPAN
lang=EN-US
style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial; mso-font-kerning: 0pt">
auto-mapped results to Person object (JavaBean) properties
--><O:P></O:P></SPAN></B></P>
<P class=smallFont
style="MARGIN-LEFT: 21pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 2.0gd"
align=left><B><SPAN lang=EN-US
style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: Arial; mso-font-kerning: 0pt"><O:P></O:P></SPAN></B></P>
<P class=smallFont
style="MARGIN-LEFT: 21pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 2.0gd"
align=left><B><SPAN lang=EN-US
style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt"><mapped-statement
name="getPerson"
result-class="examples.domain.Person"><O:P></O:P></SPAN></B></P>
<P class=smallFont
style="MARGIN-LEFT: 42pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 4.0gd"
align=left><B><SPAN lang=EN-US
style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt">SELECT<O:P></O:P></SPAN></B></P>
<P class=smallFont
style="MARGIN-LEFT: 42pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 4.0gd"
align=left><B><SPAN lang=EN-US
style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt">PER_ID
as id,<O:P></O:P></SPAN></B></P>
<P class=smallFont
style="MARGIN-LEFT: 42pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 4.0gd"
align=left><B><SPAN lang=EN-US
style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt">PER_FIRST_NAME
as firstName,<O:P></O:P></SPAN></B></P>
<P class=smallFont
style="MARGIN-LEFT: 42pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 4.0gd"
align=left><B><SPAN lang=EN-US
style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt">PER_LAST_NAME
as lastName,<O:P></O:P></SPAN></B></P>
<P class=smallFont
style="MARGIN-LEFT: 42pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 4.0gd"
align=left><B><SPAN lang=EN-US
style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt">PER_BIRTH_DATE
as birthDate,<O:P></O:P></SPAN></B></P>
<P class=smallFont
style="MARGIN-LEFT: 42pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 4.0gd"
align=left><B><SPAN lang=EN-US
style="FONT-SIZE: 9pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt">PER_WEIGHT_KG
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -