成果達(dá)內(nèi)java視頻-hibernate exday_第1頁(yè)
成果達(dá)內(nèi)java視頻-hibernate exday_第2頁(yè)
成果達(dá)內(nèi)java視頻-hibernate exday_第3頁(yè)
成果達(dá)內(nèi)java視頻-hibernate exday_第4頁(yè)
成果達(dá)內(nèi)java視頻-hibernate exday_第5頁(yè)
已閱讀5頁(yè),還剩45頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

12Hibernate345注 練習(xí)1:Ant開(kāi)發(fā)工具演示 錯(cuò)誤!未定義書(shū)簽練習(xí)2:Hibernate 練習(xí)3:二級(jí)緩存 練習(xí)4:查詢(xún)緩存 練習(xí)5:鎖機(jī)制_樂(lè)觀(guān)鎖 1:Ant02<taskdef<taskdefname=""classname=""classpathref=""<taskdefname="hibernatetool"<taskdefname="hibernatetool"<projectname="tts_hibernate"default=""<propertyname="dir.lib"value="WebRoot/WEB-INF/lib"<propertyname="dir.src"value="src"<path<fileset <target<!--ANTTask1--<copy<fileset<fileset<target<!--ANTTask2--<deletedir="note"<mkdirdir="note"<taskdef<taskdefclasspathref=classpathref="cp"format=format="true"<target<hibernatetool<classpathrefid="cp" Hibernate<hbm2ddl 導(dǎo)入配置文件HibernatejarAntsql查看生成的sql文droptableifexistst_dept;droptableifexistscreatetablet_deptt_idintegernotnullauto_increment,t_namevarchar(255),primarykeycreatetablet_empt_idintegernotnullauto_increment,t_namevarchar(255),t_hire_datedate,t_last_logindatetime,t_registerchar(1),t_dept_idinteger,primarykey(t_id)altertableaddconstraintFK68F5E7DD38E4BB5foreignkey(t_dept_id)referencest_dept根據(jù)Hibernate.cfg.xml中指定的方言的丌同,可以自勱生成oraclesql戒者mysqlsql<projectname="tts_hibernate"default=""<propertyname="dir.lib"value="WebRoot/WEB-INF/lib"<propertyname="dir.src"value="src"<path<fileset <taskdefname=<taskdefname="hibernatetool"classpathref="cp"/><target<hibernatetool<classpathrefid="cp"<hbm2javajdk5="true"測(cè)試運(yùn)行AntDept.java(案例結(jié)束2:HibernateQuerypackagepackagepublicclassTestQuerypublicvoidtst1()Sessionsession=HibernateUtils.getSession();Queryquery=session.createQuery("fromFoo");intpage=introwsPerPage=query.setFirstResult((1)*}}}List<Foo>fooList=for(Foof}如果將方言改為Oralceoracledb2**publicvoidtst2()Sessionsession=HibernateUtils.getSession();Queryquery=session.createQuery("fromEmpempwhereList<Emp>fooList=for(Empe:}}**publicvoidtst3()Sessionsession=Queryquery=List<Emp>fooList=query.list();for(Empee.getId()+""+e.getName()+""+}<!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"lazy="false"fetch="join"<query<![CDATA[fromEmpempwhere測(cè)試(略*勱態(tài)條件查詢(xún)*勱態(tài)條件查詢(xún)publicvoidtst4()Sessionsession= Queryquery= "fromEmpempwhere Criteriacri= Criteriacri= }}Restrictions.ge("salary",4000.0),List<Emp>empList=for(EmpeempList){e.getId()+""+e.getName()+""+}123from返回selectemp.idfromEmp返回selectemp.id,fromEmp返回selectnewEmp(emp.id,)fromEmppublicvoidpublicvoidtst5()Sessionsession=}}ListempList=session.createQuery("fromfromselectemp.idfromEmpselectemp.id,fromEselectnewEmp(emp.id,)fromEmpList<EmpEmpidnamepublicpublicvoidtst6()Sessionsession=HibernateUtils.getSession();Transactiontx=session.beginTransaction();Queryquery="deleteElike}publicpublicvoidtst7()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是Queryquery=session.createSQLQuery("select*from List<Object[]>list= for(Object[]obj:list) System.out.println(obj[0]+","+obj[1]+","+ List<Dept>list=for(Deptdept:list)System.out.println(dept.getId()+","+dept.getName()++}}因?yàn)橛袝r(shí)候如果碰到個(gè)巨復(fù)雜無(wú)比的SQL,直接寫(xiě)還是方便些。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"+"E");List<Object[]>list4=for(Object[]obj:list4)System.out.println(obj[0]+","+}}joinjoinfetchpublicvoidtst9()Sessionsession=HibernateUtils.getSession();Queryquery=session.createQuery("fromStudentstujoinfetchstu.coursesc"+"=?");List<Student>stuList=for(Studentstu:{stu.getName()+","+}}(案例結(jié)束練習(xí)3:二級(jí)緩存ehcache.jar的Jar請(qǐng)ehcache-1.2.3.jar.zip<!--Setsthepathtothedirectorywherecache.datafilesareIfthepathisaJavaSystemPropertyitisreplacedbyitsvalueintherunningVM.Thefollowingpropertiesareuser.home-User'shomedirectoryuser.dir-User'scurrentworkingdirectoryjava.io.tmpdir-Defaulttempfilepath--<diskStore<!--DefaultCacheThesewillappliedtocachesprogrammaticallycreatedthroughtheCacheManager.Thefollowingattributesarerequiredfor-Setsthe umnumberofobjectsthatwillbecreatedinmemorySetswhetherelementsareeternal.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,andwillexpireanelementifitisidleformorethan5minutesandlivesformorethan10Iftherearemorethan10000elementsitwilloverflowtodiskcache,whichinthisconfigurationwillgotowhereverjava.io.tmpisdefinedonyoursystem.OnastandardLinuxsystemthiswillbe/tmp"<cachename="sampleCache1"<!--SamplecachenamedThiscachecontains1000elements.Elementswillalwaysbeheldinmemory.Theyarenotexpired.--><cachename="sampleCache2"/>--<!--Placeconfigurationforyourcachesfollowing--一般都為false表示對(duì)象在緩存中存活的時(shí)間,一個(gè)對(duì)象迚入到本緩存中120秒后,就會(huì)自勱被清除(一般設(shè)置的時(shí)間會(huì)比timeToIdleSeconds時(shí)間長(zhǎng)),設(shè)置此屬性是為了讓活躍的對(duì)象迚入到maxElementInMemoryregion屬 表示指定使用哪個(gè)二級(jí)緩usage屬 表示二級(jí)緩存的使用方 Jar首先,通過(guò)region屬性指定要使用的二級(jí)緩存;(案例結(jié)束練習(xí)4:查詢(xún)緩存fromE,emp.salaryfromEmp請(qǐng)publicpublicvoidtst8()Sessionsession=Queryquery=",count(emp.id)from"+"E");for(Object[]obj:list4)}}}(案例結(jié)束5:鎖機(jī)制_樂(lè)觀(guān)鎖dropdroptableifexistst_bar;createtablet_bar(t_valueinteger,primarykeyinsertintot_bar(t_value)valuepackagepackagepublicclassprivateIntegerprivateIntegerpublicBar()}publicBar(Integervalue)this.value=}}}publicIntegergetId(){returnpublicvoidsetId(Integerid){this.id=publicIntegergetValue(){returnpublicvoidsetValue(Integervalue){this.value=<!DOCTYPEhibernate- DTD" <classname="Bar"<idname="id"type="integer"<generator<propertyname="value"type="integer"column="t_value" packageimportorg.hibernate.Session;importcom.tarena.tts.po.Bar;importpublicclassTestLockpublicstaticvoidpackageimportorg.hibernate.Session;importcom.tarena.tts.po.Bar;importpublicclassTestLockpublicstaticvoidmain(String[]args)Threadt=newOtherThread();Sessionsession=HibernateUtils.getSession();Transactiontx=session.beginTransaction();Barbar=(Bar)session.get(Bar.class,1);intvalue=bar.getValue();//try}catch(InterruptedExceptione)}value-=2;//98bar.setValue(value);//98}}classclassOtherThreadextends{publicvoidrun()try}catch(InterruptedExceptione)}Sessionsession=HibernateUtils.getSession();Transactiontx=session.beginTransaction();Barbar=(Bar)session.get(Bar.class,1);intvalue=bar.getValue();////value-=1;//99bar.setValue(val

溫馨提示

  • 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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論