ibatis 增刪改查

<p>iBatis是像Hibernate, JDO和EJB同樣的數據持久框架,它將對象映射爲SQL語句.它是個輕量級的框架而且持久性API適合持久化POJO.iBatis也與Hibernate, JDO不一樣,由於它使用存儲過程和現有的SQL來處理數據庫. </p> <p>本節咱們將向你講述如何配置iBatis來運行一個小型程序.既然一次性將全部知識所有解釋很難,咱們索性把本教程分爲幾個單獨的例子來陳述.該例是關於如何從數據庫讀取數據並將結果顯示在你的命令提示符上.在第二個例子中你將會到如何添加更多的數據到數據庫中,在此以後的第三個例子將會向你展現如何經過iBatis從記錄中刪除數據. </p> <p>如今的第一個例子將會向你展現如何從數據庫中讀取記錄,咱們須要一個數據庫來執行查詢,因此咱們使用Oracle做爲這個例子的數據庫. </p> <p>這裏咱們將要檢索一些人的contact的信息, contact的表結構給出以下 : </p><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">-- Create table</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=create&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">create</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=table&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">table</a> contact </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">( </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> id NUMBER(4) <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=not&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">not</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=null&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">null</a>, </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> firstName <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=VARCHAR&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">VARCHAR</a>2(10), </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> lastName <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=VARCHAR&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">VARCHAR</a>2(9), </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> email <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=VARCHAR&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">VARCHAR</a>2(9) </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">) </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">tablespace USERS </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> pctfree 10 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> initrans 1 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> maxtrans 255 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> storage </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> ( </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> initial 64K </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> minextents 1 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> maxextents unlimited </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> );</pre></pre> <p>根據Contact表咱們須要建立一個POJO類,在咱們的例子中,數據庫scott有一個表Contact,包括四個字段 :</p> <ul> <li>id <li>firstName <li>lastName <li>email </li></ul> <p>Contact.java</p><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">package</span> com.ztesoft.domain; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">/** </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * @author yangshangwei </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> */</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Contact { </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">private</span> String firstName; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">private</span> String lastName; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">private</span> String email; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">private</span> <span style="color: #0000ff">int</span> id; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">public</span> Contact() { </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> } </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">public</span> Contact(String firstName, String lastName, String email) { </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">this</span>.firstName = firstName; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">this</span>.lastName = lastName; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">this</span>.email = email; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> } </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">public</span> String getFirstName() { </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">return</span> firstName; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> } </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> setFirstName(String firstName) { </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">this</span>.firstName = firstName; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> } </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">public</span> String getLastName() { </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">return</span> lastName; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> } </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> setLastName(String lastName) { </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">this</span>.lastName = lastName; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> } </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">public</span> String getEmail() { </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">return</span> email; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> } </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> setEmail(String email) { </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">this</span>.email = email; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> } </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> getId() { </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">return</span> id; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> } </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> setId(<span style="color: #0000ff">int</span> id) { </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> <span style="color: #0000ff">this</span>.id = id; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> } </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre></pre> <p><strong>搭建iBatis 開發環境:</strong></p> <p><a href="http://static.oschina.net/uploads/img/201308/25010202_6cmk.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://static.oschina.net/uploads/img/201308/25010206_o4YV.png" width="210" height="244"></a></p> <p>1 、導入相關的jar 包,ibatis-2.3.0.677.jar 、ojdbc14-10.2.0.2.jar</p> <p>2 、編寫配置文件:</p> <p>Jdbc 鏈接的屬性文件SqlMap.properties</p> <p>總配置文件, <a name="OLE_LINK1">SqlMapConfig.xml</a></p> <p>關於每一個實體的映射文件(Map 文件)</p> <p><font color="#ff0000">sqlMap.properties</font></p><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">driver=oracle.jdbc.driver.OracleDriver </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">url=jdbc:oracle:thin:@localhost:1521:orcl </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">username=scott </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">password=tiger</pre></pre>java

