




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、楊教授工作室 精心創(chuàng)作的優(yōu)秀程序員 職業(yè)提升必讀系列資料目 錄1.1在容器外實現(xiàn)hibernate的包含關(guān)系及應用實例(mysql第2部分)21.1.1應用單元測試用例測試本示例的應用效果21.1.2體驗set集合映射技術(shù)及應用實例51.1.3體驗的應用技術(shù)及示例181.1 在容器外實現(xiàn)hibernate的包含關(guān)系及應用實例(mysql第2部分)1.1.1 應用單元測試用例測試本示例的應用效果1、在項目中添加一個測試用例類testcontainhibernatedaoimple包名稱為com.px1987.hexample.dao.test,被測試的類為containhibernatedaoi
2、mple2、編程該測試用例類package com.px1987.hexample.dao.test;import java.io.unsupportedencodingexception;import junit.framework.assert;import org.hibernate.hibernateexception;import org.junit.afterclass;import org.junit.beforeclass;import org.junit.test;import com.px1987.sshwebcrm.dao.imple.containhibernateda
3、oimple;import er.containhibernatedaointerface;public class testcontainhibernatedaoimple static containhibernatedaointerface containhibernatedaoimplebean=null;beforeclasspublic static void setupbeforeclass() throws exception containhibernatedaoimplebean=new containhibernat
4、edaoimple();afterclasspublic static void teardownafterclass() throws exception containhibernatedaoimplebean=null;testpublic void testdoinsertuserinfodatatodb()throws hibernateexception, unsupportedencodingexception boolean actualreturn=containhibernatedaoimplebean.doinsertuserinfodatatodb();assert.a
5、sserttrue(actualreturn);testpublic void testdoselectuserinfodatafromdb() string selecthql=from com.px1987.sshwebcrm.dao.pobject.userinfomailpo;boolean actualreturn=containhibernatedaoimplebean.doselectuserinfodatafromdb(selecthql);assert.asserttrue(actualreturn);3、執(zhí)行本測試用例在數(shù)據(jù)庫表中也增加了一條記錄在控制臺中的輸出1.1.2
6、體驗set集合映射技術(shù)及應用實例1、userinfo_setpo、包名稱為com.px1987.sshwebcrm.dao.pobjectprivate string username;private string id;private set addrs = new hashset();提供一個幫助方法 public void addaddress(string addr) /該方法為“幫助方法” addrs.add(addr); 和 overridepublic int hashcode() final int prime = 31;int result = 1;result = prim
7、e * result + (addrs = null) ? 0 : addrs.hashcode();result = prime * result + (id = null) ? 0 : id.hashcode();result = prime * result+ (username = null) ? 0 : username.hashcode();return result;overridepublic boolean equals(object obj) if (this = obj)return true;if (obj = null)return false;if (getclas
8、s() != obj.getclass()return false;final userinfo_set other = (userinfo_set) obj;if (addrs = null) if (other.addrs != null)return false; else if (!addrs.equals(other.addrs)return false;if (id = null) if (other.id != null)return false; else if (!id.equals(other.id)return false;if (username = null) if
9、(other.username != null)return false; else if (!username.equals(other.username)return false;return true;最后為下面的結(jié)果代碼package com.px1987.sshwebcrm.dao.pobject;import java.util.hashset;import java.util.set;public class userinfo_setpo private string username;overridepublic int hashcode() final int prime =
10、 31;int result = 1;result = prime * result + (addrs = null) ? 0 : addrs.hashcode();result = prime * result + (id = null) ? 0 : id.hashcode();result = prime * result+ (username = null) ? 0 : username.hashcode();return result;overridepublic boolean equals(object obj) if (this = obj)return true;if (obj
11、 = null)return false;if (getclass() != obj.getclass()return false;userinfo_setpo other = (userinfo_setpo) obj;if (addrs = null) if (other.addrs != null)return false; else if (!addrs.equals(other.addrs)return false;if (id = null) if (other.id != null)return false; else if (!id.equals(other.id)return
12、false;if (username = null) if (other.username != null)return false; else if (!username.equals(other.username)return false;return true;public string getusername() return username;public void setusername(string username) this.username = username;public string getid() return id;public void setid(string
13、 id) this.id = id;public set getaddrs() return addrs;public void setaddrs(set addrs) this.addrs = addrs;private string id;private set addrs = new hashset(); public void addaddress(string addr) /該方法為“幫助方法” addrs.add(addr); public userinfo_setpo() 2、addresspo類、包名稱為com.px1987.sshwebcrm.dao.pobjectpriva
14、te string addresstext;package com.px1987.sshwebcrm.dao.pobject;public class addresspo overridepublic int hashcode() final int prime = 31;int result = 1;result = prime * result+ (addresstext = null) ? 0 : addresstext.hashcode();return result;overridepublic boolean equals(object obj) if (this = obj)re
15、turn true;if (obj = null)return false;if (getclass() != obj.getclass()return false;addresspo other = (addresspo) obj;if (addresstext = null) if (other.addresstext != null)return false; else if (!addresstext.equals(other.addresstext)return false;return true;private string addresstext;public string ge
16、taddresstext() return addresstext;public void setaddresstext(string addresstext) this.addresstext = addresstext;public addresspo() 3、對應的數(shù)據(jù)庫表 userinfo_set和addressinfo_set數(shù)據(jù)庫表結(jié)構(gòu)如下數(shù)據(jù)庫表之間必須滿足主外鍵的關(guān)系要求。4、建立它們之間的映射關(guān)系 ?(1)在項目中再添加userinfopo_set.hbm.xml 4、hibernate.cfg.xml文件的內(nèi)容5、編程dao方法(1)修改containhibernateda
17、ointerface接口package er;import org.hibernate.hibernateexception;public interface containhibernatedaointerface public boolean doinsertuserinfodatatodb() throws hibernateexception,java.io.unsupportedencodingexception; public boolean doselectuserinfodatafromdb(string selecthq
18、l) throws hibernateexception; public boolean doinsertuserandmailtodb() throws hibernateexception; public boolean doselectuserandmaildatafromdb(string selecthql) throws hibernateexception;(2)修改containhibernatedaoimple類添加實現(xiàn)方法 public boolean doinsertuserandmailtodb() throws hibernateexceptionsession se
19、ssion=null;transaction tx=null; try session = hibernateutil.currentsession(); tx = session.begintransaction(); userinfo_setpo oneuser = new userinfo_setpo(); oneuser.setusername(yang); oneuser.addaddress(); oneuser.addaddress(); oneuser.addaddress(); sess
20、ion.save(oneuser); mit(); catch (hibernateexception he) if (tx != null) tx.rollback(); log.error(在doinsertuserandmailtodb方法中出現(xiàn)了hibernateexception錯誤, he); throw he; finally hibernateutil.closesession(); return true; public boolean doselectuserandmaildatafromdb(string selecthql) throws hibernate
21、exceptionsession session=null;transaction tx=null;trysession = hibernateutil.currentsession();tx= session.begintransaction();list alluserinfo_set = session.createquery(selecthql).list();for (listiterator iterator = alluserinfo_set.listiterator(); iterator.hasnext(); ) userinfo_setpo oneuserinfo_set
22、= (userinfo_setpo) iterator.next(); system.out.println(id=+oneuserinfo_set.getid() + tusername= + oneuserinfo_set.getusername(); set alladdreess=oneuserinfo_set.getaddrs();iterator oneaddress=alladdreess.iterator();while(oneaddress.hasnext()string useraddress=(string)oneaddress.next();system.out.pri
23、ntln(address info:+useraddress); mit(); catch(hibernateexception he) log.error(在doselectuserandmaildatafromdb方法中出現(xiàn)了hibernateexception錯誤, he); throw he; finally hibernateutil.closesession(); return true;6、修改測試用例testpublic void testdoinsertuserandmailtodb() throws exceptionboolean actualreturn=c
24、ontainhibernatedaoimplebean.doinsertuserandmailtodb();assert.asserttrue(actualreturn);testpublic void testdoselectuserandmaildatafromdb() throws exceptionstring selecthql=from com.px1987.sshwebcrm.dao.pobject.userinfo_setpo;boolean actualreturn=containhibernatedaoimplebean.doselectuserandmaildatafro
25、mdb(selecthql);assert.asserttrue(actualreturn);對前面的各個測試方法可以采用ignore臨時屏蔽掉。7、執(zhí)行測試用例同時在控制臺中產(chǎn)生的輸出同時在用戶的信息數(shù)據(jù)庫表中將出現(xiàn)下面的數(shù)據(jù)另外,在用戶的地址信息數(shù)據(jù)庫表中將出現(xiàn)下面的數(shù)據(jù)1.1.3 體驗的應用技術(shù)及示例1、修改前面的userinfo_setpo類,以增加一個set mailaddrs成員定義private set mailaddrs = new hashset(); public set getmailaddrs() return mailaddrs; public void setmai
26、laddrs(set mailaddrs) this.mailaddrs = mailaddrs; public void addmailaddress(emailpo emailaddr) /該方法為“幫助方法” mailaddrs.add(emailaddr); 2、而emailpo類可以采用前面已經(jīng)生成的emailpo.java它代表集合中的元素類型-此時,不再是string類型,而是我們自己定義的類的類型。3、再新建一個數(shù)據(jù)庫表emailinfo當然,原來的數(shù)據(jù)庫表不變。因此,emailpo類中的代碼實現(xiàn)了重用(再次被使用)。l userinfo_set表結(jié)構(gòu)為l addressinf
27、o_set表結(jié)構(gòu)為4、修改該userinfo_set類的userinfo_set.hbm.xml文件以增加對mailaddrs的集合的定義 5、修改dao組件(1)doinsertuserandtwomailtodb方法的代碼public boolean doinsertuserandtwomailtodb() throws hibernateexceptionsession session=null;transaction tx=null; try session = hibernateutil.currentsession(); tx = session.begintransaction(
28、); userinfo_setpo oneuser = new userinfo_setpo(); oneuser.setusername(yang); oneuser.addaddress(); oneuser.addaddress(); oneuser.addaddress(); emailpo oneemail=new emailpo(); oneemail.setmailname(abc); oneemail.sethost(); emailpo twoemail=new email
29、po(); twoemail.setmailname(trainict); twoemail.sethost(); emailpo threeemail=new emailpo(); threeemail.setmailname(yangsb); threeemail.sethost(); oneuser.addmailaddress(oneemail); oneuser.addmailaddress(twoemail); oneuser.addmailaddress(threeemail); session.save(oneuser);
30、mit(); catch (hibernateexception he) if (tx != null) tx.rollback(); log.error(在doinsertuserandtwomailtodb方法中出現(xiàn)了hibernateexception錯誤, he); throw he; finally hibernateutil.closesession(); return true;(2)doselectuserandtwomaildatafromdb方法public void doselectuserandtwomaildatafromdb(string selecthql) throws hibernateexceptionsession session=null;transaction tx=null;trysession = hibernateutil.currentsession();tx= session.begintrans
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 科技產(chǎn)品如何通過社交媒體提升用戶粘性
- 構(gòu)建多元文化背景下的全球老年大學網(wǎng)絡(luò)
- 2025年湖北云學名校聯(lián)盟高三年級2月聯(lián)考語文試卷
- 科技發(fā)展趨勢下的管理決策方法
- 施工中標合同范本
- 2025-2030年中國甲醇制芳烴行業(yè)發(fā)展狀況及營銷戰(zhàn)略研究報告
- 訂婚押金合同范本
- 2025-2030年中國汽車釬焊鋁合金散熱器市場現(xiàn)狀分析規(guī)劃研究報告
- 2025-2030年中國汽車點火線圈市場運行態(tài)勢及投資發(fā)展前景調(diào)研報告
- 2025-2030年中國汽車摩擦材料行業(yè)運行狀況及發(fā)展前景分析報告
- 青春期的婦科知識講座
- JTT589-2004 水泥混凝土路面嵌縫密封材料
- 《社區(qū)康復》課件-第三章 社區(qū)康復的實施
- 中職生心理健康教育全套教學課件
- JC-T 2704-2022 聚酯纖維裝飾吸聲板
- WTE朗文英語2B 單詞卡片
- 初三物理復習計劃詳細計劃
- 汽車懸架概述
- 心房顫動的教學查房課件
- 重大緊急情況的報告模板
- 二人合伙開寵物店協(xié)議書(5篇)
評論
0/150
提交評論