现在的位置: 首页 > 综合 > 正文

ibatis-2.3.3.720的simple_example

2013年10月08日 ⁄ 综合 ⁄ 共 5816字 ⁄ 字号 评论关闭

 ibatis-2.3.3.720的simple_example

 

说明:用到的就是这些文件(格式),需要的时候,复制粘贴过去修改即可。

 

SqlMapConfig.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE sqlMapConfig      
  3.     PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"      
  4.     "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
  5. <sqlMapConfig>
  6.   <!-- Configure a built-in transaction manager.  If you're using an 
  7.        app server, you probably want to use its transaction manager 
  8.        and a managed datasource -->
  9.   <transactionManager type="JDBC" commitRequired="false">
  10.     <dataSource type="SIMPLE">
  11.       <property name="JDBC.Driver" value="org.hsqldb.jdbcDriver"/>
  12.       <property name="JDBC.ConnectionURL" value="jdbc:hsqldb:."/>
  13.       <property name="JDBC.Username" value="sa"/>
  14.       <property name="JDBC.Password" value="sa"/>
  15.     </dataSource>
  16.   </transactionManager>
  17.   <!-- List the SQL Map XML files. They can be loaded from the 
  18.        classpath, as they are here (com.domain.data...) -->
  19.   <sqlMap resource="com/mydomain/data/Account.xml"/>
  20.   <!-- List more here...
  21.   <sqlMap resource="com/mydomain/data/Order.xml"/>
  22.   <sqlMap resource="com/mydomain/data/Documents.xml"/>
  23.   -->
  24. </sqlMapConfig>

 

Account.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE sqlMap      
  3.     PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"      
  4.     "http://ibatis.apache.org/dtd/sql-map-2.dtd">
  5. <sqlMap namespace="Account">
  6.   <!-- Use type aliases to avoid typing the full classname every time. -->
  7.   <typeAlias alias="Account" type="com.mydomain.domain.Account"/>
  8.   <!-- Result maps describe the mapping between the columns returned
  9.        from a query, and the class properties.  A result map isn't
  10.        necessary if the columns (or aliases) match to the properties 
  11.        exactly. -->
  12.   <resultMap id="AccountResult" class="Account">
  13.     <result property="id" column="ACC_ID"/>
  14.     <result property="firstName" column="ACC_FIRST_NAME"/>
  15.     <result property="lastName" column="ACC_LAST_NAME"/>
  16.     <result property="emailAddress" column="ACC_EMAIL"/>
  17.   </resultMap>
  18.   <!-- Select with no parameters using the result map for Account class. -->
  19.   <select id="selectAllAccounts" resultMap="AccountResult">
  20.     select * from ACCOUNT
  21.   </select>
  22.   <!-- A simpler select example without the result map.  Note the 
  23.        aliases to match the properties of the target result class. -->
  24.   <select id="selectAccountById" parameterClass="int" resultClass="Account">
  25.     select
  26.       ACC_ID as id,
  27.       ACC_FIRST_NAME as firstName,
  28.       ACC_LAST_NAME as lastName,
  29.       ACC_EMAIL as emailAddress
  30.     from ACCOUNT
  31.     where ACC_ID = #id#
  32.   </select>
  33.    
  34.   <!-- Insert example, using the Account parameter class -->
  35.   <insert id="insertAccount" parameterClass="Account">
  36.     insert into ACCOUNT (
  37.       ACC_ID,
  38.       ACC_FIRST_NAME,
  39.       ACC_LAST_NAME,
  40.       ACC_EMAIL
  41.     values (
  42.       #id#, #firstName#, #lastName#, #emailAddress#
  43.     )
  44.   </insert>
  45.   <!-- Update example, using the Account parameter class -->
  46.   <update id="updateAccount" parameterClass="Account">
  47.     update ACCOUNT set
  48.       ACC_FIRST_NAME = #firstName#,
  49.       ACC_LAST_NAME = #lastName#,
  50.       ACC_EMAIL = #emailAddress#
  51.     where
  52.       ACC_ID = #id#
  53.   </update>
  54.   <!-- Delete example, using an integer as the parameter class -->
  55.   <delete id="deleteAccountById" parameterClass="int">
  56.     delete from ACCOUNT where ACC_ID = #id#
  57.   </delete>
  58. </sqlMap>

SimpleExample.java

  1. package com.mydomain.data;
  2. import com.ibatis.sqlmap.client.SqlMapClient;
  3. import com.ibatis.sqlmap.client.SqlMapClientBuilder;
  4. import com.ibatis.common.resources.Resources;
  5. import com.mydomain.domain.Account;
  6. import java.io.Reader;
  7. import java.io.IOException;
  8. import java.util.List;
  9. import java.sql.SQLException;
  10. /**
  11.  * This is not a best practices class.  It's just an example
  12.  * to give you an idea of how iBATIS works.  For a more complete
  13.  * example, see JPetStore 5.0 at http://www.ibatis.com.
  14.  */
  15. public class SimpleExample {
  16.   /**
  17.    * SqlMapClient instances are thread safe, so you only need one.
  18.    * In this case, we'll use a static singleton.  So sue me.  ;-)
  19.    */
  20.   private static SqlMapClient sqlMapper;
  21.   /**
  22.    * It's not a good idea to put code that can fail in a class initializer,
  23.    * but for sake of argument, here's how you configure an SQL Map.
  24.    */
  25.   static {
  26.     try {
  27.       Reader reader = Resources.getResourceAsReader("com/mydomain/data/SqlMapConfig.xml");
  28.       sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
  29.       reader.close(); 
  30.     } catch (IOException e) {
  31.       // Fail fast.
  32.       throw new RuntimeException("Something bad happened while building the SqlMapClient instance." + e, e);
  33.     }
  34.   }
  35.   public static List selectAllAccounts () throws SQLException {
  36.     return sqlMapper.queryForList("selectAllAccounts");
  37.   }
  38.   public static Account selectAccountById  (int id) throws SQLException {
  39.     return (Account) sqlMapper.queryForObject("selectAccountById", id);
  40.   }
  41.   public static void insertAccount (Account account) throws SQLException {
  42.     sqlMapper.insert("insertAccount", account);
  43.   }
  44.   public static void updateAccount (Account account) throws SQLException {
  45.     sqlMapper.update("updateAccount", account);
  46.   }
  47.   public static void deleteAccount (int id) throws SQLException {
  48.     sqlMapper.delete("deleteAccount", id);
  49.   }
  50. }

Account.java

  1. package com.mydomain.domain;
  2. public class Account {
  3.   private int id;
  4.   private String firstName;
  5.   private String lastName;
  6.   private String emailAddress;
  7.   public int getId() {
  8.     return id;
  9.   }
  10.   public void setId(int id) {
  11.     this.id = id;
  12.   }
  13.   public String getFirstName() {
  14.     return firstName;
  15.   }
  16.   public void setFirstName(String firstName) {
  17.     this.firstName = firstName;
  18.   }
  19.   public String getLastName() {
  20.     return lastName;
  21.   }
  22.   public void setLastName(String lastName) {
  23.     this.lastName = lastName;
  24.   }
  25.   public String getEmailAddress() {
  26.     return emailAddress;
  27.   }
  28.   public void setEmailAddress(String emailAddress) {
  29.     this.emailAddress = emailAddress;
  30.   }
  31. }

 

抱歉!评论已关闭.