﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-欢迎来到小小家院-文章分类-数据库</title><link>http://www.blogjava.net/wangxq/category/17205.html</link><description>共同努力,共造辉煌!</description><language>zh-cn</language><lastBuildDate>Thu, 08 Mar 2007 02:37:17 GMT</lastBuildDate><pubDate>Thu, 08 Mar 2007 02:37:17 GMT</pubDate><ttl>60</ttl><item><title>[转]Java 的JDBC 数据库连接池实现方法</title><link>http://www.blogjava.net/wangxq/articles/41818.html</link><dc:creator>扭转乾坤</dc:creator><author>扭转乾坤</author><pubDate>Wed, 19 Apr 2006 02:11:00 GMT</pubDate><guid>http://www.blogjava.net/wangxq/articles/41818.html</guid><wfw:comment>http://www.blogjava.net/wangxq/comments/41818.html</wfw:comment><comments>http://www.blogjava.net/wangxq/articles/41818.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wangxq/comments/commentRss/41818.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wangxq/services/trackbacks/41818.html</trackback:ping><description><![CDATA[自：<a href="http://www.matrix.org.cn/resource/article/43/43837_JDBC.html">http://www.matrix.org.cn/resource/article/43/43837_JDBC.html</a><br />Java 的JDBC 数据库连接池实现方法 <br /><br />关键字: Java, JDBC, Connection Pool, Database, 数据库连接池, sourcecode<br /><br />  虽然 J2EE 程序员一般都有现成的应用服务器所带的JDBC 数据库连接池，不过对于开发一般的 Java Application 、 Applet 或者 JSP、velocity 时，我们可用的JDBC 数据库连接池并不多，并且一般性能都不好。 Java 程序员都很羡慕 Windows ADO ，只需要 new Connection 就可以直接从数据库连接池中返回 Connection。并且 ADO Connection 是线程安全的，多个线程可以共用一个 Connection， 所以 ASP 程序一般都把 getConnection 放在 Global.asa 文件中，在 IIS 启动时建立数据库连接。ADO 的 Connection 和 Result 都有很好的缓冲，并且很容易使用。<br /><br />其实我们可以自己写一个JDBC 数据库连接池。写 JDBC connection pool 的注意事项有：<br /><br />1. 有一个简单的函数从连接池中得到一个 Connection。 <br />2. close 函数必须将 connection 放回 数据库连接池。 <br />3. 当数据库连接池中没有空闲的 connection， 数据库连接池必须能够自动增加 connection 个数。 <br />4. 当数据库连接池中的 connection 个数在某一个特别的时间变得很大，但是以后很长时间只用其中一小部分，应该可以自动将多余的 connection 关闭掉。 <br />5. 如果可能，应该提供debug 信息报告没有关闭的 new Connection 。 <br /><br />如果要 new Connection 就可以直接从数据库连接池中返回 Connection， 可以这样写( Mediator pattern ) (以下代码中使用了中文全角空格)：<br /><br /><pre class="overflow">public class EasyConnection implements java.sql.Connection{<br />　　private Connection m_delegate = null;<br /><br />　　public EasyConnection(){<br />　　　　m_delegate = getConnectionFromPool();<br />　　}<br /><br />　　public void close(){<br />　　　　putConnectionBackToPool(m_delegate);<br />　　}<br /><br />　　public PreparedStatement prepareStatement(String sql) throws SQLException{<br />　　　　m_delegate.prepareStatement(sql);<br />　　}<br /><br />　　//...... other method<br /><br />}</pre><br /><br />看来并不难。不过不建议这种写法，因为应该尽量避免使用 Java Interface, 关于 Java Interface 的缺点我另外再写文章讨论。大家关注的是 Connection Pool 的实现方法。下面给出一种实现方法。 <br /><br /><pre class="overflow">import java.sql.*;<br />import java.lang.reflect.*;<br />import java.util.*;<br />import java.io.*;<br /><br />public class SimpleConnetionPool {<br />　　private static LinkedList m_notUsedConnection = new LinkedList();<br />　　private static HashSet m_usedUsedConnection = new HashSet();<br />　　private static String m_url = "";<br />　　private static String m_user = "";<br />　　private static String m_password = "";<br />　　static final boolean DEBUG = true;<br />　　static private long m_lastClearClosedConnection = System.currentTimeMillis();<br />　　public static long CHECK_CLOSED_CONNECTION_TIME = 4 * 60 * 60 * 1000; //4 hours<br /><br />　　static {<br />　　　　initDriver();<br />　　}<br /><br />　　private SimpleConnetionPool() {<br />　　}<br /><br />　　private static void initDriver() {<br />　　　　Driver driver = null;<br />　　　　//load mysql driver<br />　　　　try {<br />　　　　　　driver = (Driver) Class.forName("com.mysql.jdbc.Driver").newInstance();<br />　　　　　　installDriver(driver);<br />　　　　} catch (Exception e) {<br />　　　　}<br /><br />　　　　//load postgresql driver<br />　　　　try {<br />　　　　　　driver = (Driver) Class.forName("org.postgresql.Driver").newInstance();<br />　　　　　　installDriver(driver);<br />　　　　} catch (Exception e) {<br />　　　　}<br />　　}<br /><br />　　public static void installDriver(Driver driver) {<br />　　　　try {<br />　　　　　　DriverManager.registerDriver(driver);<br />　　　　} catch (Exception e) {<br />　　　　　　e.printStackTrace();<br />　　　　}<br />　　}<br /><br /><br />　　public static synchronized Connection getConnection() {<br />　　　　clearClosedConnection();<br />　　　　while (m_notUsedConnection.size() &gt; 0) {<br />　　　　　　try {<br />　　　　　　　　ConnectionWrapper wrapper = (ConnectionWrapper) m_notUsedConnection.removeFirst();<br />　　　　　　　　if (wrapper.connection.isClosed()) {<br />　　　　　　　　　　continue;<br />　　　　　　　　}<br />　　　　　　　　m_usedUsedConnection.add(wrapper);<br />　　　　　　　　if (DEBUG) {<br />　　　　　　　　　　wrapper.debugInfo = new Throwable("Connection initial statement");<br />　　　　　　　　}<br />　　　　　　　　return wrapper.connection;<br />　　　　　　} catch (Exception e) {<br />　　　　　　}<br />　　　　}<br />　　　　int newCount = getIncreasingConnectionCount();<br />　　　　LinkedList list = new LinkedList();<br />　　　　ConnectionWrapper wrapper = null;<br />　　　　for (int i = 0; i &lt; newCount; i++) {<br />　　　　　　wrapper = getNewConnection();<br />　　　　　　if (wrapper != null) {<br />　　　　　　　　list.add(wrapper);<br />　　　　　　}<br />　　　　}<br />　　　　if (list.size() == 0) {<br />　　　　　　return null;<br />　　　　}<br />　　　　wrapper = (ConnectionWrapper) list.removeFirst();<br />　　　　m_usedUsedConnection.add(wrapper);<br /><br />　　　　m_notUsedConnection.addAll(list);<br />　　　　list.clear();<br /><br />　　　　return wrapper.connection;<br />　　}<br /><br />　　private static ConnectionWrapper getNewConnection() {<br />　　　　try {<br />　　　　　　Connection con = DriverManager.getConnection(m_url, m_user, m_password);<br />　　　　　　ConnectionWrapper wrapper = new ConnectionWrapper(con);<br />　　　　　　return wrapper;<br />　　　　} catch (Exception e) {<br />　　　　　　e.printStackTrace();<br />　　　　}<br />　　　　return null;<br />　　}<br /><br />　　static synchronized void pushConnectionBackToPool(ConnectionWrapper con) {<br />　　　　boolean exist = m_usedUsedConnection.remove(con);<br />　　　　if (exist) {<br />　　　　　　m_notUsedConnection.addLast(con);<br />　　　　}<br />　　}<br /><br />　　public static int close() {<br />　　　　int count = 0;<br /><br />　　　　Iterator iterator = m_notUsedConnection.iterator();<br />　　　　while (iterator.hasNext()) {<br />　　　　　　try {<br />　　　　　　　　( (ConnectionWrapper) iterator.next()).close();<br />　　　　　　　　count++;<br />　　　　　　} catch (Exception e) {<br />　　　　　　}<br />　　　　}<br />　　　　m_notUsedConnection.clear();<br /><br />　　　　iterator = m_usedUsedConnection.iterator();<br />　　　　while (iterator.hasNext()) {<br />　　　　　　try {<br />　　　　　　　　ConnectionWrapper wrapper = (ConnectionWrapper) iterator.next();<br />　　　　　　　　wrapper.close();<br />　　　　　　　　if (DEBUG) {<br />　　　　　　　　　　wrapper.debugInfo.printStackTrace();<br />　　　　　　　　}<br />　　　　　　　　count++;<br />　　　　　　} catch (Exception e) {<br />　　　　　　}<br />　　　　}<br />　　　　m_usedUsedConnection.clear();<br /><br />　　　　return count;<br />　　}<br /><br />　　private static void clearClosedConnection() {<br />　　　　long time = System.currentTimeMillis();<br />　　　　//sometimes user change system time,just return<br />　　　　if (time &lt; m_lastClearClosedConnection) {<br />　　　　　　time = m_lastClearClosedConnection;<br />　　　　　　return;<br />　　　　}<br />　　　　//no need check very often<br />　　　　if (time - m_lastClearClosedConnection &lt; CHECK_CLOSED_CONNECTION_TIME) {<br />　　　　　　return;<br />　　　　}<br />　　　　m_lastClearClosedConnection = time;<br /><br />　　　　//begin check<br />　　　　Iterator iterator = m_notUsedConnection.iterator();<br />　　　　while (iterator.hasNext()) {<br />　　　　　　ConnectionWrapper wrapper = (ConnectionWrapper) iterator.next();<br />　　　　　　try {<br />　　　　　　　　if (wrapper.connection.isClosed()) {<br />　　　　　　　　　　iterator.remove();<br />　　　　　　　　}<br />　　　　　　} catch (Exception e) {<br />　　　　　　　　iterator.remove();<br />　　　　　　　　if (DEBUG) {<br />　　　　　　　　　　System.out.println("connection is closed, this connection initial StackTrace");<br />　　　　　　　　　　wrapper.debugInfo.printStackTrace();<br />　　　　　　　　}<br />　　　　　　}<br />　　　　}<br /><br />　　　　//make connection pool size smaller if too big<br />　　　　int decrease = getDecreasingConnectionCount();<br />　　　　if (m_notUsedConnection.size() &lt; decrease) {<br />　　　　　　return;<br />　　　　}<br /><br />　　　　while (decrease-- &gt; 0) {<br />　　　　　　ConnectionWrapper wrapper = (ConnectionWrapper) m_notUsedConnection.removeFirst();<br />　　　　　　try {<br />　　　　　　　　wrapper.connection.close();<br />　　　　　　} catch (Exception e) {<br />　　　　　　}<br />　　　　}<br />　　}<br /><br />　　/**<br />　　 * get increasing connection count, not just add 1 connection<br />　　 * @return count<br />　　 */<br />　　public static int getIncreasingConnectionCount() {<br />　　　　int count = 1;<br />　　　　int current = getConnectionCount();<br />　　　　count = current / 4;<br />　　　　if (count &lt; 1) {<br />　　　　　　count = 1;<br />　　　　}<br />　　　　return count;<br />　　}<br /><br />　　/**<br />　　 * get decreasing connection count, not just remove 1 connection<br />　　 * @return count<br />　　 */<br />　　public static int getDecreasingConnectionCount() {<br />　　　　int count = 0;<br />　　　　int current = getConnectionCount();<br />　　　　if (current &lt; 10) {<br />　　　　　　return 0;<br />　　　　}<br />　　　　return current / 3;<br />　　}<br /><br />　　public synchronized static void printDebugMsg() {<br />　　　　printDebugMsg(System.out);<br />　　}<br /><br />　　public synchronized static void printDebugMsg(PrintStream out) {<br />　　　　if (DEBUG == false) {<br />　　　　　　return;<br />　　　　}<br />　　　　StringBuffer msg = new StringBuffer();<br />　　　　msg.append("debug message in " + SimpleConnetionPool.class.getName());<br />　　　　msg.append("\r\n");<br />　　　　msg.append("total count is connection pool: " + getConnectionCount());<br />　　　　msg.append("\r\n");<br />　　　　msg.append("not used connection count: " + getNotUsedConnectionCount());<br />　　　　msg.append("\r\n");<br />　　　　msg.append("used connection, count: " + getUsedConnectionCount());<br />　　　　out.println(msg);<br />　　　　Iterator iterator = m_usedUsedConnection.iterator();<br />　　　　while (iterator.hasNext()) {<br />　　　　　　ConnectionWrapper wrapper = (ConnectionWrapper) iterator.next();<br />　　　　　　wrapper.debugInfo.printStackTrace(out);<br />　　　　}<br />　　　　out.println();<br />　　}<br /><br />　　public static synchronized int getNotUsedConnectionCount() {<br />　　　　return m_notUsedConnection.size();<br />　　}<br /><br />　　public static synchronized int getUsedConnectionCount() {<br />　　　　return m_usedUsedConnection.size();<br />　　}<br /><br />　　public static synchronized int getConnectionCount() {<br />　　　　return m_notUsedConnection.size() + m_usedUsedConnection.size();<br />　　}<br /><br />　　public static String getUrl() {<br />　　　　return m_url;<br />　　}<br /><br />　　public static void setUrl(String url) {<br />　　　　if (url == null) {<br />　　　　　　return;<br />　　　　}<br />　　　　m_url = url.trim();<br />　　}<br /><br />　　public static String getUser() {<br />　　　　return m_user;<br />　　}<br /><br />　　public static void setUser(String user) {<br />　　　　if (user == null) {<br />　　　　　　return;<br />　　　　}<br />　　　　m_user = user.trim();<br />　　}<br /><br />　　public static String getPassword() {<br />　　　　return m_password;<br />　　}<br /><br />　　public static void setPassword(String password) {<br />　　　　if (password == null) {<br />　　　　　　return;<br />　　　　}<br />　　　　m_password = password.trim();<br />　　}<br /><br />}<br /><br />class ConnectionWrapper implements InvocationHandler {<br />　　private final static String CLOSE_METHOD_NAME = "close";<br />　　public Connection connection = null;<br />　　private Connection m_originConnection = null;<br />　　public long lastAccessTime = System.currentTimeMillis();<br />　　Throwable debugInfo = new Throwable("Connection initial statement");<br /><br />　　ConnectionWrapper(Connection con) {<br />　　　　Class[] interfaces = {java.sql.Connection.class};<br />　　　　this.connection = (Connection) Proxy.newProxyInstance(<br />　　　　　　con.getClass().getClassLoader(),<br />　　　　　　interfaces, this);<br />　　　　m_originConnection = con;<br />　　}<br /><br />　　void close() throws SQLException {<br />　　　　m_originConnection.close();<br />　　}<br /><br />　　public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {<br />　　　　Object obj = null;<br />　　　　if (CLOSE_METHOD_NAME.equals(m.getName())) {<br />　　　　　　SimpleConnetionPool.pushConnectionBackToPool(this);<br />　　　　}<br />　　　　else {<br />　　　　　　obj = m.invoke(m_originConnection, args);<br />　　　　}<br />　　　　lastAccessTime = System.currentTimeMillis();<br />　　　　return obj;<br />　　}<br />}</pre><br /><br />使用方法<br /><br /><pre class="overflow">public class TestConnectionPool{<br />　　public static void main(String[] args) {<br />　　　　SimpleConnetionPool.setUrl(DBTools.getDatabaseUrl());<br />　　　　SimpleConnetionPool.setUser(DBTools.getDatabaseUserName());<br />　　　　SimpleConnetionPool.setPassword(DBTools.getDatabasePassword());<br /><br />　　　　Connection con = SimpleConnetionPool.getConnection();<br />　　　　Connection con1 = SimpleConnetionPool.getConnection();<br />　　　　Connection con2 = SimpleConnetionPool.getConnection();<br /><br />　　　　//do something with con ...<br /><br />　　　　try {<br />　　　　　　con.close();<br />　　　　} catch (Exception e) {}<br /><br />　　　　try {<br />　　　　　　con1.close();<br />　　　　} catch (Exception e) {}<br /><br />　　　　try {<br />　　　　　　con2.close();<br />　　　　} catch (Exception e) {}<br /><br />　　　　con = SimpleConnetionPool.getConnection();<br />　　　　con1 = SimpleConnetionPool.getConnection();<br />　　　　try {<br />　　　　　　con1.close();<br />　　　　} catch (Exception e) {}<br /><br />　　　　con2 = SimpleConnetionPool.getConnection();<br />　　　　SimpleConnetionPool.printDebugMsg();<br /><br />　　}<br />}</pre><br /><br />运行测试程序后打印连接池中 Connection 状态， 以及正在使用的没有关闭 Connection 信息。<br /><img src ="http://www.blogjava.net/wangxq/aggbug/41818.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wangxq/" target="_blank">扭转乾坤</a> 2006-04-19 10:11 <a href="http://www.blogjava.net/wangxq/articles/41818.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]JDBC连接数据库经验技巧</title><link>http://www.blogjava.net/wangxq/articles/37746.html</link><dc:creator>扭转乾坤</dc:creator><author>扭转乾坤</author><pubDate>Tue, 28 Mar 2006 03:13:00 GMT</pubDate><guid>http://www.blogjava.net/wangxq/articles/37746.html</guid><wfw:comment>http://www.blogjava.net/wangxq/comments/37746.html</wfw:comment><comments>http://www.blogjava.net/wangxq/articles/37746.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wangxq/comments/commentRss/37746.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wangxq/services/trackbacks/37746.html</trackback:ping><description><![CDATA[
		<span id="LblContent">from: <a href="http://it.com.cn/f/edu/053/22/90113.htm">http://it.com.cn/f/edu/053/22/90113.htm</a><br />Java数据库连接（JDBC）由一组用 Java 编程语言编写的类和接口组成。JDBC 为工具/数据库开发人员提供了一个标准的 API，使他们能够用纯Java API 来编写数据库应用程序。然而各个开发商的接口并不完全相同，所以开发环境的变化会带来一定的配置变化。本文主要集合了不同数据库的连接方式。 <br /><br />　　<strong>一、连接各种数据库方式速查表 <br /></strong><br />　　下面罗列了各种数据库使用JDBC连接的方式，可以作为一个手册使用。 <br /><br />　　1、Oracle8/8i/9i数据库（thin模式） <br /><br />　　Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); <br />　　String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID <br />　　String user="test"; <br />　　String password="test"; <br />　　Connection conn= DriverManager.getConnection(url,user,password); <br /><br />　　2、DB2数据库 <br /><br />　　Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance(); <br />　　String url="jdbc:db2://localhost:5000/sample"; //sample为你的数据库名 <br />　　String user="admin"; <br />　　String password=""; <br />　　Connection conn= DriverManager.getConnection(url,user,password); <br /><br />　　3、Sql Server7.0/2000数据库 <br /><br />　　Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); <br />　　String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb"; <br />　　//mydb为数据库 <br />　　String user="sa"; <br />　　String password=""; <br />　　Connection conn= DriverManager.getConnection(url,user,password); <br /><br />　　4、Sybase数据库 <br /><br />　　Class.forName("com.sybase.jdbc.SybDriver").newInstance(); <br />　　String url =" jdbc:sybase:Tds:localhost:5007/myDB";//myDB为你的数据库名 <br />　　Properties sysProps = System.getProperties(); <br />　　SysProps.put("user","userid"); <br />　　SysProps.put("password","user_password"); <br />　　Connection conn= DriverManager.getConnection(url, SysProps); <br /><br />　　5、Informix数据库 <br /><br />　　Class.forName("com.informix.jdbc.IfxDriver").newInstance(); <br />　　String url = "jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver; <br />　　user=testuser;password=testpassword"; //myDB为数据库名 <br />　　Connection conn= DriverManager.getConnection(url); <br /><br />　　6、MySQL数据库 <br /><br />　　Class.forName("org.gjt.mm.mysql.Driver").newInstance(); <br />　　String url ="jdbc:mysql://localhost/myDB?user=soft&amp;password=soft1234&amp;useUnicode=true&amp;characterEncoding=8859_1" <br />　　//myDB为数据库名 <br />　　Connection conn= DriverManager.getConnection(url); <br /><br />　　7、PostgreSQL数据库 <br /><br />　　Class.forName("org.postgresql.Driver").newInstance(); <br />　　String url ="jdbc:postgresql://localhost/myDB" //myDB为数据库名 <br />　　String user="myuser"; <br />　　String password="mypassword"; <br />　　Connection conn= DriverManager.getConnection(url,user,password); <br /><br />　　8、access数据库直连用ODBC的 <br /><br />　　Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ; <br />　　String url="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ="+application.getRealPath("/Data/ReportDemo.mdb"); <br />　　Connection conn = DriverManager.getConnection(url,"",""); <br />　　Statement stmtNew=conn.createStatement() ; <br /><br />　　<strong>二、JDBC连接MySql方式 <br /></strong><br />　　下面是使用JDBC连接MySql的一个小的教程 <br /><br />　　1、查找驱动程序 <br /><br />　　MySQL目前提供的java驱动程序为Connection/J，可以从MySQL官方网站下载，并找到mysql-connector-java-3.0.15-ga-bin.jar文件，此驱动程序为纯java驱动程序，不需做其他配置。 <br /><br />　　2、动态指定classpath <br /><br />　　如果需要执行时动态指定classpath，就在执行时采用－cp方式。否则将上面的.jar文件加入到classpath环境变量中。 <br /><br />　　3、加载驱动程序 <br /><br />　　try{ <br />　　Class.forName(com.mysql.jdbc.Driver); <br />　　System.out.println(Success loading Mysql Driver!); <br />　　}catch(Exception e) <br />　　{ <br />　　System.out.println(Error loading Mysql Driver!); <br />　　e.printStackTrace(); <br />　　} <br /><br />　　4、设置连接的url <br /><br />　　jdbc：mysql：//localhost/databasename[?pa=va][＆pa=va] <br /><br />　　 <strong>三、以下列出了在使用JDBC来连接Oracle数据库时可以使用的一些技巧 <br /><br /></strong>　　1、在客户端软件开发中使用Thin驱动程序 <br /><br />　　在开发Java软件方面，Oracle的数据库提供了四种类型的驱动程序，二种用于应用软件、applets、servlets等客户端软件，另外二种用于数据库中的Java存储过程等服务器端软件。在客户机端软件的开发中，我们可以选择OCI驱动程序或Thin驱动程序。OCI驱动程序利用Java本地化接口（JNI），通过Oracle客户端软件与数据库进行通讯。Thin驱动程序是纯Java驱动程序，它直接与数据库进行通讯。为了获得最高的性能，Oracle建议在客户端软件的开发中使用OCI驱动程序，这似乎是正确的。但我建议使用Thin驱动程序，因为通过多次测试发现，在通常情况下，Thin驱动程序的性能都超过了OCI驱动程序。 <br /><br />　　2、关闭自动提交功能，提高系统性能 <br /><br />　　在第一次建立与数据库的连接时，在缺省情况下，连接是在自动提交模式下的。为了获得更好的性能，可以通过调用带布尔值false参数的Connection类的setAutoCommit()方法关闭自动提交功能，如下所示： <br /><br />　　conn.setAutoCommit(false); <br /><br />　　值得注意的是，一旦关闭了自动提交功能，我们就需要通过调用Connection类的commit()和rollback()方法来人工的方式对事务进行管理。 <br /><br />　　3、在动态SQL或有时间限制的命令中使用Statement对象 <br /><br />　　在执行SQL命令时，我们有二种选择：可以使用PreparedStatement对象，也可以使用Statement对象。无论多少次地使用同一个SQL命令，PreparedStatement都只对它解析和编译一次。当使用Statement对象时，每次执行一个SQL命令时，都会对它进行解析和编译。这可能会使你认为，使用PreparedStatement对象比使用Statement对象的速度更快。然而，我进行的测试表明，在客户端软件中，情况并非如此。因此，在有时间限制的SQL操作中，除非成批地处理SQL命令，我们应当考虑使用Statement对象。 <br /><br />　　此外，使用Statement对象也使得编写动态SQL命令更加简单，因为我们可以将字符串连接在一起，建立一个有效的SQL命令。因此，我认为，Statement对象可以使动态SQL命令的创建和执行变得更加简单。 <br /><br />　　4、利用helper函数对动态SQL命令进行格式化 <br /><br />　　在创建使用Statement对象执行的动态SQL命令时，我们需要处理一些格式化方面的问题。例如，如果我们想创建一个将名字O'Reilly插入表中的SQL命令，则必须使用二个相连的“''”号替换O'Reilly中的“'”号。完成这些工作的最好的方法是创建一个完成替换操作的helper方法，然后在连接字符串心服用公式表达一个SQL命令时，使用创建的helper方法。与此类似的是，我们可以让helper方法接受一个Date型的值，然后让它输出基于Oracle的to_date()函数的字符串表达式。 <br /><br />　　5、利用PreparedStatement对象提高数据库的总体效率 <br /><br />　　在使用PreparedStatement对象执行SQL命令时，命令被数据库进行解析和编译，然后被放到命令缓冲区。然后，每当执行同一个PreparedStatement对象时，它就会被再解析一次，但不会被再次编译。在缓冲区中可以发现预编译的命令，并且可以重新使用。在有大量用户的企业级应用软件中，经常会重复执行相同的SQL命令，使用PreparedStatement对象带来的编译次数的减少能够提高数据库的总体性能。如果不是在客户端创建、预备、执行PreparedStatement任务需要的时间长于Statement任务，我会建议在除动态SQL命令之外的所有情况下使用PreparedStatement对象。 <br /><br />　　6、在成批处理重复的插入或更新操作中使用PreparedStatement对象 <br /><br />　　如果成批地处理插入和更新操作，就能够显著地减少它们所需要的时间。Oracle提供的Statement和 CallableStatement并不真正地支持批处理，只有PreparedStatement对象才真正地支持批处理。我们可以使用addBatch()和executeBatch()方法选择标准的JDBC批处理，或者通过利用PreparedStatement对象的setExecuteBatch()方法和标准的executeUpdate()方法选择速度更快的Oracle专有的方法。要使用Oracle专有的批处理机制，可以以如下所示的方式调用setExecuteBatch()： <br /><br />　　PreparedStatement pstmt3D null; <br />　　try { <br />　　((OraclePreparedStatement)pstmt).setExecuteBatch(30); <br />　　... <br />　　pstmt.executeUpdate(); <br />　　} <br /><br />　　调用setExecuteBatch()时指定的值是一个上限，当达到该值时，就会自动地引发SQL命令执行，标准的executeUpdate()方法就会被作为批处理送到数据库中。我们可以通过调用PreparedStatement类的sendBatch()方法随时传输批处理任务。 <br /><br />　　7、使用Oracle locator方法插入、更新大对象（LOB） <br /><br />　　Oracle的PreparedStatement类不完全支持BLOB和CLOB等大对象的处理，尤其是Thin驱动程序不支持利用PreparedStatement对象的setObject()和setBinaryStream()方法设置BLOB的值，也不支持利用setCharacterStream()方法设置CLOB的值。只有locator本身中的方法才能够从数据库中获取LOB类型的值。可以使用PreparedStatement对象插入或更新LOB，但需要使用locator才能获取LOB的值。由于存在这二个问题，因此，我建议使用locator的方法来插入、更新或获取LOB的值。 <br /><br />　　8、使用SQL92语法调用存储过程 <br /><br />　　在调用存储过程时，我们可以使用SQL92或Oracle PL/SQL，由于使用Oracle PL/SQL并没有什么实际的好处，而且会给以后维护你的应用程序的开发人员带来麻烦，因此，我建议在调用存储过程时使用SQL92。 <br /><br />　　9、使用Object SQL将对象模式转移到数据库中 <br /><br />　　既然可以将Oracle的数据库作为一种面向对象的数据库来使用，就可以考虑将应用程序中的面向对象模式转到数据库中。目前的方法是创建Java bean作为伪装的数据库对象，将它们的属性映射到关系表中，然后在这些bean中添加方法。尽管这样作在Java中没有什么问题，但由于操作都是在数据库之外进行的，因此其他访问数据库的应用软件无法利用对象模式。如果利用Oracle的面向对象的技术，可以通过创建一个新的数据库对象类型在数据库中模仿其数据和操作，然后使用JPublisher等工具生成自己的Java bean类。如果使用这种方式，不但Java应用程序可以使用应用软件的对象模式，其他需要共享你的应用中的数据和操作的应用软件也可以使用应用软件中的对象模式。 <br /><br />　　10、利用SQL完成数据库内的操作 <br /><br />　　我要向大家介绍的最重要的经验是充分利用SQL的面向集合的方法来解决数据库处理需求，而不是使用Java等过程化的编程语言。 <br /><br />　　如果编程人员要在一个表中查找许多行，结果中的每个行都会查找其他表中的数据，最后，编程人员创建了独立的UPDATE命令来成批地更新第一个表中的数据。与此类似的任务可以通过在set子句中使用多列子查询而在一个UPDATE命令中完成。当能够在单一的SQL命令中完成任务，何必要让数据在网上流来流去的？我建议用户认真学习如何最大限度地发挥SQL的功能。</span>