<p><a name="OLE_LINK1"><font color="#ff0000">SqlMapConfig.xml</font></a></p><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;?</span>xml version="1.0" encoding="UTF-8"<span style="color: #0000ff">?&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span>!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">"http://ibatis.apache.org/dtd/sql-map-config-2.dtd"<span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">sqlMapConfig</span><span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">&lt;!-- 引用JDBC屬性的配置文件 --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">properties</span> <span style="color: #ff0000">resource</span>=<span style="color: #0000ff">"SqlMap.properties"</span> <span style="color: #0000ff">/&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">settings</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #ff0000">cacheModelsEnabled</span>=<span style="color: #0000ff">"true"</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #ff0000">enhancementEnabled</span>=<span style="color: #0000ff">"true"</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #ff0000">lazyLoadingEnabled</span>=<span style="color: #0000ff">"true"</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #ff0000">errorTracingEnabled</span>=<span style="color: #0000ff">"true"</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #ff0000">maxRequests</span>=<span style="color: #0000ff">"32"</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #ff0000">maxSessions</span>=<span style="color: #0000ff">"10"</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #ff0000">maxTransactions</span>=<span style="color: #0000ff">"5"</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #ff0000">useStatementNamespaces</span>=<span style="color: #0000ff">"false"</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">/&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">&lt;!-- 使用JDBC的事務管理 --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">transactionManager</span> <span style="color: #ff0000">type</span>=<span style="color: #0000ff">"JDBC"</span><span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">&lt;!-- 數據源 --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">dataSource</span> <span style="color: #ff0000">type</span>=<span style="color: #0000ff">"SIMPLE"</span><span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span>=<span style="color: #0000ff">"JDBC.Driver"</span> <span style="color: #ff0000">value</span>=<span style="color: #0000ff">"${driver}"</span> <span style="color: #0000ff">/&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span>=<span style="color: #0000ff">"JDBC.ConnectionURL"</span> <span style="color: #ff0000">value</span>=<span style="color: #0000ff">"${url}"</span> <span style="color: #0000ff">/&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span>=<span style="color: #0000ff">"JDBC.Username"</span> <span style="color: #ff0000">value</span>=<span style="color: #0000ff">"${username}"</span> <span style="color: #0000ff">/&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span>=<span style="color: #0000ff">"JDBC.Password"</span> <span style="color: #ff0000">value</span>=<span style="color: #0000ff">"${password}"</span> <span style="color: #0000ff">/&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span>=<span style="color: #0000ff">"Pool.MaximumActiveConnections"</span><span style="color: #ff0000">value</span>=<span style="color: #0000ff">"10"</span><span style="color: #0000ff">/&gt;</span> <span style="color: #008000">&lt;!-- 數據庫鏈接池可維持的最大容量。(SIMPLE,DBCP) --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span>=<span style="color: #0000ff">"Pool.MaximumIdleConnections"</span> <span style="color: #ff0000">value</span>=<span style="color: #0000ff">"5"</span><span style="color: #0000ff">/&gt;</span> <span style="color: #008000">&lt;!-- 數據庫鏈接池中容許的掛起(idle)鏈接數。(SIMPLE,DBCP) --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span>=<span style="color: #0000ff">"Pool.MaximumCheckoutTime"</span><span style="color: #ff0000">value</span>=<span style="color: #0000ff">"120000"</span><span style="color: #0000ff">/&gt;</span> <span style="color: #008000">&lt;!-- 數據庫聯接池中,鏈接被某個任務所容許佔用的最大時間,若是超過這個時間限定,鏈接將被強制收回。(毫秒)(SIMPLE) --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span>=<span style="color: #0000ff">"Pool.TimeToWait"</span> <span style="color: #ff0000">value</span>=<span style="color: #0000ff">"500"</span><span style="color: #0000ff">/&gt;</span> <span style="color: #008000">&lt;!--當線程試圖從鏈接池中獲取鏈接時,鏈接池中無可用鏈接可供使用,此時線程將進入等待狀態,直到池中出現空閒鏈接。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> 此參數設定了線程所容許等待的最長時間。(毫秒)(SIMPLE) --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span>=<span style="color: #0000ff">"Pool.PingQuery"</span> <span style="color: #ff0000">value</span>=<span style="color: #0000ff">"select 1 from contact"</span><span style="color: #0000ff">/&gt;</span> <span style="color: #008000">&lt;!-- 數據庫鏈接狀態檢測語句。某些數據庫在鏈接在某段時間持續處於空閒狀態時會將其斷開。而鏈接池管理器將經過此語句檢測池中鏈接是否可用。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> 檢測語句應該是一個最簡化的無邏輯SQL。如「select 1 from t_user」 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> 若是執行此語句成功,鏈接池管理器將認爲此鏈接處於可用狀態。(SIMPLE) --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span>=<span style="color: #0000ff">"Pool.PingEnabled"</span> <span style="color: #ff0000">value</span>=<span style="color: #0000ff">"false"</span><span style="color: #0000ff">/&gt;</span> <span style="color: #008000">&lt;!-- 是否容許檢測鏈接狀態。(SIMPLE) --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span>=<span style="color: #0000ff">"Pool.PingConnectionsOlderThan"</span> <span style="color: #ff0000">value</span>=<span style="color: #0000ff">"1"</span><span style="color: #0000ff">/&gt;</span> <span style="color: #008000">&lt;!-- 對持續鏈接時間超過設定值(毫秒)的鏈接進行檢測 (SIMPLE)--&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span>=<span style="color: #0000ff">"Pool.PingConnectionsNotUsedFor"</span><span style="color: #ff0000">value</span>=<span style="color: #0000ff">"1"</span><span style="color: #0000ff">/&gt;</span> <span style="color: #008000">&lt;!-- 對空閒超過設定值(毫秒)的鏈接進行檢測。(SIMPLE) --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">dataSource</span><span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">transactionManager</span><span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">&lt;!-- 這裏能夠寫多個實體的映射文件 --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">sqlMap</span> <span style="color: #ff0000">resource</span>=<span style="color: #0000ff">"com/ztesoft/domain/Contact.xml"</span> <span style="color: #0000ff">/&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">sqlMapConfig</span><span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">&lt;!--settings節點 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> cacheModelsEnabled 是否啓用SqlMapClient 上的緩存機制。建議設爲"true" </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> enhancementEnabled 是否針對POJO 啓用字節碼加強機制以提高getter/setter 的調用效能,避免使用Java Reflect 所帶來的性能開銷。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> 同時,這也爲Lazy Loading 帶來了極大的性能提高。建議設爲"true" </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> errorTracingEnabled 是否啓用錯誤日誌,在開發期間建議設爲"true"以方便調試 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> lazyLoadingEnabled 是否啓用延遲加載機制,建議設爲"true" </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> maxRequests 最大併發請求數(Statement 併發數) </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> maxTransactions 最大併發事務數 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> maxSessions 最大Session 數。即當前最大容許的併發SqlMapClient 數。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> maxSessions 設定必須介於 maxTransactions 和maxRequests 之間. </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> useStatementNamespaces 是否使用Statement命名空間。這裏的命名空間指的是映射文件中,sqlMap 節點的namespace 屬性,如在上例中針對contact </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">表的映射文件Contact.xml中sqlMap 節點: &lt;sqlMap namespace="Contact"&gt; 這裏,指定了此sqlMap 節點下定義的操做均從 屬於"Contact"命名空間。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">在useStatementNamespaces="true"的情 況下,Statement 調用需追加命名空間,如: sqlMap.update("User.updateUser",use r); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">不然直接經過Statement 名稱調用即 sqlMap.update("updateUser",user); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">但請注意此時須要保證全部映射 定義無重名。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">--&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">&lt;!-- transactionManager節點 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">節點定義了ibatis 的事務管理器,目前提供瞭如下幾種選擇: </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">. JDBC </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">經過傳統JDBC Connection.commit/rollback 實現事務支持。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">. JTA </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">使用容器提供的JTA 服務實現全局事務管理。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">. EXTERNAL </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">外部事務管理, 如在EJB 中使用ibatis,經過EJB 的部署配置便可實現自動的事務管理機制。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">此時ibatis 將把全部事務委託給外部容器進行管理。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">此外,經過Spring 等輕量級容器實現事務的配置化管理也是一個不錯的選擇。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">關於結合容器實現事務管理,參見「高級特性」中的描述。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">--&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">&lt;!-- dataSource 節點 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">dataSource 從屬於transactionManager 節點,用於設定ibatis 運行期使用的DataSource 屬性。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">type 屬性: dataSource 節點的type屬性指定了dataSource 的實現類型。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">可選項目: </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">. SIMPLE: </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">SIMPLE 是ibatis 內置的dataSource 實現,其中實現了一個簡單的 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">數據庫鏈接池機制,對應ibatis 實現類爲 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">com.ibatis.sqlmap.engine.datasource.SimpleDataSourceFactory 。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">. DBCP: </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">基於Apache DBCP 鏈接池組件實現的DataSource 封裝,當無容器提 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">供DataSource 服務時,建議使用該選項,對應ibatis 實現類爲 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">com.ibatis.sqlmap.engine.datasource.DbcpDataSourceFactory 。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">. JNDI: </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">使用J2EE 容器提供的DataSource 實現,DataSource 將經過指定 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">的JNDI Name 從容器中獲取。對應ibatis 實現類爲 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">com.ibatis.sqlmap.engine.datasource.JndiDataSourceFactory。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">dataSource 的子節點說明(SIMPLE&amp;DBCP): </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">參數描述 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">JDBC.Driver JDBC 驅動。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">如:oracle.jdbc.driver.OracleDriver </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">JDBC.ConnectionURL 數據庫URL 。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">如:jdbc:oracle:thin:@localhost:1521:orcl </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">若是用的是SQLServer JDBC Driver,須要在url 後追加SelectMethod=Cursor 以得到JDBC 事務的多Statement 支持。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">JDBC.Username 數據庫用戶名 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">JDBC.Password 數據庫用戶密碼 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">Pool.MaximumActiveConnections 數據庫鏈接池可維持的最大容量。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">Pool.MaximumIdleConnections 數據庫鏈接池中容許的掛起(idle)鏈接數。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">以上子節點適用於SIMPLE 和DBCP 模式,分別針對SIMPLE 和DBCP 模式的DataSource 私有配置節點以下: </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">SIMPLE: </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">參數描述 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">Pool.MaximumCheckoutTime 數據庫聯接池中,鏈接被某個任務所容許佔用的最大時間,若是超過這個時間限定,鏈接將被強制收回。(毫秒) </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">Pool.TimeToWait 當線程試圖從鏈接池中獲取鏈接時,鏈接池中無可用鏈接可供使用,此時線程將進入等待狀態,直到池中出現空閒鏈接。此參數設定了線程所容許等待的最長時間。(毫秒) </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">Pool.PingQuery 數據庫鏈接狀態檢測語句。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">某些數據庫在鏈接在某段時間持續處於空閒狀態 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">時會將其斷開。而鏈接池管理器將經過此語句檢 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">測池中鏈接是否可用。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">檢測語句應該是一個最簡化的無邏輯SQL 。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">如「select 1 from t_user」, 若是執行此語句 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">成功,鏈接池管理器將認爲此鏈接處於可用狀態。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">Pool.PingEnabled 是否容許檢測鏈接狀態。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">Pool.PingConnectionsOlderThan 對持續鏈接時間超過設定值(毫秒)的鏈接進行檢測。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">Pool.PingConnectionsNotUsedFor對空閒超過設定值(毫秒)的鏈接進行檢測。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">DBCP: </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">參數描述 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">Pool.MaximumWait 當線程試圖從鏈接池中獲取鏈接時,鏈接池中無可用鏈接可供使用,此時線程將進入等待狀態,直到池中出現空閒鏈接。此參數設定了線程所容許等待的最長時間。(毫秒) </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">Pool.ValidationQuery 數據庫鏈接狀態檢測語句。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">某些數據庫在鏈接在某段時間持續處於空閒狀態時會將其斷開。而鏈接池管理器將經過此語句檢測池中鏈接是否可用。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">檢測語句應該是一個最簡化的無邏輯SQL 。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">如「select 1 from contact」, 若是執行此語句成功,鏈接池管理器將認爲此鏈接處於可用狀態。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">Pool.LogAbandoned 當數據庫鏈接被廢棄時,是否打印日誌。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">Pool.RemoveAbandonedTimeout 數據庫鏈接被廢棄的最大超時時間 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">Pool.RemoveAbandoned 當鏈接空閒時間超過RemoveAbandonedTimeout 時,是否將其廢棄。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">JNDI </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">因爲大部分配置是在應用服務器中進行,所以ibatis 中的配置相對簡單,下面 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">是分別使用JDBC 和JTA 事務管理的JDNI 配置: </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">使用JDBC 事務管理的JNDI DataSource 配置 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">&lt;transactionManager type="JDBC" &gt; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">&lt;dataSource type="JNDI"&gt; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">&lt;property name="DataSource" value="java:comp/env/jdbc/myDataSource"/&gt; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">&lt;/dataSource&gt; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">&lt;/transactionManager&gt; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">&lt;transactionManager type="JTA" &gt; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">&lt;property name="UserTransaction" value="java:/ctx/con/UserTransaction"/&gt; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">&lt;dataSource type="JNDI"&gt; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">&lt;property name="DataSource" value="java:comp/env/jdbc/myDataSource"/&gt; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">&lt;/dataSource&gt; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">&lt;/transactionManager&gt; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre></pre> <p><font color="#ff0000">Contact.xml</font></p><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;?</span>xml version="1.0" encoding="UTF-8" <span style="color: #0000ff">?&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span>!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">"http://ibatis.apache.org/dtd/sql-map-2.dtd"<span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">sqlMap</span> <span style="color: #ff0000">namespace</span>=<span style="color: #0000ff">"Contact"</span><span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">&lt;!-- 經過typeAlias使得咱們在下面使用Contact實體類的時候不須要寫包名 --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">typeAlias</span> <span style="color: #ff0000">alias</span>=<span style="color: #0000ff">"Contact"</span> <span style="color: #ff0000">type</span>=<span style="color: #0000ff">"com.ztesoft.domain.Contact"</span> <span style="color: #0000ff">/&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">&lt;!-- 這樣之後改了sql,就不須要去改java代碼了 --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">&lt;!-- id表示select裏的sql語句,resultClass表示返回結果的類型 --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">select</span> <span style="color: #ff0000">id</span>=<span style="color: #0000ff">"selectAllContact"</span> <span style="color: #ff0000">resultClass</span>=<span style="color: #0000ff">"Contact"</span><span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">select * from contact </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">select</span><span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">&lt;!-- parameterClass表示參數的內容 --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">&lt;!-- #表示這是一個外部調用的須要傳進的參數,能夠理解爲佔位符 --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">select</span> <span style="color: #ff0000">id</span>=<span style="color: #0000ff">"selectContactById"</span> <span style="color: #ff0000">parameterClass</span>=<span style="color: #0000ff">"int"</span> <span style="color: #ff0000">resultClass</span>=<span style="color: #0000ff">"Contact"</span><span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">select firstName ,lastName ,email from contact where id=#id# </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">select</span><span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">&lt;!-- 注意這裏的resultClass類型,使用Student類型取決於queryForList仍是queryForObject --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">select</span> <span style="color: #ff0000">id</span>=<span style="color: #0000ff">"selectContactByName"</span> <span style="color: #ff0000">parameterClass</span>=<span style="color: #0000ff">"String"</span> <span style="color: #ff0000">resultClass</span>=<span style="color: #0000ff">"Contact"</span><span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">select id,email,lastName from contact where firstName like '%$name$%' </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">select</span><span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">insert</span> <span style="color: #ff0000">id</span>=<span style="color: #0000ff">"insertContact"</span> <span style="color: #ff0000">parameterClass</span>=<span style="color: #0000ff">"Contact"</span> <span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">selectKey</span> <span style="color: #ff0000">resultClass</span>=<span style="color: #0000ff">"int"</span> <span style="color: #ff0000">keyProperty</span>=<span style="color: #0000ff">"id"</span><span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">SELECT contact_sequence.NEXTVAL AS id FROM DUAL </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">&lt;!-- 這裏須要說明一下不一樣的數據庫主鍵的生成,對各自的數據庫有不一樣的方式: --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">&lt;!-- mysql:SELECT LAST_INSERT_ID() AS VALUE --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">&lt;!-- mssql:select @@IDENTITY as value --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">&lt;!-- oracle:SELECT STOCKIDSEQUENCE.NEXTVAL AS VALUE FROM DUAL --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">&lt;!-- 還有一點須要注意的是不一樣的數據庫生產商生成主鍵的方式不同,有些是預先生成 (pre-generate)主鍵的,如Oracle和PostgreSQL。 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">有些是過後生成(post-generate)主鍵的,如MySQL和SQL Server 因此若是是Oracle數據庫,則須要將selectKey寫在insert以前 --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">selectKey</span><span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">insert into contact(id,firstName,lastName,email) values(#id#,#firstName#,#lastName#,#email#) </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">insert</span><span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">update</span> <span style="color: #ff0000">id</span>=<span style="color: #0000ff">"updateContact"</span> <span style="color: #ff0000">parameterClass</span>=<span style="color: #0000ff">"Contact"</span> <span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">update Contact set firstName=#firstName#, lastName=#lastName#,email=#email# where id=#id# </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">update</span> <span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">delete</span> <span style="color: #ff0000">id</span>=<span style="color: #0000ff">"deleteContactById"</span> <span style="color: #ff0000">parameterClass</span>=<span style="color: #0000ff">"int"</span><span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">&lt;!-- #id#裏的id能夠隨意取,可是上面的insert則會有影響,由於上面的name會從Student裏的屬性裏去查找 --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">&lt;!-- 咱們也能夠這樣理解,若是有#佔位符,則ibatis會調用parameterClass裏的屬性去賦值 --&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">delete from contact where id=#id# </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">delete</span><span style="color: #0000ff">&gt;</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">sqlMap</span><span style="color: #0000ff">&gt;</span></pre></pre> <p><font color="#ff0000">ContactDao.java</font></p><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">package</span> com.ztesoft.dao; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> java.util.List; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> com.ztesoft.domain.Contact; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">/** </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * @author yangshangwei </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> */</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> ContactDao { </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">/** </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * 添加學生信息 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * @param contact </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * 學生實體 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * @return 返回是否添加成功 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> */</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span> addContact(Contact contact); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">/** </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * 根據學生id刪除學生信息 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * @param id </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * 學生id </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * @return 刪除是否成功 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> */</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span> deleteContactById(<span style="color: #0000ff">int</span> id); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">/** </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * 更新學生信息 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * @param Contact </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * 學生實體 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * @return 更新是否成功 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> */</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span> updateContact(Contact contact); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">/** </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * 查詢所有學生信息 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * @return 返回學生列表 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> */</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">public</span> List&lt;Contact&gt; selectAllContact(); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">/** </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * 根據學生姓名模糊查詢學生信息 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * @param name </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * 學生姓名 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * @return 學生信息列表 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> */</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">public</span> List&lt;Contact&gt; selectContactByName(String name); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">/** </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * 根據學生id查詢學生信息 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * @param id </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * 學生id </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * @return 學生對象 </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> */</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">public</span> Contact selectContactById(<span style="color: #0000ff">int</span> id); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre></pre> <p><font color="#ff0000">ContactDaoImpl.java</font></p><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">package</span> com.ztesoft.daoImpl; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> java.io.IOException; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> java.io.Reader; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> java.sql.SQLException; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> java.util.List; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> org.apache.log4j.Logger; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> com.ibatis.common.resources.Resources; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> com.ibatis.sqlmap.client.SqlMapClient; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> com.ibatis.sqlmap.client.SqlMapClientBuilder; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> com.ztesoft.dao.ContactDao; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> com.ztesoft.domain.Contact; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ContactDaoImpl <span style="color: #0000ff">implements</span> ContactDao { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> Logger logger = Logger.getLogger(Contact.<span style="color: #0000ff">class</span>); </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> SqlMapClient sqlMapClient = <span style="color: #0000ff">null</span>; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">// 讀取配置文件</span> </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">static</span> { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">try</span> { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">Reader reader = Resources.getResourceAsReader("<span style="color: #8b0000">SqlMapConfig.xml</span>"); </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader); </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">reader.close(); </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} <span style="color: #0000ff">catch</span> (IOException e) { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">e.printStackTrace(); </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">@Override </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span> addContact(Contact contact) { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">// TODO Auto-generated method stub</span> </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">boolean</span> flag = <span style="color: #0000ff">false</span>; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">Object obj = <span style="color: #0000ff">null</span>; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">try</span> { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">obj = sqlMapClient.insert("<span style="color: #8b0000">insertContact</span>",contact); </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} <span style="color: #0000ff">catch</span> (SQLException e) { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">e.printStackTrace(); </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">if</span> (obj != <span style="color: #0000ff">null</span>) { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">flag = <span style="color: #0000ff">true</span>; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">return</span> flag; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">@Override </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span> deleteContactById(<span style="color: #0000ff">int</span> id) { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">boolean</span> flag = <span style="color: #0000ff">false</span>; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">Object obj = <span style="color: #0000ff">null</span>; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">try</span> { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">obj = sqlMapClient.delete("<span style="color: #8b0000">deleteContactById</span>", id); </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} <span style="color: #0000ff">catch</span> (SQLException e) { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">// TODO Auto-generated catch block</span> </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">e.printStackTrace(); </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">if</span> (obj != <span style="color: #0000ff">null</span>) { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">flag = <span style="color: #0000ff">true</span>; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">return</span> flag; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">@Override </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">public</span> List&lt;Contact&gt; selectAllContact() { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">// TODO Auto-generated method stub</span> </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">List&lt;Contact&gt; contacts = <span style="color: #0000ff">null</span>; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">try</span> { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">contacts = sqlMapClient.queryForList("<span style="color: #8b0000">selectAllContact</span>"); </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} <span style="color: #0000ff">catch</span> (SQLException e) { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">// TODO Auto-generated catch block</span> </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">e.printStackTrace(); </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">return</span> contacts; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">@Override </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">public</span> Contact selectContactById(<span style="color: #0000ff">int</span> id) { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">// TODO Auto-generated method stub</span> </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">Contact contact = <span style="color: #0000ff">null</span>; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">try</span> { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">contact = (Contact) sqlMapClient.queryForObject( </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">"<span style="color: #8b0000">selectContactById</span>", id); </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} <span style="color: #0000ff">catch</span> (SQLException e) { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">logger.debug("<span style="color: #8b0000">selectContactById發生異常</span>", e); </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">e.printStackTrace(); </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">return</span> contact; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">@SuppressWarnings("<span style="color: #8b0000">unchecked</span>") </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">@Override </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">public</span> List&lt;Contact&gt; selectContactByName(String name) { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">List&lt;Contact&gt; contacts = <span style="color: #0000ff">null</span>; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">try</span> { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">contacts = sqlMapClient.queryForList("<span style="color: #8b0000">selectContactByName</span>", name); </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} <span style="color: #0000ff">catch</span> (SQLException e) { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">logger.debug("<span style="color: #8b0000">selectContactByName發生異常</span>", e); </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">e.printStackTrace(); </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">return</span> contacts; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">@Override </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span> updateContact(Contact contact) { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">boolean</span> flag = <span style="color: #0000ff">false</span>; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">Object <span style="color: #0000ff">object</span> = <span style="color: #0000ff">false</span>; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">try</span> { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">object</span> = sqlMapClient.update("<span style="color: #8b0000">updateContact</span>", contact); </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">System.out.println("<span style="color: #8b0000">更新學生信息的返回值:</span>" + <span style="color: #0000ff">object</span> + "<span style="color: #8b0000">,返回影響的行數</span>"); </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} <span style="color: #0000ff">catch</span> (SQLException e) { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">logger.debug("<span style="color: #8b0000">update發生了異常</span>", e); </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">e.printStackTrace(); </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">if</span> (<span style="color: #0000ff">object</span> != <span style="color: #0000ff">null</span>) { </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">flag = <span style="color: #0000ff">true</span>; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">return</span> flag; </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre></pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre></pre><pre><font color="#ff0000" size="4">IbatisExample.java</font></pre><pre>&nbsp;</pre><pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">package</span> com.ztesoft.test; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> java.io.IOException; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> java.io.Reader; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> java.sql.SQLException; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> java.util.List; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> org.apache.log4j.Logger; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> com.ibatis.common.resources.Resources; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> com.ibatis.sqlmap.client.SqlMapClient; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> com.ibatis.sqlmap.client.SqlMapClientBuilder; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> com.ztesoft.daoImpl.ContactDaoImpl; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">import</span> com.ztesoft.domain.Contact; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">/** </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * @author yangshangwei </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> * </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"> */</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> IbatisExample { </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> Logger logger = Logger.getLogger(IbatisExample.<span style="color: #0000ff">class</span>); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) <span style="color: #0000ff">throws</span> IOException, SQLException { </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">ContactDaoImpl contactDaoImpl = <span style="color: #0000ff">new</span> ContactDaoImpl(); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">Reader reader = Resources.getResourceAsReader("<span style="color: #8b0000">SqlMapConfig.xml</span>"); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #008000">// Output all contacts</span> </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">List&lt;Contact&gt; contacts = (List&lt;Contact&gt;) sqlMap.queryForList("<span style="color: #8b0000">selectAllContact</span>"); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">Contact contact = <span style="color: #0000ff">null</span>; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"><span style="color: #0000ff">for</span> (Contact c : contacts) { </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">System.out.print("<span style="color: #8b0000"> </span>" + c.getId()); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">System.out.print("<span style="color: #8b0000"> </span>" + c.getFirstName()); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">System.out.print("<span style="color: #8b0000"> </span>" + c.getLastName()); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">System.out.print("<span style="color: #8b0000"> </span>" + c.getEmail()); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">contact = c; </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">System.out.println("<span style="color: #8b0000"> </span>"); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">logger.debug("<span style="color: #8b0000">selectAllContact:</span>"+contactDaoImpl.selectAllContact()); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">logger.debug("<span style="color: #8b0000">selectContactById:</span>"+contactDaoImpl.selectContactById(1)); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">logger.debug("<span style="color: #8b0000">selectContactByName:</span>"+contactDaoImpl.selectContactByName("<span style="color: #8b0000">David</span>")); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">contact.setFirstName("<span style="color: #8b0000">Jrare</span>"); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">contact.setLastName("<span style="color: #8b0000">Leyd</span>"); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">contact.setEmail("<span style="color: #8b0000">ZZZ</span>"); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">logger.debug("<span style="color: #8b0000">updateContact:</span>"+contactDaoImpl.updateContact(contact)); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">logger.debug("<span style="color: #8b0000">deleteContactById:</span>"+contactDaoImpl.deleteContactById(1)); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">logger.debug("<span style="color: #8b0000">insertContact:</span>"+contactDaoImpl.addContact(contact)); </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px">} </pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 10px"></pre></pre>mysql

相關文章
相關標籤/搜索