




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、楊教授工作室 精心創(chuàng)作的優(yōu)秀程序員 職業(yè)提升必讀系列資料目 錄1.1在容器外實(shí)現(xiàn)hibernate框架繼承映射關(guān)系及實(shí)例(第2部分)21.1.1應(yīng)用單元測(cè)試技術(shù)測(cè)試本實(shí)例的應(yīng)用效果21.1.2體驗(yàn)hibernate中l(wèi)azy技術(shù)91.1.3體驗(yàn)多態(tài)查詢(xún)技術(shù)的應(yīng)用101.1.4進(jìn)一步完善本項(xiàng)目的實(shí)例111.1 在容器外實(shí)現(xiàn)hibernate框架繼承映射關(guān)系及實(shí)例(第2部分)1.1.1 應(yīng)用單元測(cè)試技術(shù)測(cè)試本實(shí)例的應(yīng)用效果1、增加一個(gè)dao的接口derivedhibernatedaointerface包名稱(chēng)為er在該接口中添加下面的方法pac
2、kage er;import org.hibernate.hibernateexception;public interface derivedhibernatedaointerface public boolean dofindallpersoninfo() throws hibernateexception;2、提供對(duì)該方法的實(shí)現(xiàn)類(lèi)derivedhibernatedaoimple包名稱(chēng)為com.px1987.sshwebcrm.dao.implepackage com.px1987.sshwebcrm.dao.imple;import
3、 java.util.arraylist;import java.util.list;import mons.logging.log;import mons.logging.logfactory;import org.hibernate.hibernateexception;import org.hibernate.session;import org.hibernate.transaction;import er.derivedhibernatedaointerface;import com.px1987.sshwebcrm.dao.p
4、object.abstractpersonpo;import com.px1987.sshwebcrm.dao.pobject.studentpo;import com.px1987.sshwebcrm.dao.pobject.teacherpo;import com.px1987.sshwebcrm.dao.util.hibernateutil;public class derivedhibernatedaoimple implements derivedhibernatedaointerface private static log log = logfactory.getlog(deri
5、vedhibernatedaoimple.class);public derivedhibernatedaoimple() public boolean dofindallpersoninfo() throws hibernateexception session session=null;transaction tx=null;list findresults = new arraylist();try session = hibernateutil.currentsession();tx = session.begintransaction();list allstudentinfo =
6、session.createquery("from com.px1987.sshwebcrm.dao.pobject.studentpo").list();findresults.addall(allstudentinfo);list allteacherinfo =session.createquery("from com.px1987.sshwebcrm.dao.pobject.teacherpo").list();findresults.addall(allteacherinfo); printallpersoninfo(findresults);
7、 mit();catch (hibernateexception he) log.error("在dofindallpersoninfo方法中出現(xiàn)了hibernateexception錯(cuò)誤", he);throw he;finally hibernateutil.closesession();return true;private void printallpersoninfo (java.util.list allpersonlist) /利用該方法實(shí)現(xiàn)數(shù)據(jù)顯示 java.util.iterator iterator= allpersonlist.iterator();
8、while(iterator.hasnext() abstractpersonpo oneperson=(abstractpersonpo)iterator.next(); if(oneperson instanceof studentpo) int departmentid=(studentpo)oneperson).getdepartment().getid(); string departmentname=(studentpo)oneperson).getdepartment().getdepname(); int studentid=(studentpo)oneperson).geti
9、d(); string studentname=(studentpo)oneperson).getpersonname(); int studentlevel=(studentpo)oneperson).getstudentlevel (); string studentmajor=(studentpo)oneperson).getstudentmajor(); system.out.print("departmentid="+departmentid+"t"); system.out.print("departmentname="+
10、departmentname+"t"); system.out.print("studentid="+studentid+"t"); system.out.print("studentname="+studentname+"t"); system.out.print("studentlevel="+studentlevel+"n"); system.out.print("studentmajor="+studentmajor+"
11、;n"); else int departmentid=(teacherpo)oneperson).getdepartment().getid(); string departmentname=(teacherpo)oneperson).getdepartment().getdepname(); int teacherid=(teacherpo)oneperson).getid(); string teachername=(teacherpo)oneperson).getpersonname(); string coursename=(teacherpo)oneperson).get
12、coursename(); float teachermonthsalary=(teacherpo)oneperson).getmonthsalary(); system.out.print("departmentid="+departmentid+"t"); system.out.print("departmentname="+departmentname+"t"); system.out.print("teacherid="+teacherid+"t"); system.
13、out.print("teachername="+teachername+"t"); system.out.print("teachermonthsalary="+teachermonthsalary+"n"); system.out.print("coursename="+coursename+"n"); 3、添加測(cè)試用例testderivedhibernatedaoimple包名稱(chēng)為com.px1987.hexample.dao.test,被測(cè)試類(lèi)為derivedhibe
14、rnatedaoimple在測(cè)試用例中對(duì)該方法進(jìn)行測(cè)試package com.px1987.hexample.dao.test;import junit.framework.assert;import org.junit.afterclass;import org.junit.beforeclass;import org.junit.test;import com.px1987.sshwebcrm.dao.imple.derivedhibernatedaoimple;import er.derivedhibernatedaointerfa
15、ce;public class testderivedhibernatedaoimple static derivedhibernatedaointerface onederivedhibernatedaobean=null;beforeclasspublic static void setupbeforeclass() throws exception onederivedhibernatedaobean=new derivedhibernatedaoimple();afterclasspublic static void teardownafterclass() throws except
16、ion onederivedhibernatedaobean=null;testpublic void testdofindallpersoninfo() boolean actualreturn=onederivedhibernatedaobean.dofindallpersoninfo();assert.asserttrue(actualreturn);4、執(zhí)行測(cè)試用例將出現(xiàn)下面的結(jié)果1.1.2 體驗(yàn)hibernate中l(wèi)azy技術(shù)如果將對(duì)department表的訪(fǎng)問(wèn)代碼(也就是上面的黑體部分)除掉,然后再次執(zhí)行,將發(fā)現(xiàn)沒(méi)有對(duì)department表進(jìn)行查詢(xún),因?yàn)閔ibernate缺省時(shí)是采
17、用 lazy加載的-只在真正需要該數(shù)據(jù)時(shí),才加載所關(guān)聯(lián)的數(shù)據(jù)。1.1.3 體驗(yàn)多態(tài)查詢(xún)技術(shù)的應(yīng)用將dao中的hql語(yǔ)句改變?yōu)橄旅娴男问絣ist allstudentinfo = session.createquery("from com.px1987.sshwebcrm.dao.pobject.studentpo").list();findresults.addall(allstudentinfo);list allteacherinfo =session.createquery("from com.px1987.sshwebcrm.dao.pobject.tea
18、cherpo").list();list allstudentinfo =session.createquery("from com.px1987.sshwebcrm.dao.pobject.abstractpersonpo").list();findresults.addall(allstudentinfo);觀察執(zhí)行的結(jié)果是否與前面的采用兩條hql語(yǔ)句的效果相同。1.1.4 進(jìn)一步完善本項(xiàng)目的實(shí)例1、在dao組件中再增加一個(gè)doinsertallpersoninfo方法以實(shí)現(xiàn)保存數(shù)據(jù)的功能2、修改derivedhibernatedaointerfacepack
19、age er;import org.hibernate.hibernateexception;public interface derivedhibernatedaointerface public boolean dofindallpersoninfo() throws hibernateexception;public boolean doinsertallpersoninfo() throws hibernateexception ;3、修改實(shí)現(xiàn)類(lèi)代碼示例public boolean doinsertallpersoninfo()
20、throws hibernateexception session session=null;transaction tx=null;try session = hibernateutil.currentsession();tx = session.begintransaction(); studentpo onestudent=new studentpo();onestudent.setpersonname("zhao");onestudent.setstudentlevel(4);onestudent.setstudentmajor("計(jì)算機(jī)應(yīng)用")
21、; int departmentid=1;departmentpo onedepartment=(departmentpo)session.load(departmentpo.class,new integer(departmentid); onestudent.setdepartment(onedepartment); session.save(onestudent); mit();catch (hibernateexception he) log.error("在doinsertallpersoninfo方法中出現(xiàn)了hibernateexception錯(cuò)誤", he);
22、throw he;finally hibernateutil.closesession();return true;4、修改測(cè)試用例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.derivedhibernatedaoimple;import er.derivedhibernatedaoi
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 木工班班組勞務(wù)分包合同
- 仔豬購(gòu)銷(xiāo)合同協(xié)議書(shū)
- 深圳住房租賃合同書(shū)
- 辦公用品采購(gòu)買(mǎi)賣(mài)合同
- 衢州職業(yè)技術(shù)學(xué)院《搜索引擎營(yíng)銷(xiāo)》2023-2024學(xué)年第二學(xué)期期末試卷
- 山東化工職業(yè)學(xué)院《英語(yǔ)學(xué)科教學(xué)設(shè)計(jì)與技能訓(xùn)練》2023-2024學(xué)年第二學(xué)期期末試卷
- 三江學(xué)院《世界古代史(下)》2023-2024學(xué)年第二學(xué)期期末試卷
- 廣東食品藥品職業(yè)學(xué)院《醫(yī)務(wù)社會(huì)工作》2023-2024學(xué)年第二學(xué)期期末試卷
- 西安交通大學(xué)城市學(xué)院《環(huán)境化學(xué)Ⅱ》2023-2024學(xué)年第二學(xué)期期末試卷
- 貴州財(cái)經(jīng)大學(xué)《中學(xué)政治課教師技能訓(xùn)練》2023-2024學(xué)年第二學(xué)期期末試卷
- 2021年陜西西安亮麗電力集團(tuán)有限責(zé)任公司招聘筆試試題
- 高中英語(yǔ)-Studying abroad教學(xué)課件設(shè)計(jì)
- 原材料取樣檢測(cè)安全操作規(guī)程
- 創(chuàng)新思維與方法(第2版)PPT全套完整教學(xué)課件
- (5.3.2)-2.2雜草的分類(lèi)農(nóng)田雜草及防除學(xué)
- 人教部編道德與法治五年級(jí)下冊(cè)單元計(jì)劃
- 天津武清區(qū)事業(yè)單位考試真題2022
- 鐵路營(yíng)業(yè)線(xiàn)施工安全管理培訓(xùn)課件
- 旅行社運(yùn)營(yíng)實(shí)務(wù)電子課件 1.2 了解旅行社核心業(yè)務(wù)部門(mén)
- 綜合交通運(yùn)輸體系認(rèn)知
- GM/T 0115-2021信息系統(tǒng)密碼應(yīng)用測(cè)評(píng)要求
評(píng)論
0/150
提交評(píng)論