面試1案例一使用Hibernate用戶分配角色_第1頁
面試1案例一使用Hibernate用戶分配角色_第2頁
面試1案例一使用Hibernate用戶分配角色_第3頁
面試1案例一使用Hibernate用戶分配角色_第4頁
面試1案例一使用Hibernate用戶分配角色_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1案例一:使用Hibernate用戶分配角案例二:進(jìn)行客戶綜合條件查案例1.1上次課內(nèi)容回顧瞬時(shí)態(tài):沒有唯一標(biāo)識(shí)OID,沒有被持久態(tài):有唯一標(biāo)識(shí)OID,被托管態(tài):有唯一1案例一:使用Hibernate用戶分配角案例二:進(jìn)行客戶綜合條件查案例1.1上次課內(nèi)容回顧瞬時(shí)態(tài):沒有唯一標(biāo)識(shí)OID,沒有被持久態(tài):有唯一標(biāo)識(shí)OID,被托管態(tài):有唯一標(biāo)識(shí)OID,沒有被管持久態(tài)托管態(tài)new對new對象,自帶的.一級(jí)緩存生命周期與設(shè)置與線程綁定的:.創(chuàng):Hibernate中的多對多關(guān)聯(lián)Hibernate的查詢的方式Hibernate對查詢進(jìn)行優(yōu)化1.2Hibernate1.2.1需求1.2.2相關(guān)知識(shí)點(diǎn)1.2.2.1Hibernate1.2Hibernate1.2.1需求1.2.2相關(guān)知識(shí)點(diǎn)1.2.2.1Hibernate:】 <idname="uid"public ser onguid;privateStringusername;privateString一個(gè)用戶有多個(gè)角色privateSet<Role>roles=new}publicclassRole{ ongrid;privateStringrname;一個(gè)角色中有多個(gè)所屬的用戶privateSet<User>users=new} 存cascade=”save-級(jí)聯(lián)刪除放棄外 權(quán)<!--加 <!--加 <generator<propertyname="username"<propertyname="password"配置關(guān)聯(lián)關(guān)系<!--name:角色集合的屬性名 <setname="roles"當(dāng)前類中在中間表的外鍵的名稱<keyclass:關(guān)聯(lián)的另一方的多的類全路徑.column:另一方在中間表中的外鍵名稱 <id<generator<propertyname="rname"<!--配置角色的關(guān)聯(lián) <setname="users"<key *保存操作*保存操作publicvoid保存兩個(gè)用戶Useruser1=newUser(); Useruser2=newUser(); 保存三個(gè)角Rolerole1newRole();Rolerole2newRole();Rolerole3newRole();//設(shè)置關(guān)系:}***}***保存的主體publicvoid保存兩個(gè)用戶Useruser1=newUseruser2=new保存三個(gè)角Rolerole1newRole();Rolerole2newRole();Rolerole3newRole();}publicvoid}publicvoidUseruser=}*移除一個(gè)用戶的所有角色publicvoidUseruser=}*1publicvoid案例二:Hibernate的查詢1.2.3案例二:Hibernate的查詢1.2.3相關(guān)的知識(shí)1.2.3.1Hibernate的檢索方式Hibernate51.OID檢 Customerc= Useruser= .get(User.class,Rolerole1= .get(Role.class,}*1號(hào)用戶退選1號(hào)角色選3publicvoidUseruser= .get(User.class,Rolerole1=ses .get(Role.class,1l);Rolerole3= .get(Role.class,}1.2.3.2HQL檢1.2.3.2HQL檢*HQL:基本檢publicvoid基本查/*Queryquery=ses .createQuery("fromCustomer");List<Customer>list=query.list();*///Queryquery= .createQuery("fromCustomerQueryquery=ses .createQuery("selectcfromCustomerc");List<Customer>list=query.list();for(Customercustomer:list){ }}Set<LinkMan>linkMans=LinkManlinkMan= HQLQBCSQL檢*HQL:排序檢publicvoid基本查Queryquery=*HQL:排序檢publicvoid基本查Queryquery= .createQuery("fromList<Customer>list=orderbycust_idfor(Customercustomer:list){ }}*HQL:條件查publicvoid/*Queryquery=//List<Customer>list.createQuery("fromCustomerwherecust_name=""query.setParameter("name",/*List<Customer>list=query.list();for(Customercustomer:list){ Customercustomer=(Customer)}*分頁查publicvoidQueryquery=}*分頁查publicvoidQueryquery=List<LinkMan>list=query.list();for(LinkManlinkMan:list)}}*聚合函publicvoidQueryquery=Longnum=count(*)from}*投影查publicvoid1.2.3.3多表查詢SQL中分成1.2.3.3多表查詢SQL中分成select*fromselect*fromA,BwhereA.id=select*fromAinnerjoinBonA.id=select*fromAjoinBonA.id=select*fromAleftouterjoinBonA.id=select*fromAleftjoinBonA.id=select*fromArightouterjoinBonA.id=select*fromArightjoinBonA.id=投影查詢一 for(Stringstring:list){ 投影查詢多列/*List<Object[]>list=ses .createQuery("selectcust_id,cust_namefromfor(Object[]objects:list) 投影的構(gòu)造的方式查詢 Customer(cust_id,cust_name)fromCustomer").list();for(Customercustomer:list){ }}1.2.3.4HQL連接查1.2.3.4HQL連接查SQL連接查詢SELECT*FROMcst_customerc,cst_linkmanlWHEREc.cust_id=HQL連接的查詢 // .createQuery("fromLinkManlinnerjoin*publicvoid迫切內(nèi)連接//迫切內(nèi)連接和內(nèi)連接所發(fā)送的SQL語句是一樣的.fetch告訴Hibernate封裝的時(shí)候要往對象中去封裝.將客戶封裝到Customer中將Customer對應(yīng)的聯(lián)系人封裝到Customer的L an的集合中.//List<Customer>list=ses .createQuery("fromCustomercinnerjoinfetchc.linkMans").list();List<Customer>list=ses .createQuery("selectdistinctcfromCustomercinnerjoinfetchc.linkMans").list();// HQL中分成外連接1.2.3.5QBC檢索:Query1.2.3.5QBC檢索:QueryBypublicvoid簡單查詢Criteriacriteria=ses List<Customer>list=criteria.list();for(Customercustomer:list){ }}條件查publicvoid條件查詢Criteriacriteria= 設(shè)置條件//criteria.add(Restrictions.eq("cust_name"," criteria.add(Restrictions.like("cust_name","%小%"));criteria.add(Restrictions.gt("cust_id",1l));for(Customercustomer:list){ }} 封裝到一個(gè)Object[]數(shù)組中,最后得到一個(gè),join后添加一個(gè)fetch關(guān)鍵字,Hibernate會(huì)將每條數(shù)封裝到對象中,最后List<Customer需要去掉重復(fù)值List<Customer>list=for(Customercustomer:list){ }}*分頁查List<Customer>list=for(Customercustomer:list){ }}*分頁查publicvoid條件查詢Criteriacriteria=設(shè)置分頁查詢List<LinkMan>list=for(LinkManlinkMan:list){ }}*排序查publicvoid排序查詢Criteriacriteria=List<LinkMan>list=1.2.4Hibernate的抓取策略)性1.2.4.1區(qū)分立即加載和延遲加載1.2.4Hibernate的抓取策略)性1.2.4.1區(qū)分立即加載和延遲加載延遲加載:當(dāng)語句執(zhí)行到該位置的時(shí)候,不會(huì)馬上發(fā)送SQL1.2.4.2延遲加載在Hibernate中分成兩類for(LinkManlinkMan:list){ }}*可以脫離 publicvoid detachedCriteria.getExecutableCriteria(ses).list離線條件查詢對象與ses綁for(Customercustomer:list){ }}*批量抓取publicvoid*批量抓取publicvoid<set>集合上的fetch和fetch:控制發(fā)送的SQL語句的格式 :默認(rèn)值.發(fā)送基本的select語句去查詢 subselect:發(fā)送一條子查詢語句true:默認(rèn)值.false:不使用延遲加載extra:及其懶惰通常情況下在<set>上一般默認(rèn)的配置了.fetch=”selectlazy=”true”;特殊的需求:查詢客戶同時(shí)查詢客戶的聯(lián)系人在一個(gè)頁面中進(jìn)行顯示.fetch=”join”<many-to-one>fetch和 :控制SQLselect默認(rèn)值,普通的select語句進(jìn)行查join: :默認(rèn)值,proxy的取值<class>上的lazy的取值no-proxy:不用通常請情況下在<many-to-one>上一般都是默認(rèn)配置fetch=”select”lazy=”proxy特殊的需求:查詢客戶同時(shí)查詢客戶的聯(lián)系人在一個(gè)頁面中進(jìn)行顯示.fetch=”join” 的 的Customercustomer= *控制類級(jí)別的延遲有幾種辦法1.持久化類使用final了---類級(jí)別延遲就失效. 上配置一個(gè)屬性lazy3.使用Hibernate類中initialize

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(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ǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論