<img src ="http://www.blogjava.net/wangxq/aggbug/37746.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wangxq/" target="_blank">扭转乾坤</a> 2006-03-28 11:13 <a href="http://www.blogjava.net/wangxq/articles/37746.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]iBATIS SQL Maps</title><link>http://www.blogjava.net/wangxq/articles/36809.html</link><dc:creator>扭转乾坤</dc:creator><author>扭转乾坤</author><pubDate>Wed, 22 Mar 2006 03:25:00 GMT</pubDate><guid>http://www.blogjava.net/wangxq/articles/36809.html</guid><wfw:comment>http://www.blogjava.net/wangxq/comments/36809.html</wfw:comment><comments>http://www.blogjava.net/wangxq/articles/36809.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wangxq/comments/commentRss/36809.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wangxq/services/trackbacks/36809.html</trackback:ping><description><![CDATA[
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<b style="mso-bidi-font-weight: normal">
						<span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">自：<a href="http://www.codesky.net/article/doc/200412/2004121597969923.htm">http://www.codesky.net/article/doc/200412/2004121597969923.htm</a><br />什么是</span>
				</b>
				<b style="mso-bidi-font-weight: normal">
						<span style="FONT-SIZE: 14pt; FONT-FAMILY: Arial">
								<span lang="EN-US">
										<a href="http://www.ibatis.com/">iBATIS</a>
								</span>
						</span>
				</b>
				<b style="mso-bidi-font-weight: normal">
						<span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">？</span>
				</b>
				<b style="mso-bidi-font-weight: normal">
						<span lang="EN-US" style="FONT-SIZE: 14pt; FONT-FAMILY: Arial">
								<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?>
								<o:p>
								</o:p>
						</span>
				</b>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0">
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial">
						<br />    和众多的</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt">SourceForge </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial">开源项目一样，</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt">iBATIS </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial">曾经也是其中的一员。在</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt">2004</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial">年</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt">11</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial">月</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt">3</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial">日成功地成为了</span>
				<span style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt">
						<span lang="EN-US">
								<a href="http://incubator.apache.org/projects/ibatis.html">Apache Incubator</a>
								<span style="COLOR: black">
								</span>
						</span>
				</span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial">下的子项目。</span>
				<span style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt">
				</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">iBATIS </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">包括</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">for Java </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">和</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">for .NET </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">两个版本，</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">for Java </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">版提供了</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">SQL Maps </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">和</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">DAO </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">框架，</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">for .NET </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">只提供了</span>
				<span style="FONT-FAMILY: Arial">
						<span lang="EN-US">SQL Maps </span>
				</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">框架。从现在开始我们只对</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">for Java </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">版的</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">SQL Maps </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">展开讨论。</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0">
				<span lang="EN-US" style="FONT-FAMILY: Arial">
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0">
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">你可以在</span>
				<span style="FONT-FAMILY: Arial">
						<span lang="EN-US">
								<a href="http://www.ibatis.com/">http://www.ibatis.com</a>
						</span>
				</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">下载</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">iBATIS for Java</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">，目前最新版本是</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">2.0.9</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">，压缩包里已经包含了</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">SQL Maps(ibatis-sqlmap-2.jar) </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">和</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">DAO </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">框架</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">(ibatis-dao-2.jar)</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">。</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-FAMILY: Arial">
						<o:p>
								<br />
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<b style="mso-bidi-font-weight: normal">
						<span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">为什么选择</span>
				</b>
				<b style="mso-bidi-font-weight: normal">
						<span style="FONT-SIZE: 14pt; FONT-FAMILY: Arial">
								<span lang="EN-US">
										<a href="http://www.ibatis.com/">iBATIS</a>
								</span>
						</span>
				</b>
				<b style="mso-bidi-font-weight: normal">
						<span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">？</span>
				</b>
				<b style="mso-bidi-font-weight: normal">
						<span lang="EN-US" style="FONT-SIZE: 14pt; FONT-FAMILY: Arial">
								<o:p>
								</o:p>
						</span>
				</b>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0">
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">
						<br />    也许各位看官已在各种不同的技术资料上了解到它的优势。但是对于我来说，选择它的理由只有一个</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">——“</span>
				<b style="mso-bidi-font-weight: normal">
						<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">利用原有资源</span>
				</b>
				<span lang="EN-US" style="FONT-FAMILY: Arial">”</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">。</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">Hibernate </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">的却优秀，但是用来整合原有系统，它却很难胜任。例如，以前在进行数据建模时使用了复合主键、跨越多表产生的几十甚至上百行的查询语句、利用原有存储过程</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">… … </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">当面临这一系列问题时</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">Hibernate </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">就显得力不从心了，要想使用</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">Hibernate</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">就只能改造原有系统！</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0">
				<span lang="EN-US" style="FONT-FAMILY: Arial">
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0">
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">当我面临系统整合问题时（整合的要求很简单：只需要保留原有系统查询部分），</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">iBATIS </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">进入了我的视线。原有系统中除</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">SQL</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">语句需要小小的修改外，数据表、查询结果都不需要改变！也不用像</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">Hibernate </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">那样映射出众多的配置文件、</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">POJO</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">，一下子清爽了很多。</span>
				<b style="mso-bidi-font-weight: normal">
						<span lang="EN-US" style="COLOR: red; FONT-FAMILY: Arial">BTW</span>
				</b>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">：</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">Hibernate </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">这种做法没有错！只是我只需要查询功能，仅仅是取我所好而已，避免了杀鸡用牛刀。</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0">
				<span lang="EN-US" style="FONT-FAMILY: Arial">
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0">
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">目前，系统整合已经结束，花了一个月时间。如果使用</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">Hibernate</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">，恐怕我现在还在为怎么设计数据表、怎样下</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">HQL</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">而和同事争论。</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"> </p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<b style="mso-bidi-font-weight: normal">
						<span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">开始另一次</span>
				</b>
				<b style="mso-bidi-font-weight: normal">
						<span style="FONT-SIZE: 14pt; FONT-FAMILY: Arial">
								<span lang="EN-US">O/R Mapping </span>
						</span>
				</b>
				<b style="mso-bidi-font-weight: normal">
						<span style="FONT-SIZE: 14pt; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">之旅</span>
				</b>
				<span lang="EN-US" style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0">
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial">
						<br />    上次</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt">O/R Mapping </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial">之旅的</span>
				<span style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt">
						<span lang="EN-US">BO</span>
				</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial">、数据表还可以重用，只是把数据库迁移到了</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt">MySQL</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial">。<span style="COLOR: black">打开</span></span>
				<span lang="EN-US" style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt">Eclipse</span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial">，新建一个</span>
				<span lang="EN-US" style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt">Resin </span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial">项目。把</span>
				<span style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt">
				</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">ibatis-sqlmap-2.jar </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">和</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">ibatis-common-2.jar </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">拷贝到</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">lib </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">目录下，再导入项目。和</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">Hibernate </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">自动配置、自动映射相比，</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">iBATIS </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">的一切都是手工完成的。</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-FAMILY: Arial">
						<span style="mso-spacerun: yes">    </span>
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-FAMILY: Arial">
						<span style="mso-spacerun: yes">    </span>
				</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">在</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">src </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">下建立配置文件</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">SqlMapConfig.xml</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">，数据库链接、连接池、</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">SqlMap </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">映射文件</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">… … </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">这些都要靠它了。官方参考手册对怎样进行设置有很详细的描述，我只对要用到的地方进行粗略说明。<br /></span>
				<span lang="EN-US" style="FONT-FAMILY: Arial">
						<o:p>
						</o:p>
				</span>
		</p>
		<p>
		</p>
		<table style="WIDTH: 509px; HEIGHT: 100px" height="100" cellspacing="0" cellpadding="0" width="509" border="1">
				<tbody>
						<tr>
								<td>
										<p>
												<font size="2">&lt;?xml version="1.0" encoding="UTF-8" ?&gt;<br />&lt;!DOCTYPE sqlMapConfig<br />    PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"<br />    "</font>
												<a href="http://www.ibatis.com/dtd/sql-map-config-2.dtd">
														<font size="2">http://www.ibatis.com/dtd/sql-map-config-2.dtd</font>
												</a>
												<font size="2">"&gt;</font>
										</p>
										<p>
												<font size="2">&lt;sqlMapConfig&gt;</font>
										</p>
										<p>
												<font size="2">  &lt;settings<br />    cacheModelsEnabled="true"<br />    enhancementEnabled="true"<br />    lazyLoadingEnabled="true"<br />    errorTracingEnabled="false"<br />    maxRequests="32"<br />    maxSessions="10"<br />    maxTransactions="5"<br />    useStatementNamespaces="false"<br />    /&gt;</font>
										</p>
										<p>
												<font size="2">  &lt;transactionManager type="JDBC"&gt;<br />    &lt;dataSource type="SIMPLE"&gt;<br />      &lt;property name="JDBC.Driver" value="org.gjt.mm.mysql.Driver"/&gt;<br />      &lt;property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost/new_db?useUnicode=true"/&gt;<br />      &lt;property name="JDBC.Username" value="root"/&gt;<br />      &lt;property name="JDBC.Password" value=""/&gt;<br />      &lt;property name="JDBC.DefaultAutoCommit" value="true"/&gt;<br />      &lt;property name="Pool.MaximumActiveConnections" value="10"/&gt;<br />      &lt;property name="Pool.MaximumIdleConnections" value="5"/&gt;<br />      &lt;property name="Pool.MaximumCheckoutTime" value="120000"/&gt;<br />      &lt;property name="Pool.TimeToWait" value="500"/&gt;<br />      &lt;property name="Pool.PingQuery" value="select 1 from ACCOUNT"/&gt;<br />      &lt;property name="Pool.PingEnabled" value="false"/&gt;<br />      &lt;property name="Pool.PingConnectionsOlderThan" value="1"/&gt;<br />      &lt;property name="Pool.PingConnectionsNotUsedFor" value="1"/&gt;<br />      &lt;property name="Pool.QuietMode" value="true"/&gt;<br />    &lt;/dataSource&gt;<br />  &lt;/transactionManager&gt;</font>
										</p>
										<p>
												<font size="2">  &lt;sqlMap resource="bo/mapping/AutoMag.xml"/&gt;<br />&lt;/sqlMapConfig&gt;</font>
										</p>
								</td>
						</tr>
				</tbody>
		</table>
		<b style="mso-bidi-font-weight: normal">
				<span lang="EN-US" style="FONT-SIZE: 12pt; FONT-FAMILY: Arial">
						<br />transactionManager </span>
		</b>
		<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial">元素定义了</span>
		<span lang="EN-US" style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt">iBATIS </span>
		<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial">事务管理器。</span>
		<span lang="EN-US" style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt">type </span>
		<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial">可选项包括：</span>
		<span lang="EN-US" style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt">
				<o:p> </o:p>
		</span>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.1pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 2.0" align="left">
				<b style="mso-bidi-font-weight: normal">
						<span lang="EN-US" style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">JDBC</span>
				</b>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">：通过传统</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">JDBC </span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">来管理事务。</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21.1pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 2.0" align="left">
				<b style="mso-bidi-font-weight: normal">
						<span lang="EN-US" style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">JTA</span>
				</b>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">：使用一个</span>
				<span style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
						<span lang="EN-US">JTA </span>
				</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">全局事务，使</span>
				<span style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
				</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt">iBATIS</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
				</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">的事务包括在更大的事务范围内（跨数据库</span>
				<span lang="EN-US" style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">Session</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">的），这个更大的事务范围可能包括了其他的数据库和事务资源。这个配置需要一个</span>
				<span style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
						<span lang="EN-US">UserTransaction </span>
				</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">属性，以便从</span>
				<span style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
						<span lang="EN-US">JNDI </span>
				</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">获得一个</span>
				<span style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
						<span lang="EN-US">UserTransaction</span>
				</span>
				<span style="FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">。</span>
				<span style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
						<span lang="EN-US">
								<span style="mso-spacerun: yes"> </span>
						</span>
				</span>
		</p>
		<p class="a" style="TEXT-JUSTIFY: inter-ideograph; MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 20.65pt; TEXT-ALIGN: justify; mso-char-indent-count: 1.96">
				<b style="mso-bidi-font-weight: normal">
						<span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">EXTERNAL</span>
				</b>
				<span style="FONT-SIZE: 10.5pt; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">
						<font face="宋体">：调用</font>
				</span>
				<span style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">
						<span lang="EN-US">iBATIS </span>
				</span>
				<font face="宋体">
						<span style="FONT-SIZE: 10.5pt; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">以外的其他事务管理器来管理事务。</span>
						<span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">
								<o:p>
								</o:p>
						</span>
				</font>
		</p>
		<p class="Default" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<b style="mso-bidi-font-weight: normal">
						<span lang="EN-US" style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: Arial">dataSource </span>
				</b>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial">元素是</span>
				<span lang="EN-US" style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt">transactionManager </span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial">的一部分。</span>
				<span lang="EN-US" style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt">type </span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial">可选项包括：<br /></span>
				<span lang="EN-US" style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt">
						<span style="mso-spacerun: yes">
								<br />    </span>
				</span>
				<b>
						<span lang="EN-US" style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">SIMPLE</span>
				</b>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">：</span>
				<span lang="EN-US" style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">SIMPLE </span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">是</span>
				<span style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
						<span lang="EN-US">iBATIS </span>
				</span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">内置的</span>
				<span style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
						<span lang="EN-US">dataSource </span>
				</span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">实现，其中实现了一个简单的数据库连接池，当无容器提供</span>
				<span lang="EN-US" style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">DataSource </span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">服务时可以使用该选项，对应</span>
				<span style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
						<span lang="EN-US">iBATIS </span>
				</span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">实现类为</span>
				<span lang="EN-US" style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">com.ibatis.sqlmap.engine.datasource.SimpleDataSourceFactory</span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">。</span>
				<span lang="EN-US" style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 5.25pt; TEXT-INDENT: -5.25pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: -.5" align="left">
				<span lang="EN-US" style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">?<span style="mso-spacerun: yes">    </span><b>DBCP:</b></span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">基于</span>
				<span style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
						<span lang="EN-US">Apache DBCP </span>
				</span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">连接池</span>
				<span lang="EN-US" style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">API </span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">实现的</span>
				<span style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
						<span lang="EN-US">DataSource</span>
				</span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">，当无容器提供</span>
				<span style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
						<span lang="EN-US">DataSource </span>
				</span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">服务时，可以使用该选项，对应</span>
				<span lang="EN-US" style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">iBATIS </span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">实现类为</span>
				<span style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
						<span lang="EN-US">com.ibatis.sqlmap.engine.datasource.DbcpDataSourceFactory</span>
				</span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">。</span>
				<span lang="EN-US" style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-char-indent-count: 2.0" align="left">
				<span lang="EN-US" style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">?<b style="mso-bidi-font-weight: normal">JNDI</b></span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">：使用</span>
				<span style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
						<span lang="EN-US">J2EE </span>
				</span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">容器提供的</span>
				<span style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
						<span lang="EN-US">DataSource </span>
				</span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">实现，</span>
				<span lang="EN-US" style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">DataSource </span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">将通过指定的</span>
				<span style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
						<span lang="EN-US">JNDI Name </span>
				</span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">从容器中获取。对应</span>
				<span style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
						<span lang="EN-US">iBATIS </span>
				</span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">实现类为</span>
				<span style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
						<span lang="EN-US">com.ibatis.sqlmap.engine.datasource.JndiDataSourceFactory</span>
				</span>
				<span style="COLOR: black; FONT-FAMILY: 宋体; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">。</span>
				<span lang="EN-US" style="COLOR: black; FONT-FAMILY: Arial; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="Default" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">
						<span style="mso-spacerun: yes">   </span>
						<b style="mso-bidi-font-weight: normal">
								<span style="mso-spacerun: yes"> </span>
						</b>
				</span>
				<font face="宋体">
						<b style="mso-bidi-font-weight: normal">
								<span style="FONT-SIZE: 10.5pt; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">注意！</span>
						</b>
						<span style="FONT-SIZE: 10.5pt; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">每种</span>
				</font>
				<b style="mso-bidi-font-weight: normal">
						<span style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">
						</span>
				</b>
				<span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">dataSource </span>
				<span style="FONT-SIZE: 10.5pt; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">
						<font face="宋体">元素的</font>
				</span>
				<span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">property </span>
				<span style="FONT-SIZE: 10.5pt; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">
						<font face="宋体">都有不同的地方，不能光把</font>
				</span>
				<span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">type </span>
				<font face="宋体">
						<span style="FONT-SIZE: 10.5pt; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">名字改了了事。</span>
						<span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">
								<o:p>
								</o:p>
						</span>
				</font>
		</p>
		<p class="Default" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">
						<o:p> </o:p>
				</span>
		</p>
		<p class="Default" style="MARGIN: 0cm 0cm 0pt">
				<b style="mso-bidi-font-weight: normal">
						<span lang="EN-US" style="FONT-FAMILY: Arial">sqlMap </span>
				</b>
				<span style="FONT-SIZE: 10.5pt; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">
						<font face="宋体">元素定义了映射文件的存放位置，配置文件中可包含多个</font>
				</span>
				<span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">sqlMap </span>
				<span style="FONT-SIZE: 10.5pt; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">
						<font face="宋体">元素，比如：</font>
				</span>
				<span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">
						<o:p>
						</o:p>
				</span>
		</p>
		<p>
		</p>
		<table style="WIDTH: 514px; HEIGHT: 84px" height="84" cellspacing="0" cellpadding="0" width="514" border="1">
				<tbody>
						<tr>
								<td>
										<p>
												<font size="2">&lt;sqlMap resource="mapping/AutoMag1.xml"/&gt;<br />&lt;sqlMap resource="bo/mapping/AutoMag2.xml"/&gt;</font>
										</p>
										<p>
												<font size="2">&lt;sqlMap url="</font>
												<a href="file:///c:/eclipse/workspace/iBATISTest/src/bo/">
														<font size="2">file:///c:/eclipse/workspace/iBATISTest/src/bo/</font>
												</a>
												<font size="2">AutoMag2.xml "/&gt;<br />… …</font>
										</p>
								</td>
						</tr>
				</tbody>
		</table>
		<p class="Default" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0">
				<span style="FONT-SIZE: 10.5pt; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">
						<font face="宋体">你也许已发现，我只定义了单个映射文件。不错，和</font>
				</span>
				<span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">Hibernate </span>
				<span style="FONT-SIZE: 10.5pt; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">
						<font face="宋体">的一个表一个映射文件不同，</font>
				</span>
				<span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">iBATIS </span>
				<font face="宋体">
						<span style="FONT-SIZE: 10.5pt; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">的映射文件个数可以人为控制，颗粒度自己掌握。</span>
						<span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">
								<o:p>
								</o:p>
						</span>
				</font>
		</p>
		<p class="Default" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">
						<o:p> </o:p>
				</span>
		</p>
		<p class="Default" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">
						<span style="mso-spacerun: yes">    </span>
				</span>
				<span style="FONT-SIZE: 10.5pt; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">
						<font face="宋体">光有</font>
				</span>
				<span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">BO </span>
				<span style="FONT-SIZE: 10.5pt; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">
						<font face="宋体">和配置文件还不行，还要为本次测试创建测试类</font>
				</span>
				<span style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">
						<span lang="EN-US">AutoMag.java</span>
				</span>
				<span style="FONT-SIZE: 10.5pt; mso-hansi-font-family: Arial; mso-ascii-font-family: Arial; mso-bidi-font-family: Arial">
						<font face="宋体">。完整的布局如下所示：<br /></font>
				</span>
				<span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">
						<o:p>
								<br />                                        <img alt="" hspace="0" src="http://blog.csdn.net/images/blog_csdn_net/rosen/68948/r_structure.jpg" align="baseline" border="0" /><br /></o:p>
				</span>
		</p>
		<p class="Default" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt; mso-char-indent-count: 2.0">
				<span style="FONT-SIZE: 10.5pt">
						<font face="宋体">
								<br />    以下为<span lang="EN-US"> iBATIS SQL Maps 工作流程，对于理解概念很有帮助。大意是 1、你可以把 JavaBean、Map 类型、原始变量（或者它们的Wrapper Class）、XML 数据作为传入对象；2、通过配置文件载入映射文件；3、利用框架翻译成 JDBC 来访问数据库；4、执行结果可以是 JavaBean、Map 类型、原始变量（或者它们的Wrapper Class）、XML 数据。<br /></span></font>
				</span>
				<span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: Arial">
						<o:p>                       <br />       <img alt="" hspace="0" src="http://blog.csdn.net/images/blog_csdn_net/rosen/68948/r_map.jpg" align="baseline" border="0" /><br /><br /><strong><font face="宋体" color="#ff0000" size="2">（请注意！引用、转贴本文应注明原作者：Rosen Jiang 以及出处：</font></strong><a href="http://blog.csdn.net/rosen"><font face="宋体" color="#ff0000" size="2"><strong>http://blog.csdn.net/rosen</strong></font></a><font face="宋体" color="#ff0000" size="2"><strong>）</strong></font></o:p>
				</span>
		</p>
