成果达内java视频-hibernate exday_第1页
成果达内java视频-hibernate exday_第2页
成果达内java视频-hibernate exday_第3页
成果达内java视频-hibernate exday_第4页
成果达内java视频-hibernate exday_第5页
免费预览已结束,剩余45页可下载查看

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

12Hibernate345注 练习1:Ant开发工具演示 错误!未定义书签练习2:Hibernate 练习3:二级缓存 练习4:查询缓存 练习5:锁机制_乐观锁 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 导入配置文件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根据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"测试运行AntDept.java(案例结束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测试(略*劢态条件查询*劢态条件查询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对象(这个方法了解即可)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()++}}因为有时候如果碰到个巨复杂无比的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"+"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()+","+}}(案例结束练习3:二级缓存ehcache.jar的Jar请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表示对象在缓存中存活的时间,一个对象迚入到本缓存中120秒后,就会自劢被清除(一般设置的时间会比timeToIdleSeconds时间长),设置此属性是为了让活跃的对象迚入到maxElementInMemoryregion属 表示指定使用哪个二级缓usage属 表示二级缓存的使用方 Jar首先,通过region属性指定要使用的二级缓存;(案例结束练习4:查询缓存fromE,emp.salaryfromEmp请publicpublicvoidtst8()Sessionsession=Queryquery=",count(emp.id)from"+"E");for(Object[]obj:list4)}}}(案例结束5:锁机制_乐观锁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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论