天天看點

Ibatis 學習(一) 1.與hibernate的差別 2.Ibatis實作第一個demo

    簡介:iBATIS一詞來源于“internet”和“abatis”的組合,iBATIS是Clinton Begin在2001年發起的開放源代碼項目。現在由APACHE基金會支援的用于加快JDBC程式設計的經過泛化的架構,是一個持久化架構。   一.Ibatis 與 hibernate之間的差別 1)Hibernate的映射關系   Class —— Table 實體類——表

Ibatis 學習(一) 1.與hibernate的差別 2.Ibatis實作第一個demo

  2)Ibatis的映射關系   結果集與實體類之間進行映射     “半自動化”,是相對Hibernate等提供了全面的資料庫封裝機制的“全自動化”

ORM 實作而言,“全自動”ORM 實作了POJO 和資料庫表之間的映射,以及SQL 的自動

生成和執行。而ibatis 的着力點,則在于POJO 與SQL之間的映射關系。也就是說,ibatis

并不會為程式員在運作期自動生成SQL 執行。具體的SQL 需要程式員編寫,然後通過映

射配置檔案,将SQL所需的參數,以及傳回的結果字段映射到指定POJO。  

Ibatis 學習(一) 1.與hibernate的差別 2.Ibatis實作第一個demo

    *主要差別小結:           ibatis                           hibernate           1)            sql語句需要手動編寫                  可以自動生成HQL語句   2)        易上手,易優化,沿用sql語句          資料庫無關性好,O/R映射能力強,不需編大量HQL    **當遇到以下幾點時建議不要使用hibernate   1)安全方面,不想對外提供具體的表結構   2)銀行類項目,所有業務邏輯部分的資料庫操作,必須在資料庫層由存儲過程實作   3)資料處理量巨大,性能要求極為苛刻,必須通過經過高度優化的SQL語句(或存儲過程)   ***ibatis與傳統JDBC的比較     1、減少了61%的代碼量     2、最簡單的資料庫持久化架構     3、架構性能架構性能增強     4、sql代碼從代碼中分離,可實作重用     5、增強了項目中的分工合作     6、增強了代碼的移植性和代碼的可維護性   二.第一個demo   1、建立資料庫              表名:stuInfo   字段名:stu_id  stu_name  stu_pwd  stu_age  stu_*** 2、建立工程              IbatisTest 3、引入jar包              ibatis-2.3.0.677.jar              mysql-connector-java-5.1.13-bin.jar   4、編寫SqlMapConfig.xml   配置JDBC事務  設定 <sqlMap resource = "student.xml" / >       

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
  3.     "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
  4. <sqlMapConfig >
  5.     <properties resource="sqlmap.properties"/>
  6.     <!-- setting參數設定 -->
  7.     <!--<settings
  8.             cacheModelsEnabled="true"
  9.             enhancementEnabled="true"
  10.             errorTracingEnabled="true"
  11.             lazyLoadingEnabled="true"
  12.             maxTransactions=""
  13.             maxSessions=""
  14.             maxRequests=""
  15.             useStatementNamespaces="true"
  16.         />-->
  17.     <!-- 配置JTA事務 -->
  18.     <!-- 配置JDBC事務 -->
  19.      <transactionManager type="JDBC">
  20.         <dataSource type="SIMPLE">
  21.      <property value="${driver}" name="JDBC.Driver"/>
  22.      <property value="${url}" name="JDBC.ConnectionURL"/>
  23.      <property value="${username}" name="JDBC.Username"/>
  24.      <property value="${password}" name="JDBC.Password"/>
  25.         </dataSource>
  26.       </transactionManager>
  27.   <sqlMap resource="student.xml"/>
  28. </sqlMapConfig>

  5、編寫sqlmap.properties 配置資料庫

  1. driver=com.mysql.jdbc.Driver
  2. url=jdbc\:mysql\://localhost\:3306/test?autoReconnect\=true&useUnicode\=true&characterEncoding\=UTF-8&mysqlEncoding\=uft-8
  3. username=root
  4. password=123456

6、編寫實體類->編寫IStuDAO接口,并且在接口類定義 增删改查學生資訊的方法   *7、編寫StuDAOImpl類并實作接口的方法   注意: sqlMapClient對象     1)使用一個 靜态類,隻擷取一次sqlMapClient   

  1. public class StuDAOImpl implements IStuDAO      {
  2. private static SqlMapClient sqlMapClient = null;
  3. static {
  4.      try {
  5.           // 設定配置檔案位置,進行讀取
  6.           Reader reader = com.ibatis.common.resources.Resources
  7.                   .getResourceAsReader("SqlMapConfig.xml");
  8.           sqlMapClient = com.ibatis.sqlmap.client.SqlMapClientBuilder
  9.                   .buildSqlMapClient(reader);
  10.           reader.close();
  11.          } catch (Exception e) {
  12.              e.printStackTrace();
  13.       }
  14. }

    2)分别實作DAO方法    使用sqlMapClient.insert/update/delect("xxxStu",stu);         a)單個對象的 增删改

  1.     public void addStu(Student stu) {
  2.         try {
  3.             sqlMapClient.insert("addStu", stu);
  4.         } catch (Exception e) {
  5.             e.printStackTrace();
  6.         }
  7.         System.out.println("插入成功!");
  8.     }

       b)對多個對象的查詢,使用 sqlMapClient.queryForList傳回一個List集合        

  1.     public List<Student> selectAllStu() {
  2.         List stuList = null;
  3.         try {
  4.             stuList = sqlMapClient.queryForList("selectAllStu");
  5.         } catch (Exception e) {
  6.             e.printStackTrace();
  7.         }
  8.         return stuList;
  9.     }

       c)使用 sqlMapClient.queryForObject傳回一個對象

  1. public Student selectStuById(int id) {
  2.    Student stu = null;
  3.    try {
  4.         stu = (Student) sqlMapClient.queryForObject("selectStuById", id);
  5.    } catch (Exception e) {
  6.         e.printStackTrace();
  7.    }
  8.         return stu;
  9. }