<img src ="http://www.blogjava.net/wangxq/aggbug/36809.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wangxq/" target="_blank">扭转乾坤</a> 2006-03-22 11:25 <a href="http://www.blogjava.net/wangxq/articles/36809.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]iBatis DAO入门与进阶</title><link>http://www.blogjava.net/wangxq/articles/36805.html</link><dc:creator>扭转乾坤</dc:creator><author>扭转乾坤</author><pubDate>Wed, 22 Mar 2006 03:17:00 GMT</pubDate><guid>http://www.blogjava.net/wangxq/articles/36805.html</guid><wfw:comment>http://www.blogjava.net/wangxq/comments/36805.html</wfw:comment><comments>http://www.blogjava.net/wangxq/articles/36805.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wangxq/comments/commentRss/36805.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wangxq/services/trackbacks/36805.html</trackback:ping><description><![CDATA[
		<div class="center">
				<h4>自：<a href="http://www.matrix.org.cn/resource/article/44/44058_iBatis+DAO.html">http://www.matrix.org.cn/resource/article/44/44058_iBatis+DAO.html</a></h4>
				<h4>摘要:</h4>这篇文章是关于如何一步一步的在项目中应用iBatis DAO框架的基础指导。我们将由如何把SQL Maps一文中的应用实例改为应用DAO框架入手。然后，我们要讨论DAO框架的构造。再下一步，我们关注事务管理是如何在DAO框架中得到支持的。最后一部分是关于如何建立自己的事务管理模块 </div>
		<div class="right">
				<div class="help">
						<b>
								<span style="FONT-SIZE: 20px">iBatis DAO入门与进阶</span>
						</b>
				</div>
		</div>
		<div class="overflow" id="text">
				<br />
				<center>作者：Sunil Patil</center>
				<br />
				<center>译者:<a href="http://www.matrix.org.cn/user.shtml?username=rotter_pal" target="_new">rotter_pal</a></center>
				<br />
				<br />
				<br />
				<br />
				<br />
				<span style="COLOR: red">版权声明：任何获得Matrix授权的网站，转载时请<b>务必</b>以超链接形式标明文章原始出处和作者信息及本声明</span>
				<br />作者:Sunil Patil;<a href="http://www.matrix.org.cn/user.shtml?username=rotter_pal" target="_new">rotter_pal</a><br />原文地址:<a href="http://www.onjava.com/pub/a/onjava/2005/08/10/ibatisdao.html" target="_new">http://www.onjava.com/pub/a/onjava/2005/08/10/ibatisdao.html</a><br />中文地址:<a href="http://www.matrix.org.cn/resource/article/44/44058_iBatis+DAO.html" target="_new">http://www.matrix.org.cn/resource/article/44/44058_iBatis+DAO.html</a><br />关键词： iBatis DAO<br /><br /><br />在核心J2EE模式中是这样介绍DAO模式的：为了建立一个健壮的J2EE应用，应该将所有对数据源的访问操作抽象封装在一个公共API中。用程序设计的语言来说，就是建立一个接口，接口中定义了此应用程序中将会用到的所有事务方法。在这个应用程序中，当需要和数据源进行交互的时候则使用这个接口，并且编写一个单独的类来实现这个接口在逻辑上对应这个特定的数据存储。<br /><br />比如考虑在iBatis: SQL Maps中的应用例子。这是一个Struts应用允许对一个关系表执行SELECT, INSERT, UPDATE和DELETE的SQL请求。在这个应用中，使用SQL Maps做持续性框架。现在我们要修改这个应用，将这个关系表储存在一个XML文件中而不是存在关系数据库中，或者使用Hibernate来实现SELECT请求，而用SQL Map来执行其他请求，因为Hibernate提供了对高速缓存更好的支持。这样的修改很难实现，或者即使我们能修改而实现了这个功能，也会是很混乱的解决方案。<br /><br /><br />对于这类问题更好的解决方法是建立一个ContactDAO接口，在这个接口中定义处理SELECT, INSERT, UPDATE, 和DELETE 请求的事务方法。然后根据不同的事务逻辑建立不同的类实现各个方法。所以可能会有一个类处理使用SQL Maps同关系表进行交互的情况，而另外一个类处理用XML文件存放关系表而不是关系数据库的情况，等等。在项目中，根据实际的需要从不同的ContactDAO中选择相应的实现。这种关系见图1：<br /><br /><img onmouseover="javascript:imgShowTip(this);" style="DISPLAY: inline" onclick="javascript:imgClick(this);" alt="image" src="http://www.matrix.org.cn/resource/upload/article/2005_12_18_224911_SOAyTyFzWZ.gif" onload="javascript:imgLoad(this);" border="0" resized="0" /><br />图1. ContactDAO 接口及实现<br /><br />iBatis DAO是由Apache主持的开源框架项目，主要目标是为了解决这类问题。它允许在工程中以DAO模式为基础建立应用。这就意味着可以建立一个XML文件，并声明XMLContactDAO.java是ContactDAO的实现类，这个类知道如何从XML文件中读写数据。SQLMapContactDAO则知道如何用SQL Maps作为持续化框架与关系表进行交互。在工程中，如果向DAO框架提交一个需要XML的ContactDAO请求，框架则会返回一个XMLContactDAO对象。同样的DAO框架提供了唯一的接口处理事务管理，这个接口能实现与数据的存储方式无关。它同样考虑了底层连接管理细节和初始化存储框架。<br /><br />这篇文章是关于如何一步一步的在项目中应用iBatis DAO框架的基础指导。我们将由如何把SQL Maps一文中的应用实例改为应用DAO框架入手。然后，我们要讨论DAO框架的构造。再下一步，我们关注事务管理是如何在DAO框架中得到支持的。最后一部分是关于如何建立自己的事务管理模块。<br /><br /><b><span style="FONT-SIZE: 16px">示例应用</span></b><br /><br />首先，我们将SQL Maps一文中的例子改为应用DAO框架。<br />1.        将ibatis-dao-2.jar文件复制到WEB-INF/lib目录下。<br />2.        在Java源程序的目录里新建一个如下的DAOMap.xml文件<br /><b>清单1：</b><br /><pre class="overflow">&lt;daoConfig&gt;<br /> &lt;context id="sqlmap"&gt;<br />  &lt;transactionManager type="SQLMAP"&gt;<br />  &lt;property name="SqlMapConfigResource" value=<br />   "com/sample/contact/dao/sqlmap/SqlMapConfig.xml"/&gt;<br />  &lt;/transactionManager&gt;<br />  &lt;dao interface="com.sample.contact.dao.ContactDAO"<br />   implementation=<br />   "com.sample.contact.dao.sqlmap.SQLMapContactDAO"/&gt;<br /> &lt;/context&gt;<br />&lt;/daoConfig&gt;</pre><br /><br />DAOMap.xml是发布iBatis DAO框架的配置文件。&lt;daoConfig&gt;是根元素，每个&lt;context&gt;元素描述了一种存储机制。在这个例子中只使用了SQL Maps来存储，所以我们这里只有一个&lt;context&gt;元素。每种存储机制必须包含一个&lt;transactionManager&gt;元素，这个元素描述连接它后面的数据存储所用的管理器，并且标记事务的界限。我们将在稍后再讨论transactionManager。<br /><br />&lt;context&gt;元素还包括一组DAO用于描述其他的存储管理机制。在这个例子中，我们将生成一个使用SQL Maps存储的ContactDAO，所以在配置文件中添加一个ie&lt;dao&gt;标记来定义SQLMapContactDAO。<br /><br />3.        建立ContactDAO.java,如下：<br /><b>清单2：</b><br /><pre class="overflow">public interface ContactDAO extends DAO {<br />    public int insertContact(Contact contact);<br />    public int updateContact(Contact contact);<br />    public Contact selectContact(int contactId);<br />    public int deleteContact(int contactId);<br />} </pre>   <br /><br />ContactDAO.java定义了用户和一个关系表进行交互所需要用到的所有事务处理方法。请注意到ContactDAO.java中的所有方法都将一个Contact对象作为参数，这是一个用来携带数据的数据传递对象。<br /><br /><br />4.        建立一个SQLMapContactDAO.java文件，如下<br /><b>清单3：</b><br /><pre class="overflow">public class SQLMapContactDAO extends<br /> SqlMapDaoTemplate implements ContactDAO {<br />  public SQLMapContactDAO(DaoManager arg0) {<br />      super(arg0);<br />  }<br />  public int deleteContact(int contactId) {<br />    return super.delete("deleteContact",<br />    new Integer(contactId));<br />  }<br />  public int insertContact(Contact contact) {<br />    Integer contactId =(Integer)super.insert<br />      ("insertContact",contact);<br />    return contact.getContactId();<br />  }<br />  public Contact selectContact(int contactId) {<br />    return (Contact)super.queryForObject("getContact",<br />      new Integer(contactId));<br />  }<br />  public int updateContact(Contact contact) {<br />    return super.update("updateContact",contact);<br />  }<br />}</pre><br /><br />SQLMapContactDAO是ContactDAO接口的具体实现，它用SQL Maps作为存储管理机制。注意到我们并没有写任何代码来或者初始化SQL Maps，或得到一个连接，或者在类中标注一个事务的界限。相反，我们继承SqlMapDaoTemplate.java类，它帮我们处理下层的、反复的操作。我们在SQLMapContactDAO类中需要考虑的唯一的事情就是事务处理逻辑。<br /><br />5.        修改ContactSelectAction.java类中的execute()方法，如下：<br /><b>清单4：</b><br /><pre class="overflow">Contact contactForm = (Contact) form;<br />Reader reader=<br />  Resources.getResourceAsReader("DAOMap.xml");<br />DaoManager daoManager =<br />  DaoManagerBuilder.buildDaoManager(reader);<br />ContactDAO contactDAO =<br />  (ContactDAO) daoManager.getDao(<br />ContactDAO.class,"sqlmap");<br /><br />request.setAttribute("contactDetail",<br />  contactDAO.selectContact(<br />    contactForm.getContactId()));</pre><br /><br />最后一步是修改ContactSelectAction类中的execute()方法，使它使用DAO框架。为了初始化DAO框架，我们需要一个为DAOMap.xml 准备一个Reader对象。iBatis框架为我们提供了方法Resources.getResourceAsReader()来读取资源。一旦有了Reader对象来读取DAOMap.xml，就能将它们读取至DAOManagerBuilder.buildDaoManager()，返回一个DaoManager实例，将来用于与DAO框架进行交互。从理论上来说，应该在项目启动的时候初始化DAO框架，在我们这个程序中，可以将这个模块放入Struts插件中，但是为了简化这个例子，我们将初始化模块放入execute方法中。<br /><br />有了DaoManager实例后，可以调用相应的接口和存储实现类(在&lt;context&gt;元素中的id属性值)的getDao()方法。在我们的例子中，需要一个SQLMapContactDAO的实例，所以以ContactDAO为接口名称，“sqlmap”为存储机制。一旦实现了SQLMapContactDAO实例，就可以在调用其中的事务方法。<br /><br />在最后的资源章节中可以下载到这个例子的源码。<br /><br /><b><span style="FONT-SIZE: 16px">DAO框架架构</span></b><br /><br />由于有了一个可以运行的示例，让我们得以粗略了解DAO框架是如何运作的。在图2表示的顺序图中演示了DAO的工作方式：<br /><br /><img onmouseover="javascript:imgShowTip(this);" style="DISPLAY: inline" onclick="javascript:imgClick(this);" height="450" alt="resized image" src="http://www.matrix.org.cn/resource/upload/article/2005_12_18_224916_OkWdiCStgH.gif" width="553" onload="javascript:imgLoad(this);" border="0" resized="1" /><br />图2. DAO顺序图<br /><br />在开始时，调用DaoManagerBuilder.buildDaoManager()并传入DAOMap.xml来初始化DAO框架。在这个方法中DAO框架会读取DAOMap.xml并且由此生成相应的DAOManager对象。这个对象包括了对支持的数据存储机制的描述。哪个接口会被实现，哪个是接口和存储机制结合的实现类？基本上这是和DAOMap.xml文件相等的 Java对象。<br /><br />当有了DAOManager对象，可以从中得到ContactDAO接口的SQL Map实例。DAO框架会返回一个包装了实现类的DaoProxy对象。在本例子中将给SQLMapContactDAO返回一个DaoProxy对象。这个DaoProxy对象允许DAO框架截获调用商业方法。本例中，当调用 contactDAO.selectContact()时，DAO框架会截获这个调用并检查事务处理是否已经开始执行，如果没有，它将调用事务管理器中的startTransaction()创建一个新的事务处理调用。如果处理已经开始，DaoProxy对象会调用事务中的SQLMapContactDAO中的selectContact()方法。当selectContact()调用返回的时候，DaoProxy对象截获返回并提交给事务。<br /><br />如果不希望事务在方法层上可见，或者希望在一个事务中调用多个不同的方法，则可在调用ContactDAO中的商业方法前调用daoManager.startTransaction()，然后在daoManager.startTransaction()执行完以后再提交商业方法。<br /><br />那么现在剩下要关心的事情就是那个模块负责存储机制的初始化并传递控制给存储机制。在这个例子中，就意味着由哪个模块负责将SqlMapConfig.xml的路径传递给SQL Map框架并给它初始化。同样意味着哪个模块负责和SQL Maps框架进行实际的交互。DAO框架为每种存储提供了Template类，在工程中，可以从这个Template类中继承实例类，并只要自己的方法中编写商业事务逻辑。然后将控制传递给这个模板类，它将负责和存储机制的交互。在我们的例子中调用super.queryForObject("getContact",new Integer(contactId))，意味着SqlMapDaoTemplate将负责SQL Maps的初始化和与之交互。<br /><br />初始化存储机制需要相关的一些信息，在例子中初始化需要SqlMapConfig.xml的路径，这个文件中包含驱动类的名字、JDBC URL、登陆信息之类的信息。这些特定的事务管理器需要的信息将会在DaoMap.xml文件中作为一个属性元素传递给管理器。下一节，我们将讨论DAO框架支持哪些事务管理器，每个管理器需要哪些初始化信息。<br /><br /><b><span style="FONT-SIZE: 16px">支持的存储管理机制</span></b><br /><br />DAO框架提供了内置的对一些存储管理机制的支持。为了使用其中的一个内置的transactionManagers，需要做两件事情：<br />1.在DAOMap.xml中增加一个&lt;transactionManager&gt;元素来声明对存储管理机制的支持。<br />2.在生成DAO实现类的时候为transactionManager继承适当的Template类。<br /><br />下面我们要研究内置transactionManagers并找出在应用程序中使用如何使用它们。<br /><br /><b>JDBC</b><br />如果不想使用任何存储框架，不想自己写JDBC代码，那么JDBC事务管理器是很好的选择。如果使用JDBC作为存储机制，则可以使用以下三种连接管理之一：<br /><br />SIMPLE：如果要使用iBatis'自己的连接池实例，可以把SIMPLE作为DataSource元素的值。将通常的JDBC属性(DriverManager类， JDBC URL，等等)传入作为Properties。在iBatis在线文档中查看更多的连接属性。<br /><b>清单5：</b><br /><pre class="overflow">&lt;transactionManager type="JDBC"&gt;<br /> &lt;property name="DataSource" value="SIMPLE"/&gt;<br /> &lt;property name="JDBC.Driver"<br />  value="com.ibm.db2j.jdbc.DB2jDriver"/&gt;<br /> &lt;property name="JDBC.ConnectionURL"<br />  value="jdbc:db2j:D:\cloudscape\wpsdb"/&gt;<br /> &lt;property name="JDBC.Username"<br />  value="db2admin"/&gt;<br /> &lt;property name="JDBC.Password"<br />  value="db2admin"/&gt;<br /> &lt;property name="JDBC.DefaultAutoCommit"<br />  value="true" /&gt;<br />&lt;/transactionManager&gt;</pre><br /><br /><br />DBCP：使用Apache DBCP作为连接管理。请查看DAO在线指导获得如何配置DBCP连接池的信息。<br /><br />JNDI：当要使用应用服务器的连接池，那么要做的是提供连接池的JNDI名，DAO框架则使用这个名称获得一个连接。<br /><b>清单6：</b><br /><pre class="overflow">&lt;transactionManager type="JDBC"&gt;<br /> &lt;property name="DataSource" value="JNDI"/&gt;<br /> &lt;property name="DBJndiContext"<br />  value="java:comp/env/jdbc/MyDataSource"/&gt;<br />&lt;/transactionManager&gt;</pre><br /><br /><br />然后要建立一个类继承JdbcDaoTemplate.java来实现事务方法借口。在示例中，我们建立了JDBCContactDAO.java。在事务方法中，可以调用getConnection()向父类请求连接。因为我们没有使用任何存储框架，所以我们只能建立并执行我们自己的SQL请求。<br /><b>清单7：</b><br /><pre class="overflow">public int updateContact(Contact contact) {<br /> try {<br />  Connection conn = getConnection();<br />  PreparedStatement updateStmt =<br />   conn.prepareStatement("UPDATE DB2ADMIN.CONTACT<br />    SET FIRSTNAME=?,LASTNAME=? WHERE CONTACTID=?");<br />  updateStmt.setString(1, contact.getFirstName());<br />  updateStmt.setString(2, contact.getLastName());<br />  updateStmt.setInt(3, contact.getContactId());<br />  return updateStmt.executeUpdate();<br /> } catch (SQLException ex) {<br />    throw new DaoException(ex);<br /> }<br />}</pre><br /><br />使用JDBC transactionManager的时候，DAO框架会调用Connection 对象中的commit和rollback方法来控制事务处理。所以事务会在Connection层被处理，而不参与全局事务处理。<br /><br /><b>JTA</b><br />如果项目是J2EE应用，那么使用应用服务器提供的连接池会更有利，因为它将比SIMPLE 或者DBCP 连接池有更好的性能。同样的，使用J2EE应用，RDBMS是唯一的处理源，除了RDBMS还需要包含JCA、MQ Server等功能。因为不能在连接层开始和处理事务，而要特别的在全局事务处理时在一个UserTransaction对象中调用begin()和commit()方法。所以对于这类请求，可以使用JTA 作为transctionManager,既可以向JNDI URL提供数据源连接池，也可以在里面包含UserTransaction对象。<br /><b>清单8：</b><br /><pre class="overflow">&lt;transactionManager type="JTA"&gt;<br /> &lt;property name="DBJndiContext"<br />  value="java:comp/env/jdbc/MyDataSource"/&gt;<br /> &lt;property name="UserTransaction"<br />  value="java:comp/env/UserTransaction"/&gt;<br />&lt;/transactionManager&gt;</pre><br /><br /><b>Hibernate</b><br />因为Hibernate是很常见的存储框架，iBatis DAO也提供了对它的支持。为了在项目中使用Hibernate，像下面那样在DAOMap.xml增加&lt;transactionManager&gt;元素：<br /><b>清单9：</b><br /><pre class="overflow">&lt;transactionManager type="HIBERNATE"&gt;<br /> &lt;property name="hibernate.dialect"<br />  value="net.sf.hibernate.dialect.Cloudscape"/&gt;<br /> &lt;property name="hibernate.connection.driver_class"<br />  value="com.ibm.db2j.jdbc.DB2jDriver"/&gt;<br /> &lt;property name="hibernate.connection.url"<br />  value="jdbc:db2j:D:\cloudscape\wpsdb"/&gt;<br /> &lt;property name="hibernate.connection.username"<br />  value="db2admin/&gt;<br /> &lt;property name="hibernate.connection.password"<br />  value="db2admin"/&gt;<br /> &lt;property name="class.1"<br />  value="com.sample.contact.Contact"/&gt;<br />&lt;/transactionManager&gt;</pre><br /><br /><br />同样的，需要建立一个DAO类继承HibernateDaoTemplate。在这个DAO内，可以通过调用getSession()方法来获得Hibernate Session对象的入口。<br /><br /><b>SQL MAP</b><br />请查看示例(在资源小节中)了解如何在项目中使用SQL Map存储框架的细节。<br /><br /><b>外部管理</b><br />外部的事务管理器允许事务处理在外部被DAO框架控制。这种行为有利于处理和非关系数据库数据源的交互。下一节，我们将讨论如何用DAO框架处理以XML文件作为数据源的情况。<br /><br /><br /><b><span style="FONT-SIZE: 16px">部署xml事务Map</span></b><br /><br />你可能也经常遇到这种情况：需要从xml中读取数据，而不是从RDBMS中读取，假想你正在从事一个银行项目，你并不能够直接接触到银行的数据库，所有的用户信息暂时都会通过一个XML文件传输给你，你必须使用这个XML文件进行开发，开发完毕再部署到真正的使用RDBMS的环境中，<br />这样的话，你需要做一下改变：<br /><br />1. 在DAOMap.xml 中增加对外部的transactionManager 的支持。<br />2. 新建一个XMLContactDAO.java文件:<br /><b>清单10：</b><br /><pre class="overflow">public class XMLContactDAO implements ContactDAO {<br /> public static final String<br />  CONTACTXMLNAME = "c:\\Contact.xml";<br /> public XMLContactDAO(DaoManager manager) {<br />    super(manager);<br /> }<br /> public int insertContact(Contact contact) {<br />  HashMap contactMap = loadChanges();<br />  if (contactMap.get(new Integer<br />    (contact.getContactId())) == null)<br />   contactMap.put(new<br />    Integer(contact.getContactId()), contact);<br />  saveChanges(contactMap);<br />  return contact.getContactId();<br /> }<br /> public Contact selectContact(int contactId) {<br />  HashMap contactMap = loadChanges();<br />  return (Contact) contactMap.get(<br />   new Integer(contactId));<br /> }<br /> public HashMap loadChanges() {<br />  HashMap contactMap = null;<br />  try {<br />   XStream xstream = new XStream(new DomDriver());<br />   xstream.alias("contact", Contact.class);<br />   contactMap =<br />    (HashMap) xstream.fromXML(<br />     new FileReader(CONTACTXMLNAME),HashMap.class);<br />  } catch (FileNotFoundException e) {<br />    e.printStackTrace();<br />    return new HashMap();<br />  }<br />  return contactMap;<br /> }<br /> public void saveChanges(HashMap contactMap) {<br />  try {<br />   XStream xstream = new XStream();<br />   xstream.alias("contact", Contact.class);<br />   xstream.toXML(contactMap,<br />    new FileWriter(CONTACTXMLNAME));<br />   } catch (IOException e) {<br />   e.printStackTrace();<br />  }<br /> }<br />}</pre><br /><br />这个例子中，XMLContactDAO实现了ContactDAO事务接口。因为我们使用了一个EXTERNAL事务管理器，所以不能使用任何已经存在的Template类。在我们的类中，我们使用XStream框架新建了两个简单的方法——loadChanges()和saveChanges()——实现对XML文件的读写。XStream是一个开源框架，实现将一个XML文件看作一个对象来读取，将对象保存为XML文件的功能。<br /><br /><b><span style="FONT-SIZE: 16px">结论</span></b><br />当今，有很多新的存储框架出现。这对于一个程序员既有好处也有坏处。好处是有更多的选择余地。坏处是因为你必须作出一个选择，更糟糕的是不得不在项目开始的时候就选择一种框架，这就意味着你可能不能完全清楚的了解项目的需求，或者不能完全确信这种框架是否能完全满足项目的需求。DAO是一种容易使用并且功能强大的框架能够处理存储机制的改变。你在前期作出了付出，但是它肯定会在最后对你有帮助的。<br /><br /><b><span style="FONT-SIZE: 16px">资源</span></b><br />·Matrix-Java开发者社区:<a href="http://www.matrix.org.cn/" target="_new">http://www.matrix.org.cn</a><br />·onjava.com:<a href="http://onjava.com/" target="_new">onjava.com</a><br />·这篇文章的示例代码：（注：与译文放入同一压缩包中）:<a href="http://www.onjava.com/onjava/2005/08/10/examples/SampleDAO.zip" target="_new">http://www.onjava.com/onjava/2005/08/10/examples/SampleDAO.zip</a><br />·iBatis主页：<a href="http://ibatis.apache.org/" target="_new">http://ibatis.apache.org/</a><br />·核心 J2EE 模式：数据存储对象：<a href="http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html" target="_new">http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html</a><br />·Hibernate主页 (或者CodeZoo: Hibernate) ：<a href="http://www.hibernate.org/" target="_new">http://www.hibernate.org/</a><br />·使用XStream序列化Java对象：<a href="http://www.xml.com/pub/a/2004/08/18/xstream.html" target="_new">http://www.xml.com/pub/a/2004/08/18/xstream.html</a>        <br />·XStream (或者 CodeZoo: XStream) ：http://xstream.codehaus.org/ ( http://www.codezoo.com/ ：http://www.codezoo.com/pub/component/3551 )<br /><br /><b><span style="FONT-SIZE: 16px">关于作者</span></b><br />Sunil Patil对J2EE技术领域的研究超过5年时间。他对感兴趣的领域是与对象相关的映射工具、UI框架和Portals<br /><br /><br /><br /></div>
