ibatis串連oracle例子!__oracle

來源:互聯網
上載者:User

一個使用ibatis對資料庫增刪改查的例子:
這裡用的開發環境是:Eclipse3.2+mysql5.0.20,ibatis包是ibatis-common-2.jar,
ibatis-dao-2.jar,ibatis-sqlmap-2.jar,mysql包是mysql-connector-java-5.0.3-bin.jar.
步驟:
1.建立資料庫:
 create database itcast;
 use itcast;
建立表:
 create table student
 (
  id int primary key auto_increment,
  firstname varchar(20) not null,
  lastname varchar(20) not null
 )

2.建立POJO類,Student.java,此程式中用到的所有類都放在cn.itcast包下面,
 其他的設定檔都放在txd.configfile包下面。
 package cn.itcast;
 
 public class Student {
  private Integer id;
 
  private String firstname;
 
  private String lastname;
 
  public String getFirstname() {
   return firstname;
  }
 
  public void setFirstname(String firstname) {
   this.firstname = firstname;
  }
 
  public Integer getId() {
   return id;
  }
 
  public void setId(Integer id) {
   this.id = id;
  }
 
  public String getLastname() {
   return lastname;
  }
 
  public void setLastname(String lastname) {
   this.lastname = lastname;
  }
 }

3.跟Student類對應的xml設定檔Student.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="student">
  <!--insert元素,id屬性值作為標識此元素,parameterClass屬性是參數的類型,此
  屬性的值是Java類的全限定名(即包括類的包名)。它是可選的,但強烈建議使用。
  它的目的是 限制輸入參數的類型為指定的Java類,並最佳化架構的效能。##符號中
  間的名字來自此類的屬性.-->
  <insert id="insert_student" parameterClass="cn.itcast.Student">
   insert into student(firstname,lastname) values
   (#firstname#,#lastname#)
  </insert>
 
  <select id="getStudent" resultClass="cn.itcast.Student">
   select id, firstname, lastname from student
  </select>
 
  <delete id="delStudent" parameterClass="int">
   delete from student where id=#value#
  </delete>
 
  <update id="updateStudent" parameterClass="cn.itcast.Student">
   update student set firstname=#firstname#,lastname=#lastname#
   where id=#id#
  </update>
 </sqlMap>

4.jdbc.properties檔案,儲存資料庫連接的driver,url,username,password等資訊,
 driver=com.mysql.jdbc.Driver
 url=jdbc:mysql:///itcast
 username=root
 password=

5. SqlMap的設定檔SqlMapConfigExample.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>元素,用於在設定檔中使用標準的Java屬性檔案(name=value)-->
  <properties resource="txd/configfile/jdbc.properties" />
  <!--
  cacheModelsEnabled:全域性地啟用或禁用SqlMapClient的所有緩衝model。
  enhancementEnabled:全域性地啟用或禁用運行時位元組碼增強,以最佳化訪問
   Java Bean屬性的效能,同時最佳化消極式載入的效能。
  lazyLoadingEnabled:全域性地啟用或禁用SqlMapClient的所有消極式載入。
  maxRequests:同時執行SQL語句的最大線程數。
  maxSessions:同一時間內活動的最大session數。
  maxTransactions:同時進入SqlMapClient.startTransaction()的最大線程數。
  useStatementNamespaces:如果啟用本屬性,必須使用全限定名來引用mapped statement。
   Mapped statement的全限定名由sql-map的名稱和mapped-statement的名稱合成。
  -->
  <settings cacheModelsEnabled="true" enhancementEnabled="true"
   lazyLoadingEnabled="true" maxRequests="32" maxSessions="10"
   maxTransactions="5" useStatementNamespaces="false" />
 
  <!--
  <transationManager>元素讓您為SQL Map配置交易管理服務。屬性type指定所
  使用的交易管理員類型。這個屬性值可以是一個類名,也可以是一個別名。
  包含在架構的三個交易管理員分別是:JDBC,JTA和EXTERNAL。
  -->
  <transactionManager type="JDBC">
   <!--dataSource元素為SQL Map資料來源設定了一系列參數。-->
   <dataSource type="SIMPLE">
    <property name="JDBC.Driver" value="${driver}" />
    <property name="JDBC.ConnectionURL" value="${url}" />
    <property name="JDBC.Username" value="${username}" />
    <property name="JDBC.Password" value="${password}" />
   </dataSource>
  </transactionManager>
  <!--<sqlMap>元素用於包括SQL Map對應檔和其他的SQL Map設定檔。-->
  <sqlMap resource="txd/configfile/Student.xml" />
 </sqlMapConfig>

6.MySqlMapClient.java類,用於產生一個SqlMapClient
 package cn.itcast;
 
 import java.io.IOException;
 import java.io.Reader;
 
 import com.ibatis.common.resources.Resources;
 import com.ibatis.sqlmap.client.SqlMapClient;
 import com.ibatis.sqlmap.client.SqlMapClientBuilder;
 
 public class MySqlMapClient {
 
  private static SqlMapClient sqlMapClient;
 
  static {
   String resource = "txd/configfile/SqlMapConfigExample.xml";
   Reader reader = null;
   try {
    reader = Resources.getResourceAsReader(resource);
    sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 
  public static SqlMapClient getSqlMapInstance() {
   return sqlMapClient;
  }
 
 }

7.StudentManager 類測試一下上面的代碼,
 package cn.itcast;
 
 import java.sql.SQLException;
 import java.util.List;
 
 import com.ibatis.sqlmap.client.SqlMapClient;
 
 public class StudentManager {
 
  public static void main(String[] args) {
   SqlMapClient sqlMap = MySqlMapClient.getSqlMapInstance();
 
   // insert一條記錄
 
   // Student student = new Student();
   // student.setFirstname("zhang");
   // student.setLastname("san");
   // try {
   // sqlMap.insert("insert_student", student);
   // System.out.println("插入成功!");
   // } catch (SQLException e) {
   // e.printStackT

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.