ibatis學習筆記

 

650) this.width=650;" border="0" alt="" src="http://img1.51cto.com/attachment/201301/150203814.jpg" />

SqlMapConfig.xml

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

SqlMap.properties

 

  1. driver=com.mysql.jdbc.Driver 
  2. url=jdbc:mysql://localhost:3306/ibatis?useUnicode=true&characterEncoding=UTF-8 
  3. username=root 
  4. password=root 

model

  1. package model; 
  2.  
  3. public class Account { 
  4.     private int id; 
  5.     private String firstName; 
  6.     private String lastName; 
  7.     private String emailAddress; 
  8.      
  9.     public int getId() { 
  10.         return id; 
  11.     } 
  12.     public void setId(int id) { 
  13.         this.id = id; 
  14.     } 
  15.     public String getFirstName() { 
  16.         return firstName; 
  17.     } 
  18.     public void setFirstName(String firstName) { 
  19.         this.firstName = firstName; 
  20.     } 
  21.     public String getLastName() { 
  22.         return lastName; 
  23.     } 
  24.     public void setLastName(String lastName) { 
  25.         this.lastName = lastName; 
  26.     } 
  27.     public String getEmailAddress() { 
  28.         return emailAddress; 
  29.     } 
  30.     public void setEmailAddress(String emailAddress) { 
  31.         this.emailAddress = emailAddress; 
  32.     } 
  33.     @Override 
  34.     public String toString() { 
  35.         return "Account [id=" + id + ", firstName=" + firstName + ", lastName=" 
  36.                 + lastName + ", emailAddress=" + emailAddress + "]"
  37.     } 
  38.      
  39.      

配置文件Account.xml

 

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

dao

  1. package dao; 
  2.  
  3. import java.util.List; 
  4.  
  5. import model.Account; 
  6.  
  7. public interface AccountDao { 
  8.     public void addAccount(Account account); 
  9.     public void deleteAccountById(int id); 
  10.     public void updateAccount(Account account); 
  11.     public List<Account> queryAllAccount(); 
  12.     public List<Account> queryStudentByFistName(String fistName); 
  13.     public Account queryAccountById(int id); 

dao.impl

 

  1. package dao.impl; 
  2.  
  3. import java.io.IOException; 
  4. import java.io.Reader; 
  5. import java.sql.SQLException; 
  6. import java.util.List; 
  7.  
  8. import com.ibatis.common.resources.Resources; 
  9. import com.ibatis.sqlmap.client.SqlMapClient; 
  10. import com.ibatis.sqlmap.client.SqlMapClientBuilder; 
  11.  
  12. import model.Account; 
  13. import dao.AccountDao; 
  14.  
  15. public class AccountDaoImpl implements AccountDao{ 
  16.      
  17.     private static SqlMapClient sqlMapClient = null ; 
  18.     static{ 
  19.         try { 
  20.             Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml"); 
  21.             sqlMapClient=SqlMapClientBuilder.buildSqlMapClient(reader); 
  22.             reader.close();  
  23.         } catch (IOException e) { 
  24.             // TODO Auto-generated catch block 
  25.             e.printStackTrace(); 
  26.         } 
  27.     } 
  28.      
  29.     @Override 
  30.     public void addAccount(Account account) { 
  31.         try { 
  32.             this.sqlMapClient.insert("insertAccount", account); 
  33.         } catch (SQLException e) { 
  34.             // TODO Auto-generated catch block 
  35.             e.printStackTrace(); 
  36.         } 
  37.     } 
  38.  
  39.     @Override 
  40.     public void deleteAccountById(int id) { 
  41.         try { 
  42.             this.sqlMapClient.delete("deleteAccountById",id); 
  43.         } catch (SQLException e) { 
  44.             // TODO Auto-generated catch block 
  45.             e.printStackTrace(); 
  46.         } 
  47.     } 
  48.  
  49.     @Override 
  50.     public void updateAccount(Account account) { 
  51.         // TODO Auto-generated method stub 
  52.         try { 
  53.             this.sqlMapClient.update("updateAccount", account); 
  54.         } catch (SQLException e) { 
  55.             // TODO Auto-generated catch block 
  56.             e.printStackTrace(); 
  57.         } 
  58.     } 
  59.  
  60.     @Override 
  61.     public List<Account> queryAllAccount() { 
  62.         // TODO Auto-generated method stub 
  63.         List<Account> list = null
  64.         try { 
  65.             list = this.sqlMapClient.queryForList("selectAllAccounts"); 
  66.         } catch (SQLException e) { 
  67.             // TODO Auto-generated catch block 
  68.             e.printStackTrace(); 
  69.         } 
  70.         return list; 
  71.     } 
  72.  
  73.     @Override 
  74.     public List<Account> queryStudentByFistName(String fistName) { 
  75.         // TODO Auto-generated method stub 
  76.         try { 
  77.             return this.sqlMapClient.queryForList("queryAccountByFirstName", fistName); 
  78.         } catch (SQLException e) { 
  79.             // TODO Auto-generated catch block 
  80.             e.printStackTrace(); 
  81.         } 
  82.         return null; 
  83.     } 
  84.  
  85.     @Override 
  86.     public Account queryAccountById(int id) { 
  87.         Object o =null
  88.         try { 
  89.             o=this.sqlMapClient.queryForList("selectAccountById",id); 
  90.         } catch (SQLException e) { 
  91.             // TODO Auto-generated catch block 
  92.             e.printStackTrace(); 
  93.         } 
  94.         return (Account) o; 
  95.     } 
  96.      
  97.  
  98.     public static void main(String[] args) { 
  99.         AccountDao accountDao = new AccountDaoImpl(); 
  100.          
  101.         /////////////////////////1 queryAllAccount //////////////////////// 
  102. //      List<Account> list = accountDao.queryAllAccount(); 
  103. //      for(Account a:list){ 
  104. //          System.out.println(a); 
  105. //      } 
  106.          
  107.         ////////////////////////2 queryAccount/////////////////////// 
  108. //      Account a = accountDao.queryAccountById(1); 
  109. //      System.out.println(a); 
  110.          
  111.         ////////////////////////3 addAccount////////////////////////// 
  112. //      Account a = new Account(); 
  113. //      a.setEmailAddress("[email protected]"); 
  114. //      a.setFirstName("柯南"); 
  115. //      accountDao.addAccount(a); 
  116.          
  117.         ////////////////////////4 deleteAccountById//////////////////////////////// 
  118. //      accountDao.deleteAccountById(2); 
  119.          
  120.         ////////////////////////5 //////////////////////////////////////// 
  121. //      Account a = new Account(); 
  122. //      a.setId(1); 
  123. //      a.setFirstName("update"); 
  124. //      accountDao.updateAccount(a); 
  125.          
  126.          
  127.         ////////////////////////6////////////////////// 
  128.         List<Account> l = accountDao.queryStudentByFistName(""); 
  129.         for(Account o : l){ 
  130.             System.out.println(o); 
  131.         } 
  132.     } 

 

本文出自 “Kenan_ITBlog” 博客,請務必保留此出處http://soukenan.blog.51cto.com/5130995/1114691

發佈了115 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章