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

相关文章
相关标签/搜索