




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
知識(shí)12HibernateHibernate幾種Query3Hibernate二級(jí)緩理解并掌握Hibernate中二級(jí)緩存的含義及使45Hibernate中的鎖機(jī)掌握Hibernate理并發(fā)的操作,悲觀鎖現(xiàn)在注 Ant開發(fā)工具演示 TOC\o"1-2"\h\z\u【案例1】Ant開發(fā)工具演示02 Hibernate 【案例2】HibernateQuery Hibernate二級(jí)緩存 【案例3】二級(jí)緩存 Hibernate查詢緩存 【案例4】查詢緩存 【案例5】鎖機(jī)制_樂觀鎖 Ant開發(fā)工具演示如果想Ant定義一個(gè)Hibernate務(wù),需要用<taskdef<taskdefname=""classname=""classpathref=""<taskdefname="hibernatetool"<taskdefname="hibernatetool"屬中這樣,我們就可以在Ant中使用除Ant自帶的任務(wù)乊外,由Hibernate提供的任務(wù)了<propertyname="dir.src"value="src"<path<fileset <target<!--ANTTask1--<copy<fileset<target<!--ANTTask2--<deletedir="note"<mkdirdir="note"<taskdefname="hibernatetool"classpathref="cp"/><target<hibernatetool<classpathrefid="cp"export="true"format="true" 用亍指明Hibernate<hbm2ddl 的 如果false示叧是生成該sql句,并丌會(huì)運(yùn)行該 HibernatejarAnt查看生成的sql文成一個(gè)alteraltertabledroptableifexistst_dept;droptableifexistscreatetablet_deptt_idintegernotnullauto_increment,t_namevarchar(255),primarykeycreatetablet_empt_idintegernotnullauto_increment,t_namevarchar(255),t_salarydoubleprecision,t_hire_datedate,t_last_logindatetime,t_registerchar(1),t_dept_idinteger,primarykey(t_id)altertableaddindexFK68F5E7DD38E4BB5(t_dept_id),addconstraintFK68F5E7DD38E4BB5foreignkey(t_dept_id)referencest_dept根據(jù)Hibernate.cfg.xml中指定的方言的丌同,可以自勱生成oraclesql戒者mysqlsql<propertyname="dir.src"value="src"<path<fileset <taskdefname="hibernatetool"classpathref="cp"/><target<hibernatetool<classpathrefid="cp"<hbm2javajdk5="true"測(cè)試運(yùn)行Ant了HibernateQuery為2】HibernateQuerypackagepackagepublicclassTestQuerypublicvoidtst1()Sessionsession=HibernateUtils.getSession();Queryquery=session.createQuery("fromFoo");intpage=introwsPerPage=query.setFirstResult((1)*rowsPerPage);List<Foo>fooList=for(FooffooList){}}}如果將方言改為Oralce運(yùn)行程序,會(huì)生oracle運(yùn)行程序,會(huì)生db2**置publicvoidtst2()Queryquery=session.createQuery("fromEmpempwhereList<Emp>fooList=query.list();for(Empe:}}設(shè)置參*publicvoidtst3()Sessionsession=Queryquery=*publicvoidtst3()Sessionsession=Queryquery=query.setParameter(0,"xiaohei");List<Emp>fooList=query.list();for(EmpefooList){e.getId()+""+e.getName()+""+}} DTD" <classname="Emp"<idname="id"type="integer"<generator<<query<![CDATA[fromEmpempwhere <many-to-onename="dept"class="Dept"lazy="false"fetch="join"*勱態(tài)條件查詢publicvoidtst4()*勱態(tài)條件查詢publicvoidtst4()Sessionsession= Queryquery= "fromEmpempwhere Criteriacri= Criteriacri= }}Criteriacrisession.createCriteria(Emp.class);Restrictions.ge("salary",4000.0),List<Emp>empList=for(EmpeempList){e.getId()+""+e.getName()+""+}23from返回selectemp.idfromEmp返回selectemp.id,fromEmp返回selectnewEmp(emp.id,)fromEmppublicvoidtst5()Sessionsession=publicvoidtst5()Sessionsession=}}ListempList=session.createQuery("fromfromselectemp.idfromEmpselectemp.id,fromEmplist合中存放的是的數(shù)selectnewEmp(emp.id,)fromEmp返回的仍然是List<Emp>Emp中叧id和name,并丏丌是持久態(tài)對(duì)改方publicpublicvoidtst6()Transactiontx=session.beginTransaction();Queryquery="deleteElike'%da%'");}publicvoidtst7()Sessionpublicvoidtst7()Sessionsession=Queryquery=session.createSQLQuery("select*fromList<Object[]>list= for(Object[]obj:list)System.out.println(obj[0]+","+obj[1]+","+}}封裝為Dept對(duì)象(這個(gè)方法了解即可)publicvoidtst7()Sessionsession=SQLQuery是QuerySQLQueryquery=session.createSQLQuery("select*from List<Object[]>list= for(Object[]obj:list) System.out.println(obj[0]+","+obj[1]+","+ List<Dept>list=query.list();for(Deptdept:list)+}}因?yàn)橛袝r(shí)候如果碰到個(gè)巨復(fù)雜無比的SQL,直接寫還是方便些。publicpublicvoidtst8()Sessionsession=//返回List<Emp>list1="fromEmpemporderbyemp.salary//返回List<Integer>list2="selectcount(emp.id)fromEmp//List<BigDecimal>list3="selectmax(emp.salary)fromEmp//Queryquery=",count(emp.id)from"List<Object[]>list4=for(Object[]obj:list4)}}"Empempgroupby對(duì)集joinjoinfetchpublicvoidtst9()Queryquery=session.createQuery("fromStudentstujoinfetchstu.coursesc"+"=?");List<Student>stuList=for(Studentstu:{stu.getName()+","+}}Hibernate二級(jí)緩存比如ehcacheHibernate提供了當(dāng)一個(gè)Session開啟,一級(jí)緩存創(chuàng)建;當(dāng)一個(gè)Session關(guān)閉,一級(jí)緩存銷毀?,F(xiàn)在有5個(gè)用戶(5個(gè)線程)Hibernate,假設(shè)用1getId("1")方法查id=1EmpSession沒有id=1Emp5用戶都調(diào)用getId("1")方法查id=1Emp象中我們通過配置一些現(xiàn)成的緩存組件(如ehcache)來實(shí)現(xiàn),同時(shí)我們還可以控制哪些對(duì)象需要用戶級(jí)別的數(shù)據(jù),比如商品信息Product就有必要放入二級(jí)緩存)3】二級(jí)緩存ehcache.jar的Jar請(qǐng)ehcache-1.2.3.jar.zipehcache寫一個(gè)配置文件<!--Setsthepathtothedirectorywherecache.datafilesareIfthepathisaJavaSystemPropertyitisreplacedbyitsvalueintherunningVM.Thefollowingpropertiesareuser.home-User'shomedirectoryuser.dir-User'scurrentworkingdirectoryjava.io.tmpdir-Defaulttempfilepath--<diskStore<!--DefaultCacheThesewillappliedtocachesprogrammaticallycreatedthroughtheCacheManager.ThefollowingattributesarerequiredforSets umnumberofobjectsthatwillbecreatedinSetswhetherelementsareeternal.Ifeternal,timeoutsareignoredandtheelementisneverexpired.timeToIdleSeconds-Setsthetimetoidleforanelementbeforeitexpires.Isonlyusediftheelementisnoteternal.Idletimeisnow-lastaccessedtimetimeToLiveSeconds-Setsthetimetoliveforanelementbeforeitexpires.Isonlyusediftheelementisnoteternal.TTLisnow-creationtimeSetswhetherelementscanoverflowtodiskwhenthein-memorycachehasreachedthemaxInMemorylimit.<!--Predefinedcaches.AddyourcacheconfigurationsettingsIfyoudonothaveaconfigurationforyourcacheaWARNINGwillbeissuedwhentheCacheManagerThefollowingattributesarerequiredforSetsthenameofthecache.Thisisusedtoidentifythecache.ItmustbeSets umnumberofobjectsthatwillbecreatedinSetswhetherelementsareeternal.Ifeternal,timeoutsareignoredandtheelementisneverexpired.timeToIdleSeconds-Setsthetimetoidleforanelementbeforeitexpires.Isonlyusediftheelementisnoteternal.Idletimeisnow-lastaccessedtimeSetsthetimetoliveforanelementbeforeitexpires.Isonlyusediftheelementisnoteternal.TTLisnow-creationtimeSetswhetherelementscanoverflowtodiskwhenthein-memorycachehasreachedthemaxInMemorylimit.<!--SamplecachenamedThiscachecontainsa uminmemoryof10000elements,andwillexpireanelementifitisidleformorethan5minutesandlivesformorethan10Iftherearemorethan10000elementsitwilloverflowtodiskdiskcache,whichinthisconfigurationwillgotowhereverjava.io.tmpisdefinedonyoursystem.OnastandardLinuxsystemthiswillbe/tmp"<cachename="sampleCache1"<!--SamplecachenamedThiscachecontains1000elements.Elementswillalwaysbeheldinmemory.Theyarenotexpired.--><cachename="sampleCache2"/>--<!--Placeconfigurationforyourcachesfollowing--為個(gè),一般都為false表示對(duì)象在緩存中存活的時(shí)間,一個(gè)對(duì)象迚入到本緩存中120秒后,就會(huì)自勱被清除(一般設(shè)置的時(shí)間會(huì)比timeToIdleSeconds時(shí)間長(zhǎng)),設(shè)置此屬性是為了讓活躍的對(duì)象迚入到表示如果活躍對(duì)象已經(jīng)超maxElementInMemory置的最大值時(shí),超出的對(duì)象要被寫入region屬 就 Jar首先,通過region屬性指定要使用的二級(jí)緩存;過Hibernate查詢緩存4】查詢緩存fromE,emp.salaryfromE,emp.salaryfromEmp請(qǐng)publicvoidtst8()Sessionsessionpublicvoidtst8()Sessionsession=Queryquery=",count(emp.id)from"+"E");List<Objectlist4query.list();for(Object[]obj:}}息Hibernate中的鎖機(jī)制5】鎖機(jī)制_樂觀鎖dropdroptableifexistst_bar;createtablet_bar(t_valueinteger,primarykeyinsertintot_bar(t_value)valuepackagepackagepublicclassprivateIntegerprivateIntegerpublicBar()}publicBar(Integervalue)this.value=}publicIntegergetId(){returnthis.id;}publicvoidsetId(Integerid){this.id=id;}publicIntegergetValue(){returnthis.value;}publicvoidsetValue(Integervalue){this.value=} DTD <classname="Bar"<idname="id"type="integer"<generator 主線程準(zhǔn)備買2,最終系統(tǒng)顯示給man的應(yīng)該是98張而other線程在主線程期間買了1packagepackageimportimportimportimportpublicclassTestLockpublicstaticvoidmain(String[]args)Threadt=newOtherThread();Transactiontx=session.beginTransaction();Barbar=(Bar)session.get(Bar.class,1);intvalue=bar.getValue();//try
}catch(InterruptedExceptione)}value-=2;//98bar.setValue(value);//98}}classOtherThreadextends{publicvoidrun()try}catch(InterruptedExceptione)}Transactiontx=session.beginTransaction();}}}Barbar=(Bar)session.get(Bar.class,intvalue=bar.ge
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 中國(guó)總代理授權(quán)合同范例
- 農(nóng)村水車出售合同范例
- 興義農(nóng)村建房合同范例
- 個(gè)人安裝費(fèi)合同范例
- 加工模具承包合同范例
- 民辦合肥經(jīng)濟(jì)技術(shù)職業(yè)學(xué)院《機(jī)床電氣及PC控制》2023-2024學(xué)年第二學(xué)期期末試卷
- 重慶電子工程職業(yè)學(xué)院《商業(yè)攝影基礎(chǔ)》2023-2024學(xué)年第一學(xué)期期末試卷
- 國(guó)際貨物買賣合同合集大全
- 景德鎮(zhèn)陶瓷職業(yè)技術(shù)學(xué)院《項(xiàng)目投融資及可行性研究》2023-2024學(xué)年第二學(xué)期期末試卷
- 興安職業(yè)技術(shù)學(xué)院《全媒體節(jié)目制作與包裝》2023-2024學(xué)年第二學(xué)期期末試卷
- 電網(wǎng)防高墜安全教育
- 中醫(yī)養(yǎng)生-春季養(yǎng)生
- 幼兒園防欺凌家長(zhǎng)會(huì)內(nèi)容
- 興隆街辦拆遷規(guī)劃方案
- 2《秦腔》公開課一等獎(jiǎng)創(chuàng)新教學(xué)設(shè)計(jì) 統(tǒng)編版高中語文選擇性必修下冊(cè)
- 特種作業(yè)人員管理規(guī)定
- 安全管理之雙重預(yù)防機(jī)制
- 建筑工程趕工補(bǔ)償費(fèi)用計(jì)算表
- 2024屆陜西省西安市西北工業(yè)大學(xué)高考語文一模試卷含解析
- 2024年興湘集團(tuán)全資子公司招聘筆試參考題庫含答案解析
- 第十七課 《虛擬與現(xiàn)實(shí)》(課件)2023-2024學(xué)年北師大版(2013)初中心理健康七年級(jí)上冊(cè)
評(píng)論
0/150
提交評(píng)論