iBatis入門例子,用ORACLE和Java測試____Java

來源:互聯網
上載者:User

一個簡單的iBatis入門例子,用ORACLE和Java測試

目錄結構:

1.匯入iBatis和oracle驅動。

 

2.建立類Person.java

 

package com.ibeats;
import java.util.Date;

public class Person {
 private int id;
 private String firstName;
 private String lastName;
 private double weightInKilograms;
 private double heightInMeters;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getFirstName() {
  return firstName;
 }
 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }
 public String getLastName() {
  return lastName;
 }
 public void setLastName(String lastName) {
  this.lastName = lastName;
 }
 public double getWeightInKilograms() {
  return weightInKilograms;
 }
 public void setWeightInKilograms(double weightInKilograms) {
  this.weightInKilograms = weightInKilograms;
 }
 public double getHeightInMeters() {
  return heightInMeters;
 }
 public void setHeightInMeters(double heightInMeters) {
  this.heightInMeters = heightInMeters;
 }
 }

 

 

3.設定檔SQLMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
 PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
 "http://www.ibatis.com/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
    <properties resource="com/ibeats/db.properties" />  
 <settings
  cacheModelsEnabled="true"
  enhancementEnabled="true"
  lazyLoadingEnabled="true"
  maxRequests="32"
  maxSessions="10"
  maxTransactions="5"
  useStatementNamespaces="false"
 />
 
 <transactionManager type="JDBC" >
  <dataSource type="SIMPLE">
   <property name="JDBC.Driver" value="${driver}"/>
   <property name="JDBC.ConnectionURL" value="${url}"/>
   <property name="JDBC.Username" value="${user}"/>
   <property name="JDBC.Password" value="${password}"/>
  </dataSource>
 </transactionManager>

 <sqlMap resource="com/ibeats/Person.xml" />
</sqlMapConfig>

 

(檔案元素說明:
resource:屬性列表設定檔,以便用於資料庫連接參數設定。
settings:
 cacheModelsEnabled:資料快取,提高程式效能,利用LRU(最近最久未使用)方法對使用過的資料儲存在記憶體中。預設true
 enhancementEnabled:指定是否用cglib中那些已最佳化的類來提高消極式載入的效能。預設ture
 lazyLoadingEnabled:消極式載入,除非絕對需要,否則延後載入的技術。預設true
 maxRequests(已廢棄):一次最多有多少個請求,預設為512
 maxSessions(已廢棄):任何時候只允許會話數,預設128
 maxTransactions(已廢棄):事務最大數目,預設32
 useStatementNamespaces:在引用已映射語句時,是否需要使用限定名。預設false
transactionManager:處理所有的資料庫事務。
        dataSoutce:資料來源工廠
        property:配置項
sqlMap:配置SQLMAP檔案
typeAlias:定義別名)

 

 

4.設定檔person.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
 PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
 "http://www.ibatis.com/dtd/sql-map-2.dtd">

<sqlMap namespace="Person">
 <!-- Use primitive wrapper type (e.g. Integer) as parameter and allow results to
 be auto-mapped results to Person object (Java Bean) properties -->
 <select id="getPerson" parameterClass="int" resultClass="com.ibeats.Person">
  SELECT PER_ID as id,
  PER_FIRST_NAME as firstName,
  PER_LAST_NAME as lastName,
  PER_WEIGHT_KG as weightInKilograms,
  PER_HEIGHT_M as heightInMeters
  FROM PERSON
  WHERE PER_ID = #id#
 </select>

</sqlMap>


(檔案元素說明:
parameterClass:傳入參數
resultClass:傳出參數
)

 

 

5.資料庫設定檔db.properties
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:ORACLEXC
user=scott
password=tiger

6.測試類別SqlMapClient.java
package com.ibeats;
import com.ibatis.sqlmap.client.*;
import com.ibatis.common.resources.*;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;

public class SqlMapClient {
public static void main(String args[]) {

 try {
  com.ibatis.sqlmap.client.SqlMapClient sqlMap = null;
  String resource = "com/ibeats/SQLMapConfig.xml";
  Reader reader = Resources.getResourceAsReader (resource);
  sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
  Person person =(Person)sqlMap.queryForObject("getPerson",new Integer(1));
                System.out.println(person.getFirstName());  //列印
 } catch (IOException e) {
  e.printStackTrace();
 } catch (SQLException e1){
  e1.printStackTrace();
 }
}
}


(說明:
queryForObject():擷取一條記錄。
queryForList():返回一行或多行,可用參數返回固定幾行,可用分頁。
)

大家只要改下資料庫代碼就可以在自己機器上運行了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.