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

2)Ibatis的映射關系 結果集與實體類之間進行映射 “半自動化”,是相對Hibernate等提供了全面的資料庫封裝機制的“全自動化”
ORM 實作而言,“全自動”ORM 實作了POJO 和資料庫表之間的映射,以及SQL 的自動
生成和執行。而ibatis 的着力點,則在于POJO 與SQL之間的映射關系。也就是說,ibatis
并不會為程式員在運作期自動生成SQL 執行。具體的SQL 需要程式員編寫,然後通過映
射配置檔案,将SQL所需的參數,以及傳回的結果字段映射到指定POJO。
*主要差別小結: 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" / >
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
- "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
- <sqlMapConfig >
- <properties resource="sqlmap.properties"/>
- <!-- setting參數設定 -->
- <!--<settings
- cacheModelsEnabled="true"
- enhancementEnabled="true"
- errorTracingEnabled="true"
- lazyLoadingEnabled="true"
- maxTransactions=""
- maxSessions=""
- maxRequests=""
- useStatementNamespaces="true"
- />-->
- <!-- 配置JTA事務 -->
- <!-- 配置JDBC事務 -->
- <transactionManager type="JDBC">
- <dataSource type="SIMPLE">
- <property value="${driver}" name="JDBC.Driver"/>
- <property value="${url}" name="JDBC.ConnectionURL"/>
- <property value="${username}" name="JDBC.Username"/>
- <property value="${password}" name="JDBC.Password"/>
- </dataSource>
- </transactionManager>
- <sqlMap resource="student.xml"/>
- </sqlMapConfig>
5、編寫sqlmap.properties 配置資料庫
- driver=com.mysql.jdbc.Driver
- url=jdbc\:mysql\://localhost\:3306/test?autoReconnect\=true&useUnicode\=true&characterEncoding\=UTF-8&mysqlEncoding\=uft-8
- username=root
- password=123456
6、編寫實體類->編寫IStuDAO接口,并且在接口類定義 增删改查學生資訊的方法 *7、編寫StuDAOImpl類并實作接口的方法 注意: sqlMapClient對象 1)使用一個 靜态類,隻擷取一次sqlMapClient
- public class StuDAOImpl implements IStuDAO {
- private static SqlMapClient sqlMapClient = null;
- static {
- try {
- // 設定配置檔案位置,進行讀取
- Reader reader = com.ibatis.common.resources.Resources
- .getResourceAsReader("SqlMapConfig.xml");
- sqlMapClient = com.ibatis.sqlmap.client.SqlMapClientBuilder
- .buildSqlMapClient(reader);
- reader.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
2)分别實作DAO方法 使用sqlMapClient.insert/update/delect("xxxStu",stu); a)單個對象的 增删改
- public void addStu(Student stu) {
- try {
- sqlMapClient.insert("addStu", stu);
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println("插入成功!");
- }
b)對多個對象的查詢,使用 sqlMapClient.queryForList傳回一個List集合
- public List<Student> selectAllStu() {
- List stuList = null;
- try {
- stuList = sqlMapClient.queryForList("selectAllStu");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return stuList;
- }
c)使用 sqlMapClient.queryForObject傳回一個對象
- public Student selectStuById(int id) {
- Student stu = null;
- try {
- stu = (Student) sqlMapClient.queryForObject("selectStuById", id);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return stu;
- }
8.配置實體類對應的配置檔案Student.xml 配置如下兩行,才可以使用sqlMap之類的标簽
- <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
- "http://ibatis.apache.org/dtd/sql-map-2.dtd">
Student.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
- "http://ibatis.apache.org/dtd/sql-map-2.dtd">
- <sqlMap>
- <typeAlias alias="Student" type="cn.com.ibatis.entity.Student" />
- <!-- Student的結果集,配置O/R映射 -->
- <resultMap id="StudentResult" class="Student">
- <result property="id" column="stu_id" />
- <result property="name" column="stu_name" />
- <result property="pwd" column="stu_pwd" />
- <result property="age" column="stu_age" />
- <result property="***" column="stu_***" />
- </resultMap>
- <!-- 寫DAO中對應方法的SQL語句 -->
- <!-- 查詢所有學生 -->
- <select id="selectAllStu" resultMap="StudentResult" cacheModel="userCache">
- select * from stuInfo
- </select>
- <!-- 通過學生ID查詢單個學生 -->
- <select id="selectStuById" parameterClass="int" resultClass="Student">
- select * from stuInfo where stu_id=#id#
- </select>
- <!-- 通過學生name查詢單個學生 -->
- <select id="selectStuByName" parameterClass="String" resultClass="Student">
- select * from stuInfo where stu_id=#id#
- </select>
- <!-- 删除一個學生 -->
- <delete id="delStuById" parameterClass="int">
- delete from stuInfo where
- stu_id=#id#
- </delete>
- <!-- 插入一個學生 -->
- <insert id="addStu" parameterClass="Student">
- insert into
- stuInfo(stu_id
- ,stu_name,stu_pwd,stu_***,stu_age)
- values(null,#name#,#pwd#,#***#,#age#)
- </insert>
- <!-- 更新一個學生的資訊 -->
- <update id="updateStu" parameterClass="Student">
- update
- stuInfo set
- stu_name=#name#,stu_pwd=#pwd#,stu_***=#***#,stu_age=#age# where
- stu_id=#id#
- </update>
- <!-- 設定緩存 -->
- <cacheModel type="LRU" id="userCache">
- <flushInterval hours="2" />
- <flushOnExecute statement="updateStu" />
- <property name="size" value="1000" />
- </cacheModel>
- <!-- 存儲過程 <procedure id=""> </procedure> -->
- </sqlMap>
9.編寫測試類測試
- package cn.com.test;
- import cn.com.ibatis.dao.impl.StuDAOImpl;
- import cn.com.ibatis.entity.Student;
- public class TestStuDAOImpl {
- public static void main(String[] args) {
- StuDAOImpl stuImp = new StuDAOImpl();
- Student stu = new Student();
- stu.setId(3);
- stu.setName("可樂小牛");
- stu.setPwd("123");
- stu.setAge(20);
- stu.set***("男");
- // stuImp.addStu(stu);
- // stuImp.updateById(stu);
- // stuImp.selectStuById(1);
- // stuImp.selectAllStu();
- // stuImp.selectStuByName("可樂大牛");
- // stuImp.delById(2);
- }
- }
一個簡單的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 開發指南.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...
給主人留下些什麼吧!~~ 評論熱議