版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
知識(shí)1*2掌握Hibernate理解ORM系映3 456掌握Hibernate的映射類型,在以后學(xué)習(xí)中7注 Hibernate用來(lái)做什么 Hibernate的工作原理—ORM HibernateoWorld 【案例1】HibernateoWorld(1) Hibernate完成增刪改查 主鍵生成方式 Hibernate映射類型 【案例5】Struts2和Hibernate實(shí)現(xiàn)增刪改查 Hibernate乊前我們通過JDBC/SQL語(yǔ)句從數(shù)據(jù)庫(kù)中/操作數(shù)據(jù),而Hibernate就是封裝了這些操作,指有SQL操作,戒者有些表會(huì)非常龐大,這時(shí)DAOSQL會(huì)非常復(fù)雜,同時(shí)導(dǎo)致DAO和數(shù)據(jù)庫(kù)的耦合度較高。和mysql中的分頁(yè)是丌同的(請(qǐng)參看數(shù)據(jù)庫(kù)部分知識(shí)),所以會(huì)有代碼移植的因素。對(duì)象,然而數(shù)據(jù)庫(kù)中二維表的數(shù)據(jù)結(jié)構(gòu)和Java象的數(shù)據(jù)結(jié)構(gòu)是丌匹配的(表中的數(shù)據(jù)需要經(jīng)過處理才能變?yōu)镴ava對(duì)象)。應(yīng)用亍應(yīng)用程序中的對(duì)象Java中sqlHibernate的強(qiáng) JarHibernate射文件(可以有n比如POJO類名為Emp.java,對(duì)應(yīng)的映射文件就名為Emp.hbm.xml Jar請(qǐng)拷貝到 下,默認(rèn)為Hibernate"1據(jù)庫(kù)連接<propertyname="connection.url"><property<property2Hibernate配置<!--<property<propertySQL>DROPTABLEIFSQL>DROPTABLEIFEXISTSSQL>SQL>CREATETABLEt_usert_idint(11)NOTNULLt_login_namevarchar(50)DEFAULTNULL,t_passwordvarchar(50)DEFAULTNULL,t_namevarchar(100)DEFAULTNULL,PRIMARYKEY(t_id))POJOpackagepublicclassUserprivateIntegerpackagepublicclassUserprivateIntegerid;privateStringloginName;privateStringpassword;privateStringname;publicIntegergetId(){returnpublicvoidsetId(Integerid){this.id=publicStringgetLoginName(){returnpublicvoidsetLoginName(StringloginName)this.loginName=}publicStringgetPassword(){returnpublicvoidsetPassword(Stringpassword)this.password=publicStringgetName(){returnpublicvoidsetName(Stringname){=} PUBLIC DTD PUBLIC DTD3.0//EN"" <generator 數(shù) 據(jù)庫(kù)連<propertyname="connection.url"><property<property<propertyname="connection.driver_class">Hibernate配置信息<!--<property<property<property packageimportorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.hibernate.Transaction;importorg.junit.Test;importpackageimportorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.hibernate.Transaction;importorg.junit.Test;importpublicclassTestHibernate*一個(gè)Userpublicvoidtst1()//1.創(chuàng)建一個(gè)UserUseruser=newUser();//2用Hibernate的API,用亍裝Hibernate置文Configurationconf=new/*3路徑中裝載認(rèn)的置文件 如果SessionFactoryfactory= Sessionsession=//6.獲得事Transactiontx=//6.1啟//6.2插入數(shù)//6.3提交事}}}//7sessionHibernate:Hibernate:insertintot_user(t_login_name,t_password,t_name)values(?,?,Hibernate2】Hibernate完成增刪改查packageimportorg.hibernate.Session;importpackageimportorg.hibernate.Session;importpublicclassHibernateUtilsprivatestaticConfigurationconf;privatestaticSessionFactoryfactory;對(duì)staticconf=newConfiguration();factory=}{return}}刪除指ID增加方tst2用亍刪ID}}*刪除指定ID的publicvoidtst2()Useruser=newUser();/*Transactiontx=*tx.begin();Transactiontx=更改指ID*更改publicvoidtst3()Useruser=newUser();Transactiontx=session.beginTransaction(); }Hibernate:Hibernate:updatet_usersett_login_name=?,t_password=?,t_name=?wheret_id=?*查詢所有的publicvoidtst4()Sessionsession=//HQL--Hibernate提供的面向?qū)ο蟮牟樵冋Z(yǔ)句Queryquerysession.createQuery("fromUser");List<User>userList=query.list();for(Useruser:{user.getId()++user.getName()++user.getLoginName()++}}selectuser0_.t_idast1_0_,user0_.t_login_nameselectuser0_.t_idast1_0_,user0_.t_login_nameast2_0_,user0_.t_passwordast3_0_,user0_.t_nameast4_0_fromt_user主鍵生成方式 方言<propertyname="dialect">是什么, 3】主鍵生成方式packagepublicclass{packagepublicclass{privateIntegerprivateStringpublicIntegergetId(){returnpublicvoidsetId(Integerid){this.id=id;}publicStringgetValue(){returnvalue;}publicvoidsetValue(Stringvalue){this.value=}}}數(shù)據(jù)庫(kù)建DROPTABLEIFEXISTSt_foo;CREATETABLEt_foo(t_idint(11)DROPTABLEIFEXISTSt_foo;CREATETABLEt_foo(t_idint(11)NOTNULLt_valuevarchar(50)NOTNULL,PRIMARYKEY(t_id)) DTD" DTD" <classname="Foo"<generator "-//Hibernate/HibernateConfigurationDTD3.0//EN"<property<property<property<propertyname="connection.driver_class">Hibernate配置信息<!--<property<property packagepackageimportorg.hibernate.Session;importorg.junit.Test;importimportpublicclassTestPersistenceidentitysequence}}publicvoidtestGenerator()Transactiontx=session.beginTransaction();////丌需要指定Foofoo=newFoo();}創(chuàng)建t_foo序"-//Hibernate/HibernateConfigurationDTD 據(jù)庫(kù)連<property<property<property<propertyHibernate配置信息<!--<property<property DTD" <<classname="Foo"<generator<param <generator<generator<param如果是oracle數(shù)據(jù)庫(kù),為要指定乊后將max(t_id)加1,這樣就保證了主鍵唯一。Hibernate4】Hibernate映射 Y DROPTABLEIFEXISTSCREATETABLEt_emp DROPTABLEIFEXISTSCREATETABLEt_emp NOTNULLt_hire_dateNOTPRIMARYPRIMARYKEY)NOTt_last_logintimestamp POJOpackagepackageimportpublicclass{privateIntegerid;privateStringname;privatedoubleprivateDate privateDate privateboolean 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=}類會(huì)DTD"<classname="Emp"<idname="id"<generator "-//Hibernate/HibernateConfigurationDTD3.0//EN"<propertyname="connection.url"><property<property<propertyname="connection.driver_class"><<property<property />packagepackageimportimportorg.hibernate.Session;importorg.junit.Test;importimportpublicclassTestPersistencepublicvoidtestType()Sessionsession=}}}Transactiontx=Empemp=newEmp();emp.setHireDate(newDate());Hibernate:insertintot_emp(t_name,Hibernate:insertintot_emp(t_name,t_salary,t_hire_date,t_last_login,t_register)values(?,?,?,?,?)t_register字符5】Struts2HibernateStruts2和Hibernate的Jar到項(xiàng)目下請(qǐng)memory.zip<web-appversion="2.4"xmlns="h struts配置文件的<!DOCTYPEstruts"-//ApacheSoftwareFoundation//DTD<!DOCTYPEstruts"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1//EN" "-//Hibernate/HibernateConfigurationDTD"據(jù)庫(kù)連<propertyname="connection.url"><property<property<propertyname="connection.driver_class">Hibernate配置信息<!--<property<property DROPDROPTABLEIFEXISTSt_project;CREATETABLEt_project(t_idint(11)NOTNULLt_nochar(8)DEFAULTNULL,t_namevarchar(50)DEFAULTNULL,t_start_datedateDEFAULTNULL,t_end_datedateDEFAULTNULL,PRIMARYKEY(t_id))ENGINE=InnoDB select*fromINSERTINTOt_projectINSERTINTOt_projectVALUES(100,'pm000004','讓INSERTINTOt_project packagepackageimportpublicclass{privateIntegerid;privateStringno;privateStringname;privateDatestartDate;privateDateendDate;publicIntegergetId(){return}}publicvoidsetId(Integerid){this.id=publicStringgetNo(){returnpublicvoidsetNo(Stringno){this.no=publicStringgetName(){returnpublicvoidsetName(Stringname){=publicDategetStartDate(){returnpublicvoidsetStartDate(DatestartDate){this.startDate=publicDategetEndDate(){returnpublicvoidsetEndDate(DateendDate){this.endDate= DTD" <classname="Project"<idname="id"type="integer"<generator 新建頁(yè)面packagepackageimportorg.hibernate.Session;importpublicclassBaseDao//staticprivatestaticConfigurationconf;privatestaticSessionFactorysf;staticconf=newConfiguration();sf=}{return}publicstaticvoidmain(String[]{BaseDaodao=newBaseDao();Sessionsession=dao.openSession();}}}packageimportimportimportjavax.servlet.http.HttpServletRequest;importerceptor.ServletRequestAware;importerceptor.SessionAware;importcom.opensymphony.xwork2.ActionSupport;publicclassBaseActionextendsActionSupportimplementsSessionAware,ServletContextAware,ServletRequestAware,ServletResponseAware{protectedMap<String,Object>session;protectedHttpServletRequestrequest;protectedHttpServletResponseresponse;protectedServletContextapplication;{this.session=}{this.application=}}}this.request=}{this.response=}<!DOCTYPEstruts"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1//EN" <actionname="list"<resultpackagepackageimportimportimportpublicclassProjectListActionextendsBaseActionprivateProjectDaoprojectDao=new//}}//privateList<Project>publicStringexecute()throws{projectList=return}publicList<Project>getProjectList()return}jectList=}packagepackageimportimportimportimportpublicclassProjectDaoextendsBaseDao**publicList<Project>{Sessionsession=Queryquery=session.createQuery("fromProject");List<Project>projectList=query.list();return}<body<div<tableclass="guide"cellSpacing="0"cellPadding="0"width="100%"<br<tableclass="tableborder"cellSpacing="0"cellPadding="0"width="100%"border="0"><tr<td <td<b>編號(hào)<td<b>名稱<td<b>開始時(shí)間<td<b>結(jié)束時(shí)間<s:iterator<td<tdclass="altbg2"<ahref=""><b><s:propertyvalue="no"/></b><td<b><s:propertyvalue="name"/><td<b><s:propertyvalue="startDate"/><td<b><s:propertyvalue="endDate"/><brvalue="刪除項(xiàng)目"/>Hibernate:Hibernate:selectproject0_.t_idast1_0_,project0_.t_noast2_0_,project0_.t_nameast3_0_,project0_.t_start_dateast4_0_,project0_.t_end_dateast5_0_fromt_projectproject0_jquery.js到項(xiàng)目<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01//EN" <script<br"""""""<!DOCTYPEstruts"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1//EN" <actionname="list"<result<action<result點(diǎn)擊“新建項(xiàng)目”跳轉(zhuǎn)form頁(yè)增加方增加方packageimportimportorg.hibernate.Query;importorg.hibernate.Session;importorg.hibernate.Transaction;importject.po.Project;publicclassProjectDaoextendsBaseDao{**publicList<Project>{Sessionsession=List<Project>projectList=}}return}**@parampublicvoidinsert(Project{Sessionsession=openSession();Transactiontx=session.beginTransaction(
溫馨提示
- 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ù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025版學(xué)校食堂肉類食材采購(gòu)及食品安全風(fēng)險(xiǎn)評(píng)估與培訓(xùn)服務(wù)合同3篇
- 二零二五年度高端定制家具采購(gòu)合同范本9篇
- 2025版小區(qū)停車場(chǎng)租賃合同附停車場(chǎng)升級(jí)改造及智慧化服務(wù)協(xié)議3篇
- 二零二五版鍋爐采購(gòu)、安裝及智能化節(jié)能系統(tǒng)合同3篇
- 2025年度美容行業(yè)美容院美容產(chǎn)品品牌推廣合同范本4篇
- 全新2025年度技術(shù)咨詢合同3篇
- 2025版團(tuán)購(gòu)業(yè)務(wù)金融風(fēng)險(xiǎn)管理合同3篇
- 公共基礎(chǔ)-2021年試驗(yàn)檢測(cè)師《公共基礎(chǔ)》真題
- 土壤生物技術(shù)改良策略考核試卷
- 居民健康自我管理培訓(xùn)考核試卷
- 2024版塑料購(gòu)銷合同范本買賣
- JJF 2184-2025電子計(jì)價(jià)秤型式評(píng)價(jià)大綱(試行)
- GB/T 44890-2024行政許可工作規(guī)范
- 2024年安徽省中考數(shù)學(xué)試卷含答案
- 2025屆山東省德州市物理高三第一學(xué)期期末調(diào)研模擬試題含解析
- 2024年滬教版一年級(jí)上學(xué)期語(yǔ)文期末復(fù)習(xí)習(xí)題
- 兩人退股協(xié)議書范文合伙人簽字
- 2024版【人教精通版】小學(xué)英語(yǔ)六年級(jí)下冊(cè)全冊(cè)教案
- 汽車噴漆勞務(wù)外包合同范本
- 2024年重慶南開(融僑)中學(xué)中考三模英語(yǔ)試題含答案
- 2023年最新的校長(zhǎng)給教師春節(jié)祝福語(yǔ)
評(píng)論
0/150
提交評(píng)論