<img src ="http://www.blogjava.net/wangxq/aggbug/36805.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wangxq/" target="_blank">扭转乾坤</a> 2006-03-22 11:17 <a href="http://www.blogjava.net/wangxq/articles/36805.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]ibatis初步</title><link>http://www.blogjava.net/wangxq/articles/36374.html</link><dc:creator>扭转乾坤</dc:creator><author>扭转乾坤</author><pubDate>Mon, 20 Mar 2006 08:46:00 GMT</pubDate><guid>http://www.blogjava.net/wangxq/articles/36374.html</guid><wfw:comment>http://www.blogjava.net/wangxq/comments/36374.html</wfw:comment><comments>http://www.blogjava.net/wangxq/articles/36374.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wangxq/comments/commentRss/36374.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wangxq/services/trackbacks/36374.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 自: http://dev.csdn.net/article/79/79164.shtm&lt;!--[if !supportLists]--&gt;1.   &lt;!--[endif]--&gt;总体描述以Eclipse为例说明ibatis用法，数据库为MS SQL2000，ibatis版本为2.0, jDK1.5, 以对一个用户信息表 user_info的插入、查询（单条记录），多记录查询为...&nbsp;&nbsp;<a href='http://www.blogjava.net/wangxq/articles/36374.html'>阅读全文</a><img src ="http://www.blogjava.net/wangxq/aggbug/36374.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wangxq/" target="_blank">扭转乾坤</a> 2006-03-20 16:46 <a href="http://www.blogjava.net/wangxq/articles/36374.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>