版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
知識點列123掌握ThreadLocal的使用方法4增刪改查025注 持久化對象和一級緩存機制 對象的三種狀態(tài) 一級緩存機制 【案例1】對象的3種狀態(tài)演示 Hibernate延遲加載機制 基本原理 實現(xiàn)原理 【案例2】延遲加載機制 【案例3】Struts2和Hibernate實現(xiàn)增刪改查(2) 【案例4】在項目中使用延遲加載機制 many-to-one關聯(lián)映射 數(shù)據(jù)表的關聯(lián) many-to-one many-to-one需求下的常見操作 【案例5】many-to-one 持久化對象和一級緩存機制對象的三種狀態(tài)EmpEmpe=newEmp();一級緩存機制一旦用戶對緩存中的數(shù)據(jù)做了修改,session立刻將數(shù)據(jù)更新到數(shù)據(jù)庫中。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();如上所示,當執(zhí)行了 session.save(foo);語句執(zhí)行后,Hibernateinsert mit()操作時,會自勱調(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,一旦用戶對緩存中的數(shù)據(jù)做了修改,session立刻將數(shù)據(jù)更新到數(shù)據(jù)庫中。(案例結(jié)束Hibernate延遲加載機制基本原理調(diào)用實現(xiàn)原理該子類重寫了getXXX方法,在該方法中觸發(fā)了對數(shù)據(jù)庫的。loaditeratorHibernateGBLIBSpring——具體使用ThreadLocal——其實是一個線程為KEY的Map,2】延遲加載機制而當調(diào)用Foofoo=Foofoo=(Foo)session.load(Foo.class,Foo{publicStringgetValue(){return}}Java.class我們并沒有創(chuàng)建Foo$$EnhancerByCGLIB$$b3a0560c這個類,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;語句,從數(shù)據(jù)庫中僅僅將id取出;(案例結(jié)束參考請新增****@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測試(略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="恢復"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測跳轉(zhuǎn)到數(shù)據(jù)更新頁新增****@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"/>測試(略(案例結(jié)束4】在項目中使用延遲加載機制請Struts+Hibernate的jar 修改如果在ProjectDao中調(diào)用session.close()關閉了session,如果ProjectDao中丌調(diào)用session.close方法,這樣就造成一個如何在項目中使用延緩加載?OpesoIewpackagepackagepublicclassTestThreadLocalpublicstaticvoidmain(String[]{Somes1=SomeFactory.getSome();}publicstaticvoid{Somes2=SomeFactory.getSome();}}classSome{classSomeFactorypublicstaticSomegetSome()returnnew}}f(s2mian(s1首先注意一點,f()方法和main()方法同樣都是在一個線程中,有相同的線程號。的Some對象是同一個。packagepackageimportimportpublicclassTestThreadLocalpublicstaticvoidmain(String[]{Somes1=SomeFactory.getSome();}publicstaticvoid{Somes2=SomeFactory.getSome();}}classSome{classSomeFactoryprivatestaticMap<Long,Some>map=newHashMap<Long,publicstaticSomegetSome()Somesome=if(some==some=new}}}}return假設Some就是Session,如果使用這種機制獲取Session,當同一個用戶瀏覽器丌論怎么調(diào)用session都是同一個(叧要在相同的線程中)。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}}測試(略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()關閉Session。"-//Hibernate/HibernateConfigurationDTD3.0//EN"<propertyname="connection.url"><property<property<!--<property<property <propertypackagepackageimportimportimportpublicclassHibernateUtilsprivatestaticConfigurationprivatestaticSessionFactorystaticconf=newfactory=}**}}publicstaticSessiongetSession()return}*publicstaticvoidcloseSession()Sessionsession=if(session!={}}(案例結(jié)束many-to-one關聯(lián)映射數(shù)據(jù)表的關聯(lián)數(shù)據(jù)表的關聯(lián)丌一定導致Hibernate關聯(lián)映射。t_dept(t_id)的時候步驟1privateDept}<many-to-onename="dept"class="Dept"many-to-one需求下的常見操作1)保存2)取出Emp(帶著Dept)Empemp=<many-to-one...Dept還是用單獨的SQLfetchjoinSQL3)查詢Emp(帶著Dept)HQL:fromDept還是用單獨的HQLfromEmpeleftouterjoinfetche.dept4)根據(jù)特定條件查詢Emp(帶著Dept)HQL:fromEmpewhereHQLfromE='...'5)根據(jù)Dept的屬性查詢Emp(帶著Dept)HQL:fromEmpewhereHQL:fromEmpeleftouterjoinfetche.deptwheret_emp注意:這里丌用寫主外鍵關聯(lián)約束也是可以的(現(xiàn)在約束的使用越來越少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對象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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五版食堂泔水處理與環(huán)保設備銷售合同2篇
- 2025年度電子商務平臺承包招商合同范本3篇
- 二零二五版大棚租賃合同綠色環(huán)保附加條款3篇
- 2025年度安全生產(chǎn)風險評估與管理合同集3篇
- 年度鈷基及鈷鎳基競爭策略分析報告
- 2025年暑期實習崗位勞動合同范本3篇
- 2025年度專業(yè)舞臺搭建租賃合同3篇
- 2024-2025學年高中歷史課時分層作業(yè)十二5.1科學社會主義的奠基人馬克思含解析新人教版選修4
- 2025年度環(huán)保節(jié)能零星工程設計與施工一體化合同4篇
- 2025年度現(xiàn)代農(nóng)業(yè)示范區(qū)農(nóng)資集成采購合同3篇
- 類文閱讀:一起長大的玩具(金波)
- 食品公司冷庫崗位風險告知卡
- 《AI營銷畫布:數(shù)字化營銷的落地與實戰(zhàn)》
- 崗位安全培訓考試題參考答案
- 英文書信及信封格式詳解(課堂)課件
- 星巴克的市場營銷策劃方案
- 南京某商城機電安裝施工組織設計
- 醫(yī)療設備托管服務投標方案
- 宗教教職人員備案表
- 麻醉藥品、精神藥品、放射性藥品、醫(yī)療用毒性藥品及藥品類易制毒化學品等特殊管理藥品的使用與管理規(guī)章制度
- 信訪事項復查復核申請書
評論
0/150
提交評論