iBatis初级入门

qshpeng 2013-01-22

一个简单的iBatis应用所需要的文件

1:POJO类  例:Person.java

public class Person {
      private int id;
      private String firstName;
      private String lastName;
      private Date birthDate;
      private double weightInKilograms;
      private double heightInMeters;
      // getter,setter省略
}

2:iBatis总的配置文件  例: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>
   <!-- 加载JDBC配置文件 -->
   <properties resource="examples/sqlmap/maps/SqlMapConfigExample.properties" />
      <!-- 设置连接属性 -->
      <settings
            cacheModelsEnabled="true"
            enhancementEnabled="true"
            lazyLoadingEnabled="true"
            maxRequests="32"
            maxSessions="10"
            maxTransactions="5"
            useStatementNamespaces="false" />
      <!-- 别名 -->
      <typeAlias alias="order" type="testdomain.Order" />
      <!-- transaction&dataSource配置  -->
      <transactionManager type="JDBC">
            <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>
      <!-- 引入POJO类的配置文件 -->
      <sqlMap resource="examples/sqlmap/maps/Person.xml" />
</sqlMapConfig>

3:JDBC配置文件 SqlMapConfigExample.properties

driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:oracle1
username=jsmith
password=test

4:对应POJO类的配置文件  例: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">
      <!-- 查询 -->
     <select id="getPerson" parameterClass="int"        resultClass="examples.domain.Person">
            SELECT
                  PER_ID as id,
                  PER_FIRST_NAME as firstName,
                  PER_LAST_NAME as lastName,
                  PER_BIRTH_DATE as birthDate,
                  PER_WEIGHT_KG as weightInKilograms,
                  PER_HEIGHT_M as heightInMeters
            FROM PERSON WHERE
                  PER_ID = #value#
      </select>
      <!-- 新增 -->
      <insert id="insertPerson" parameterClass="examples.domain.Person">
            INSERT INTO PERSON
                  (PER_ID, PER_FIRST_NAME, PER_LAST_NAME, PER_BIRTH_DATE, PER_WEIGHT_KG,
                  PER_HEIGHT_M)
            VALUES
                  (#id#, #firstName#, #lastName#, #birthDate#,#weightInKilograms#, #heightInMeters#)
      </insert>
      <!-- 更新 -->
      <update id="updatePerson" parameterClass="examples.domain.Person">
            UPDATE PERSON SET
                  PER_FIRST_NAME = #firstName#,
                  PER_LAST_NAME = #lastName#,
                  PER_BIRTH_DATE = #birthDate#,
                  PER_WEIGHT_KG = #weightInKilograms#,
                  PER_HEIGHT_M = #heightInMeters#
            WHERE PER_ID = #id#
      </update>
      <!-- 删除    -->
      <delete id="deletePerson" parameterClass="examples.domain.Person">
            DELETE PERSON WHERE
                  PER_ID = #id#
      </delete>
</sqlMap>

5:对应表 Person.sql

CREATE TABLE PERSON(
	PER_ID NUMBER (5, 0) NOT NULL,
	PER_FIRST_NAME VARCHAR (40) NOT NULL,
	PER_LAST_NAME VARCHAR (40) NOT NULL,
	PER_BIRTH_DATE DATETIME ,
	PER_WEIGHT_KG NUMBER (4, 2) NOT NULL,
	PER_HEIGHT_M NUMBER (4, 2) NOT NULL,
	PRIMARY KEY (PER_ID)
)

 6:封装

public class MyAppSqlConfig {
      private static final SqlMapClient sqlMapClient ;
      static {
            try {
               String resource = "com/ibatis/example/sql-map-config.xml";
               Reader reader = Resources.getResourceAsReader(resource);
               sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
            } catch (Exception e) {
                  e.printStackTrace();
            }
      }
 
      public static SqlMapClient getSqlMapInstance() {
            return sqlMapClient;
      }
}

 7:应用

public class Person {
      public static void main(String[] args)
      {
            //---------查询--------------
           SqlMapClient sqlMapClient = MyAppSqlMapConfig.getSqlMapInstance();
            Integer personPk = new Integer(5);
            Person person = (Person) sqlMapClient.queryForObject ("getPerson", personPk);
           
            //---------更新-------------
            person.setHeightInMeters(1.83);
            person.setWeightInKilograms(86.36);
            sqlMapClient.update("updatePerson", person);
           
            //---------删除----------
            sqlMapClient.delete("deletePerson", person);
           
            //---------插入----------
            Person newPerson = new Person();
            newPerson.setId(11);
            newPerson.setFirstName("Clinton");
            newPerson.setLastName("Begin");
            newPerson.setBirthDate (null);
            newPerson.setHeightInMeters(1.83);
            newPerson.setWeightInKilograms(86.36);
            sqlMapClient.insert ("insertPerson", newPerson);
 
      }
}

相关推荐

兔子压倒窝边草 / 0评论 2013-07-26
蜀川居 / 0评论 2014-07-16