8.配置實體類對應的配置檔案Student.xml   配置如下兩行,才可以使用sqlMap之類的标簽

  1. <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
  2. "http://ibatis.apache.org/dtd/sql-map-2.dtd">

Student.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
  3. "http://ibatis.apache.org/dtd/sql-map-2.dtd">
  4. <sqlMap>
  5. <typeAlias alias="Student" type="cn.com.ibatis.entity.Student" />
  6. <!-- Student的結果集,配置O/R映射 -->
  7. <resultMap id="StudentResult" class="Student">
  8. <result property="id" column="stu_id" />
  9. <result property="name" column="stu_name" />
  10. <result property="pwd" column="stu_pwd" />
  11. <result property="age" column="stu_age" />
  12. <result property="***" column="stu_***" />
  13. </resultMap>
  14. <!-- 寫DAO中對應方法的SQL語句 -->
  15. <!-- 查詢所有學生 -->
  16. <select id="selectAllStu" resultMap="StudentResult" cacheModel="userCache">
  17. select * from stuInfo
  18. </select>
  19. <!-- 通過學生ID查詢單個學生 -->
  20. <select id="selectStuById" parameterClass="int" resultClass="Student">
  21. select * from stuInfo where stu_id=#id#
  22. </select>
  23. <!-- 通過學生name查詢單個學生 -->
  24. <select id="selectStuByName" parameterClass="String" resultClass="Student">
  25. select * from stuInfo where stu_id=#id#
  26. </select>
  27. <!-- 删除一個學生 -->
  28. <delete id="delStuById" parameterClass="int">
  29. delete from stuInfo where
  30. stu_id=#id#
  31. </delete>
  32. <!-- 插入一個學生 -->
  33. <insert id="addStu" parameterClass="Student">
  34. insert into
  35. stuInfo(stu_id
  36. ,stu_name,stu_pwd,stu_***,stu_age)
  37. values(null,#name#,#pwd#,#***#,#age#)
  38. </insert>
  39. <!-- 更新一個學生的資訊 -->
  40. <update id="updateStu" parameterClass="Student">
  41. update
  42. stuInfo set
  43. stu_name=#name#,stu_pwd=#pwd#,stu_***=#***#,stu_age=#age# where
  44. stu_id=#id#
  45. </update>
  46. <!-- 設定緩存 -->
  47. <cacheModel type="LRU" id="userCache">
  48. <flushInterval hours="2" />
  49. <flushOnExecute statement="updateStu" />
  50. <property name="size" value="1000" />
  51. </cacheModel>
  52. <!-- 存儲過程 <procedure id=""> </procedure> -->
  53. </sqlMap>

  9.編寫測試類測試

  1. package cn.com.test;
  2. import cn.com.ibatis.dao.impl.StuDAOImpl;
  3. import cn.com.ibatis.entity.Student;
  4. public class TestStuDAOImpl {
  5.     public static void main(String[] args) {
  6.         StuDAOImpl stuImp = new StuDAOImpl();
  7.         Student stu = new Student();
  8.         stu.setId(3);
  9.         stu.setName("可樂小牛");
  10.         stu.setPwd("123");
  11.         stu.setAge(20);
  12.         stu.set***("男");
  13.     //    stuImp.addStu(stu);
  14.     //    stuImp.updateById(stu);
  15.     //    stuImp.selectStuById(1);
  16.     //    stuImp.selectAllStu();
  17.     //    stuImp.selectStuByName("可樂大牛");
  18.     //    stuImp.delById(2);
  19.     }
  20. }

一個簡單的demo完成了,功能也都實作了。    在O/R那一塊,關于SQL語句中 是寫字段名還是寫實體類的屬性名,糾結了一會。最終結論還是前面寫字段名,後面寫#xxx#   eg. insert into

  stuInfo(stu_id

  ,stu_name,stu_pwd,stu_***,stu_age)

  values(null,#name#,#pwd#,#***#,#age#)   update

  stuInfo set

  stu_name=#name#,stu_pwd=#pwd#,stu_***=#***#,stu_age=#age# where

  stu_id=#id#   這隻是我目前的了解,大家有誰有更加深入的關于我糾結的這一塊了解,麻煩給我留言說說哦,謝謝了   下面附上很有用的ibatis指導

Ibatis 學習(一) 1.與hibernate的差別 2.Ibatis實作第一個demo

 夏昕-ibatis 開發指南.pdf                    <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script> 閱讀(958) | 評論(0) | 轉發(0) | 0

上一篇:Spring 學習(一) 1.第一個demo + 2.配置檔案分開編寫後整合方法 别名效果 + 3.IOC

下一篇:ExtJS 初體驗 第一個helloword demo

相關熱門文章

  • 定位Linux下定位程序被誰KILL...
  • sqlplus翻頁工具uniread
  • Word2Vec指令的功能
  • RxJava操作符(二)Transformi...
  • 歡迎hibernates9在ChinaUnix博...
  • 關于SSI
  • IP Sec VPN與NAT破鏡重圓
  • JS模拟滑鼠自動點選
  • UT2.0正式版下載下傳
  • tomcat6.0配置(含配置視訊下載下傳...
  • linux dhcp peizhi roc
  • 關于Unix檔案的軟連結
  • 求教這個指令什麼意思,我是新...
  • sed -e "/grep/d" 是什麼意思...
  • 誰能夠幫我解決LINUX 2.6 10...

給主人留下些什麼吧!~~ 評論熱議

繼續閱讀