版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
知識(shí)點(diǎn)列123掌握ThreadLocal的使用方法4增刪改查025注 持久化對(duì)象和一級(jí)緩存機(jī)制 對(duì)象的三種狀態(tài) 一級(jí)緩存機(jī)制 【案例1】對(duì)象的3種狀態(tài)演示 Hibernate延遲加載機(jī)制 基本原理 實(shí)現(xiàn)原理 【案例2】延遲加載機(jī)制 【案例3】Struts2和Hibernate實(shí)現(xiàn)增刪改查(2) 【案例4】在項(xiàng)目中使用延遲加載機(jī)制 many-to-one關(guān)聯(lián)映射 數(shù)據(jù)表的關(guān)聯(lián) many-to-one many-to-one需求下的常見(jiàn)操作 【案例5】many-to-one 持久化對(duì)象和一級(jí)緩存機(jī)制對(duì)象的三種狀態(tài)EmpEmpe=newEmp();一級(jí)緩存機(jī)制一旦用戶對(duì)緩存中的數(shù)據(jù)做了修改,session立刻將數(shù)據(jù)更新到數(shù)據(jù)庫(kù)中。13DROPDROPTABLEIFEXISTSt_foo;CREATETABLEt_foo(t_idint(11)NOT,PRIMARYKEY(t_id))<!DOCTYPEhibernate- DTD" <classname="Foo"<idname="id"type="java.lang.Integer"<propertyname="value"type="java.lang.String"column="t_value" <generatorpackagepackageimportorg.hibernate.Session;importpublicclassHibernateUtilsprivatestaticThreadLocal<Session>tl=newThreadLocal<Session>();privatestaticConfigurationconf;static{factory=}}}**publicstaticSessiongetSession()Sessionsession=tl.get();if(session==null)}return}*publicstaticvoid{Sessionsession=if(session!={);}}packageimportjava.util.Date;packageimportjava.util.Date;importjava.util.List;importorg.hibernate.Query;importorg.hibernate.Session;importorg.junit.Test;}}}importimportpublicclass{{Foofoo=newFoo();Sessionsession=HibernateUtils.getSession();Transactiontx=session.beginTransaction();如上所示,當(dāng)執(zhí)行了 session.save(foo);語(yǔ)句執(zhí)行后,Hibernateinsert mit()操作時(shí),會(huì)自勱調(diào)用session.flash() 所以,執(zhí)行了2次update操作新建getfoo1foo2Foofoo2=(Foo)session.get(Foo.class,FooFoofoo2=(Foo)session.get(Foo.class,Foofoo1=(Foo)session.get(Foo.class,一旦用戶對(duì)緩存中的數(shù)據(jù)做了修改,session立刻將數(shù)據(jù)更新到數(shù)據(jù)庫(kù)中。(案例結(jié)束Hibernate延遲加載機(jī)制基本原理調(diào)用實(shí)現(xiàn)原理該子類(lèi)重寫(xiě)了getXXX方法,在該方法中觸發(fā)了對(duì)數(shù)據(jù)庫(kù)的。loaditeratorHibernateGBLIBSpring——具體使用ThreadLocal——其實(shí)是一個(gè)線程為KEY的Map,2】延遲加載機(jī)制而當(dāng)調(diào)用Foofoo=Foofoo=(Foo)session.load(Foo.class,Foo{publicStringgetValue(){return}}Java.class我們并沒(méi)有創(chuàng)建Foo$$EnhancerByCGLIB$$b3a0560c這個(gè)類(lèi),cglib-2.1.3.jar調(diào)用了如下所示,F(xiàn)oo$$EnhancerByCGLIB$$b3a0560ccglibFoofinal package增加packageimportimportimportimportorg.hibernate.Query;importorg.junit.Test;importimportpublicclass{publicvoidtestIterator()Sessionsession=HibernateUtils.getSession();Queryquery=session.createQuery("fromFoo");//List<Foo>fooList=Iterator<Foo>fooIt=//selectt_idfromwhile{Foofoo=//select*fromt_foowheret_id=?}}}先執(zhí)行selectt_idfromt_foo;語(yǔ)句,從數(shù)據(jù)庫(kù)中僅僅將id取出;(案例結(jié)束參考請(qǐng)新增****@parampublicvoiddelete(Integer[]{Sessionsession=Transactiontx=for(Integerid:projects)Projectproject=newProject();}}packagepackageimportpublicclassProjectDeleteActionextendsBaseActionprivateProjectDaoprojectDao=new//privateInteger[]//OutputpublicStringexecute()throws{return}}}publicInteger[]getProjects()return}publicvoidsetProjects(Integer[]projects)jects=}<!DOCTYPEstruts"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1//EN" <actionname="list"<result<action<result<actionname="create"<result<action<%@tagliburi="/struts-tags"<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01//EN" <linkhref="../css/style.css"type="text/css"rel="stylesheet"<script<body<divcellPadding="0"width="100%"<br<tableclass="tableborder"cellSpacing="0"cellPadding="0"width="100%"border="0"><tr<td <td<td<td<td<s:iterator
<td<inputtype="checkbox"<tdclass="altbg2"href="load.action?project.id=${id}"><b><s:propertyvalue="no"/></b></a><td<b><s:propertyvalue="name"/><td<b><s:propertyvalue="startDate"/><td<b><s:propertyvalue="endDate"/><br測(cè)試(略findById**@parampublicProjectfindById(Integer{Sessionsession=Projectproject=(Project)session.get(Project.class,id);return}ject.po.Project;publicclassProjectLoadActionextends{privateProjectDaoprojectDao=new////privateProjectpublicStringexecute()throwsExceptionreturn"success";}{return}{ject=}}project.struts.xml增加<actionname="load"><<actionname="load"<result<linkhref="css/style.css"type="text/css"rel="stylesheet"<body<div<tableclass="guide"cellSpacing="0"cellPadding="0"width="100%"<br<formid=""action=""value=""/><tableclass="tableborder"cellSpacing="0"<tr<tdclass="altbg1"<tdclass="altbg2"valign="middle"<inputtype="text"name="no"<spanstyle="color:red"><tdclass="altbg1"<td<inputtype="text"name="name"<spanstyle="color:red"><tdclass="altbg1"<td<inputtype="text"name="startDate"<spanstyle="color:<tdclass="altbg1"<td<inputtype="text"name="endDate"<spanstyle="color:<br<inputclass="buttontype="button"value="取消"name="settingsubmit"/><inputclass="buttontype="button"value="恢復(fù)"name="settingsubmit"/><inputclass="button"type="submit"value=name="settingsubmit"/><tableclass="tableborder"cellSpacing="0"cellPadding="0"width="100%"border="0"><tr<td <td<td<td<td<s:iterator
<td<inputtype="checkbox"<tdclass="altbg2"<a<b><s:propertyvalue="no"/><td<b><s:propertyvalue="name"/><<td<b><s:propertyvalue="startDate"/><td<b><s:propertyvalue="endDate"/><br測(cè)跳轉(zhuǎn)到數(shù)據(jù)更新頁(yè)新增****@parampublicvoidupdate(Project{Sessionsession=openSession();}ject.po.Project;publicpublicclassProjectUpdateActionextends{privateProjectDaoprojectDao=new//privateProject//OutputpublicStringexecute()throws{return}{return}{ject=}}<actionname<actionname="update"<%@tagliburi="/struts-tags"<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01//EN" <linkhref="../css/style.css"type="text/css"rel="stylesheet"<body<divcellPadding="0"width="100%"<br<s:formid="settings"action="update"method="post"<s:hidden<tableclass="tableborder"cellSpacing="0"cellPadding="0"width="100%"border="0"><tr<tdclass="altbg1"<tdclass="altbg2"valign="middle"<s:textfieldname="project.no"<spanstyle="color:red"><tdclass="altbg1"<td<spanstyle="color:red"><tdclass="altbg1"<td<s:textfield<spanstyle="color:<tdclass="altbg1"<td<s:textfield<spanstyle="color:<brname="settingsubmit"/>name="settingsubmit"/><inputclass="button"type="submitvalue=name="settingsubmit"/>測(cè)試(略(案例結(jié)束4】在項(xiàng)目中使用延遲加載機(jī)制請(qǐng)Struts+Hibernate的jar 修改如果在ProjectDao中調(diào)用session.close()關(guān)閉了session,如果ProjectDao中丌調(diào)用session.close方法,這樣就造成一個(gè)如何在項(xiàng)目中使用延緩加載?OpesoIewpackagepackagepublicclassTestThreadLocalpublicstaticvoidmain(String[]{Somes1=SomeFactory.getSome();}publicstaticvoid{Somes2=SomeFactory.getSome();}}classSome{classSomeFactorypublicstaticSomegetSome()returnnew}}f(s2mian(s1首先注意一點(diǎn),f()方法和main()方法同樣都是在一個(gè)線程中,有相同的線程號(hào)。的Some對(duì)象是同一個(gè)。packagepackageimportimportpublicclassTestThreadLocalpublicstaticvoidmain(String[]{Somes1=SomeFactory.getSome();}publicstaticvoid{Somes2=SomeFactory.getSome();}}classSome{classSomeFactoryprivatestaticMap<Long,Some>map=newHashMap<Long,publicstaticSomegetSome()Somesome=if(some==some=new}}}}return假設(shè)Some就是Session,如果使用這種機(jī)制獲取Session,當(dāng)同一個(gè)用戶瀏覽器丌論怎么調(diào)用session都是同一個(gè)(叧要在相同的線程中)。packagepackagepublicclassTestThreadLocalpublicstaticvoidmain(String[]{Somes1=SomeFactory.getSome();}publicstaticvoid{Somes2=Somes2=SomeFactory.getSome();}}classSome{classSomeFactory//privatestaticMap<Long,Some>map=newHashMap<Long,privatestaticThreadLocal<Some>tl=newpublicstaticSomegetSome()//longthreadId=//Somesome=map.get(threadId);Somesome=tl.get();if(some==null)some=new//map.put(threadId,}return}}測(cè)試(略1(推薦packageimportpackageimportimportimportimportpublicclassHibUtilprivatestaticConfigurationprivatestaticSessionFactoryconf=new}publicstaticSessionreturn}}packagepackageimportorg.hibernate.Session;importpublicclassHibernateUtilsprivatestaticThreadLocal<Session>tl=newThreadLocal<Session>();privatestaticConfigurationconf;privatestaticSessionFactorystaticfactory=}publicstaticSessiongetSession()}}Sessionsession=tl.get();if(session==null)}return}publicstaticvoid{Sessionsession=tl.get();if(session!=null){);}}在器中調(diào)用closeSession()關(guān)閉Session。"-//Hibernate/HibernateConfigurationDTD3.0//EN"<propertyname="connection.url"><property<property<!--<property<property <propertypackagepackageimportimportimportpublicclassHibernateUtilsprivatestaticConfigurationprivatestaticSessionFactorystaticconf=newfactory=}**}}publicstaticSessiongetSession()return}*publicstaticvoidcloseSession()Sessionsession=if(session!={}}(案例結(jié)束many-to-one關(guān)聯(lián)映射數(shù)據(jù)表的關(guān)聯(lián)數(shù)據(jù)表的關(guān)聯(lián)丌一定導(dǎo)致Hibernate關(guān)聯(lián)映射。t_dept(t_id)的時(shí)候步驟1privateDept}<many-to-onename="dept"class="Dept"many-to-one需求下的常見(jiàn)操作1)保存2)取出Emp(帶著Dept)Empemp=<many-to-one...Dept還是用單獨(dú)的SQLfetchjoinSQL3)查詢(xún)Emp(帶著Dept)HQL:fromDept還是用單獨(dú)的HQLfromEmpeleftouterjoinfetche.dept4)根據(jù)特定條件查詢(xún)Emp(帶著Dept)HQL:fromEmpewhereHQLfromE='...'5)根據(jù)Dept的屬性查詢(xún)Emp(帶著Dept)HQL:fromEmpewhereHQL:fromEmpeleftouterjoinfetche.deptwheret_emp注意:這里丌用寫(xiě)主外鍵關(guān)聯(lián)約束也是可以的(現(xiàn)在約束的使用越來(lái)越少PRIMARYKEYt_loct_locvarchar(200)NOTPRIMARYKEY)insertintot_dept(t_name,t_loc))DROPTABLEIFEXISTSCREATETABLEt_deptDROPTABLEIFEXISTSCREATETABLEt_empt_idint(11)NOTNULLt_namevarchar(50)NOTNULL,t_salarydouble(9,2)NOTNULL,t_hire_datedateNOTNULL,t_registerchar(1)NOTt_idint(11)NOTNULLt_dept_idint(11)NOTEmpDeptEmp EmpsDeptDept DeptEmpsItem OrderItem packagepackageimportpublicclass{privateIntegerid;privateStringname;privatedoublesalary;privateDatehireDate;privateDatelastLogin;publicIntegergetDeptId(){returnpublicvoidsetDeptId(IntegerdeptId)this.deptId=publicIntegergetId(){returnpublicvoidsetId(Integerid){this.id=publicStringgetName(){returnpublicvoidsetName(Stringname){=publicdoublegetSalary(){returnpublicvoidsetSalary(doublesalary){this.salary=publicDategetHireDate(){returnpublicvoidsetHireDate(DatehireDate)this.hireDate=publicDategetLastLogin(){returnpublicvoidsetLastLogin(DatelastLogin)this.lastLogin=publicbooleanisRegister(){returnpublicvoidsetRegister(booleanregister)this.register=}privateInteger<!DOCTYPEhibernate- DTD" <classname="Emp"<idname="id"type="integer"<generator<propertyname="name"type="string"column="t_name"<propertyname="salary"type="double"column="t_salary"<propertyname="hireDate"type="date"column="t_hire_date" packagepackagepublicclass{privateIntegerid;privateStringname;publicIntegergetId(){returnpublicvoidsetId(Integerid){this.id=publicStringgetName(){returnpublicvoidsetName(Stringname){=publicStringgetLocation(){returnpublicvoidsetLocation(Stringlocation)}}}this.location=<!DOCTYPEhibernate- DTD" <classname="Dept"<idname="id"type="integer"<generator<propertyname="name"type="string"column="t_name"<propertyname="location"type="string"column="t_loc" many-to-onemany-to-onepackageimportpublicclass{privateIntegerid;privateStringname;privatedoublesalary;privateDatehireDate;privateDatelastLogin;privateDeptprivateDeptpublicpublicvoidsetDept(Deptdept){this.dept=publicDeptgetDept(){returnpublicIntegergetId(){returnpublicvoidsetId(Integerid){this.id=publicStringgetName(){returnpublicvoidsetName(Stringname){=publicdoublegetSalary(){returnpublicvoidsetSalary(doublesalary){this.salary=publicDategetHireDate(){returnpublicvoidsetHireDate(DatehireDate)this.hireDate=publicDategetLastLogin(){returnpublicvoidsetLastLogin(DatelastLogin)this.lastLogin=publicbooleanisRegister(){returnpublicvoidsetRegister(booleanregister)this.register=}<!DOCTYPEhibernate- "
DTD <classname="Emp"<idname="id"type="integer"<generator<propertyname="name"type="string"column="t_name"<propertyname="salary"type="double"column="t_salary"<propertyname="hireDate"type="date"column="t_hire_date"<many-to-onename="dept"class="Dept"<many-to-onename="dept"class="Dept"column="t_dept_id" 提供的<方方配置文件"-//Hibernate/HibernateConfigurationDTD <propertyname="connection.url"><property<propertyHibernate<!--<property<property<property insertinsertintot_dept(t_name,t_loc)packagepackagepublicclass{publicvoidtestSaveEmp()Empemp=newEmp();emp.setHireDate(newDate());Deptdept=newDept();Sessionsession=HibernateUtils.getSession();Transactiontx=session.beginTransaction();}}packagepackageimportimportimportimportpublicclass{publicvoidtestGetEmp()Sessionsession=HibernateUtils.getSession();Empemp=(Emp)session.get(Emp.class,1);}}}Hibernate:selectdept0_.t_idast1_1_0_,dept0_.t_nameasHibernate:selectdept0_.t_idast1_1_0_,dept0_.t_nameast2_1_0_,dept0_.t_locast3_1_0_fromt_deptdept0_wheredept0_.t_id=?Hibernate:selectemp0_.t_idast1_0_0_,emp0_.t_nameast2_0_0_,emp0_.t_salaryast3_0_0_,emp0_.t_hire_dateast4_0_0_,emp0_.t_last_loginast5_0_0_,emp0_.t_registerast6_0_0_,emp0_.t_dept_idast7_0_0_fromt_empemp0_,fromt_empejoint_dept即使我們使用的是get方法獲取的Emp對(duì)象EmpEmpemp=(Emp)session.get(Emp.class,<many-to-onename="dept"class="Dept"讓我們把兩條SQL合并為一條<many-to-onename="dept"class="Dept"lazy="false"<<many-to-onename="dept"class="Dept"戒<many-to-onename="dept"class="Dept"lazy="proxy"<many-to-onename="dept"class="Dept"lazy="false"Hibernate:Hibernate:selectemp0_.t_idast1_0_1_,emp0_.t_nameast2_0_1_,emp0_.t_salaryast3_0_1_,emp0_
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 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ì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024-2025學(xué)年人教版九年級(jí)英語(yǔ)復(fù)習(xí) 專(zhuān)題05 閱讀理解之說(shuō)明文 【期末必刷15篇】
- 八年級(jí)語(yǔ)文第三次月考卷(考試版A3)【測(cè)試范圍:八上第1~5單元】(湖南長(zhǎng)沙專(zhuān)用)-A4
- 三年級(jí)下冊(cè)英語(yǔ)一課一練-Module 7 unit2 it's warm today∣外研社(三起)(含解析)-1小學(xué)英語(yǔ)教學(xué)教材課件
- 2023年高頻電控氣閥項(xiàng)目融資計(jì)劃書(shū)
- 烹飪?cè)现R(shí)題庫(kù)(附參考答案)
- 養(yǎng)老院老人生活照顧細(xì)節(jié)制度
- 養(yǎng)老院老人健康巡查制度
- 汽車(chē)行業(yè)質(zhì)量管理體系內(nèi)審員模擬試題及答案
- 新造集裝箱檢驗(yàn)合同范本
- 承包道路填石粉工程協(xié)議書(shū)
- 三維超聲輸卵管造影的應(yīng)用課件
- 高壓旋噴樁檢測(cè)方案
- Unit1 My classroom Part A Lets spell(說(shuō)課稿)-2022-2023學(xué)年英語(yǔ)四年級(jí)上冊(cè)
- 查看下載鄭州電視臺(tái)商都頻道簡(jiǎn)介
- 2023年國(guó)開(kāi)大學(xué)期末考復(fù)習(xí)題-10861《理工英語(yǔ)4》
- 公安廉政心談話六篇
- 【要點(diǎn)解讀】《實(shí)踐是檢驗(yàn)真理的唯一標(biāo)準(zhǔn)》論證邏輯圖
- 數(shù)字電子技術(shù)(山東工商學(xué)院)知到章節(jié)答案智慧樹(shù)2023年
- 商務(wù)禮儀(山東聯(lián)盟)知到章節(jié)答案智慧樹(shù)2023年山東財(cái)經(jīng)大學(xué)
- 人教部編版語(yǔ)文九年級(jí)上冊(cè)第一單元分層作業(yè)設(shè)計(jì)
- 《怪奇事物所》讀書(shū)筆記思維導(dǎo)圖PPT模板下載
評(píng)論
0/150
提交評(píng)論