




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第五章Criteria
查詢HQL優(yōu)化從哪幾個方面考慮?Hibernate數(shù)據(jù)加載方式有哪些?有什么區(qū)別?
HQL聯(lián)接查詢有幾種方式?在映射文件中如何定義命名查詢?如何使用本地SQL查詢?如何使用Criteria查詢數(shù)據(jù)如何使用Criteria進行查詢排序如何使用Criteria進行查詢分頁使用Criteria查詢租房信息使用Criteria查詢實現(xiàn)租房信息分頁顯示掌握Criteria查詢使用Criteria進行查詢排序使用Criteria進行查詢分頁用到特定于數(shù)據(jù)庫的SQL語句,程序本身會依賴于特定的數(shù)據(jù)庫不了解SQL
語句,恐怕對使用HQL帶來困難Hibernate提供的Criteria查詢幫助我們解決了這種問題Criteria查詢采用面向對象方式封裝查詢條件,又稱為對象查詢對SQL
語句進行封裝采用對象的方式來組合各種查詢條件由Hibernate
自動產(chǎn)生SQL
查詢語句Criteria由Hibernate
Session進行創(chuàng)建SessionFactory
sessionFactory
=
newConfiguration().configure().buildSessionFactory();Session
session
=
sessionFactory.openSession();Criteria
criteria
=
session.createCriteria(User.class);List
result
=
criteria.list();Iterator
it
=
result.iterator();while
(it.hasNext())
{User
user
=
(User)
it.next();System.out.println("用戶名:"+user.getName());}session.close();sessionFactory.close();創(chuàng)建Criteria對象使用Criteria的list()方法獲得數(shù)據(jù),list()方法返回List
實例criteria.add(Restrictions.eq("name",
"bob"));Iterator
it
=
result.iterator();while
(it.hasNext())
{User
user
=
(User)
it.next();System.out.println("用戶名:"+user.getName());}session.close();sessionFactory.close();創(chuàng)建Criteria對象使用aSdeds(s)添ion加F查actory
sessionFactory
=
List
result
=
criteria.list();查詢條件:name='admin'詢條件new
Configuratio返n(回).c條on件fi實gu例re().buildSessionFactory();Session
session=sessionFactory.openSession();Criteria
criteria
=
session.createCriteria(User.class);演示示例:Criteria查詢方法說
明Restrictions.eq()對應SQL的等于(’=’)Restrictions.allEq()使用Map,使用key/value進行多個相等的值的比對Restrictions.gt()對應SQL的大于(‘>’)Restrictions.ge()對應SQL的大于等于(‘>=’)Restrictions.lt()對應SQL的小于(‘<’)Restrictions.le()對應SQL的小于等于(‘<=’)Restrictions.between()對應SQL的between子句Restrictions.like()對應SQL的like子句Restrictions.in()對應SQL的in子句Restrictions.and()對應SQL的andRestrictions.or()對應SQL的orRestrictions.not()對應SQL的not需求說明:以標題(title)、價格(price)范圍和面積(floorage)范圍為條件對租房信息進行綜合的“模糊
”查詢通過控制臺接收查詢條件,若條件為空則表示查詢全部完成時間:15分鐘SessionFactory
sessionFactory
=new
Configuration().configure().buildSessionFactory();Session
session
=
sessionFactory.openSession();
Criteria
criteria
=
session.createCriteria(House.class);criteria.add(Restrictions.or(Restrictions.eq("price",new
Double(2300)),Restrictions.like("title","%地鐵%")));List
result
=
criteria.list();Iterator
it
=
result.iterator();while
(it.hasNext())
{House
house=(House)it.next();System.out.println("標題:"+house.getTitle());}如果屬性條件很多,使用Restrictions
也不方便Hibseersnsaiotne.c提lo供se(E);xample
的create()方法來建立Exasmespsiloen實Fac例tor,y.cEloxsae(m);ple
實現(xiàn)了Criteria
接口House
house
=
new
House();house.setPrice(new
Double(2000));house.setFloorage(new
Integer(40));SessionFactory
sessionFactory
=
newConfiguration().configure().buildSessionFactory();Session
session
=
sessionFactory.openSession();Criteria
criteria
=
session.createCriteria(House.class);criteria.add(Example.create(house));List
results
=
criteria.list();Iterator
it
=
results.iterator();while(it.hasNext()){House
h=
(House)it.next();System.out.println("標題:"+h.getTitle()+"
價格"+h.getPrice());}essionFa查詢條件,添加到house對象中創(chuàng)建Criteria對象通過Example的create()方法來建立Example實例作為查詢條件Hibernate
在自動生成SQL
語句時將自動過濾掉對session象.c的lo空se屬();性,根據(jù)有非空屬性值的屬性生成查詢條件s
ctory.close();
演示示例:使用Example常見調試問題及解決辦法代碼規(guī)范問題共性問題集中講解如何使用Criteria查詢?Restrictions常用限定查詢方法有哪些?
Criteria查詢如何使用對象作為查詢條件?Criteria查詢不僅能組合出SQL中where子句的功能,還可以組合出排序查詢功能使用org.hibernate.criterion.Order對結果進行排序排序的方法為:asc()desc()SessionFactory
sessionFactory
=
new
Configuration().configure().buildSessionFactory();Session
session
=
sessionFactory.openSession();Criteria
criteria
=
session.createCriteria(House.class);criteria.addOrder(Order.desc("price"));List
result
=
criteria.list();Iterator
it
=
result.iterator();while
(it.hasNext())
{創(chuàng)建Criteria對象加入Order
條件以價格降序的方式排列House
house
=
(House)
it.next();System.out.println("標題:"
+
house.getTitle()
+
"
價格"+
house.getPrice());}session.close();sessionFactory.close();演示示例:Criteria排序練習——使用Criteria實現(xiàn)排序需求說明:已知房屋租金為2000元,面積為75平米要求查詢出與之類似的其他房屋信息并以房屋編號升序的方式進行排序完成時間:15分鐘Criteria的setMaxResults()方法可以限定查詢返回數(shù)據(jù)的行數(shù)Criteria的setFirstResult()設定查詢返回結果的第一行數(shù)據(jù)的位置結合以上Criteria的兩個方法即可實現(xiàn)簡單分頁ssionFase
ctory.close();SessionFactory
sessionFactory=newHCiboenrfnigauter:astieolne(c)t.c*ofnrofimgu(res(e)l.ebcutilrdoSwe_s.s*,iornoFwancutmoryro()w;n創(chuàng)u建m_Crfritoemria對象
(Sselsescitotnhsise_s.sidioans=ids4e_s3s_i,otnhFisa_c.tuosrye.ro_pidenaSseussseior2n_()4;_3_,this_.type_idaCs
rtiyteprei3a_c4r_it3e_r,iath=iss_e.strieoent._cirde
aseCstrrieteerti4a_(H4_o3u_s,et.hcilsa_s.sti)t;le
as
title4_3_,this_.price
as
price4_3_,this_.pubdate
as
pubdate4_3_,thcirsit_e.frlioao.sreatgFeirasstRfelosourlat(g3e);4_3_,this_.contact設a定s
c查o詢nt返ac回t4結_3果_,的ucsreirte2r_i.aid.saestMida0x_R0e_s,uultsse(r22);_.name
as
name0_0第_一,u行se數(shù)r2據(jù)_.的pa位ss置word
aspLaissstwreosrdu0lt_s0=_,curisteri2a_.l.itsetl(e)p;hone
as
telephone0_0_,user2_.usernameaIsteursaetornr
aitm=er0e_s0u_l,tsu.siterr2a_to.irs(a);dmin
as
isadm限in定0_查0_詢,t返yp回e3數(shù)_據(jù).id的asidw3h_i1le_(,ityhpaes3N_e.txytp()e){_name
as
type2_3_1_,street4_.行id數(shù)as
id2_2_,street4_.street_name
as
street2_2_2_,street4_.district_id
asdistricHto3u_2s_e2h_
=fr(oHmoujbsiet.)hito.nuesxet(t)h;is_
inner
join
jbit.users
user2_
onthis_.Suyssetre_mid.=ouust.eprr2i_n.tildn(i"n標ne題r
:joi"n+h.oguestTetitylpe(e)+ty"pe價3_格o:n"+h.getPrice());this_.type_id=type3_.id
inner
join
street
street4_
onth}is_.street_id=street4_.id
)
row_
where
rownum
<=
?)
whereroswesnsuimon_.c>lo?
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 長江師范學院《管理技能與創(chuàng)新實踐》2023-2024學年第二學期期末試卷
- 桂林旅游學院《微機原理與接口技術(3)》2023-2024學年第二學期期末試卷
- 蘇州城市學院《書法(一)》2023-2024學年第二學期期末試卷
- 東華理工大學《汽車發(fā)展史》2023-2024學年第二學期期末試卷
- 2025屆四川省新高考教研聯(lián)盟高三上學期八省適應性聯(lián)考模擬演練考試(二)歷史試卷
- 合肥城市學院《建筑施工安全》2023-2024學年第二學期期末試卷
- 2024-2025學年上海市松江區(qū)高三上學期期末質量監(jiān)控考試歷史試卷
- 長春大學旅游學院《高分子材料改性原理及技術》2023-2024學年第二學期期末試卷
- 林州建筑職業(yè)技術學院《化工制圖與AutoCAD》2023-2024學年第二學期期末試卷
- 華東交通大學《中國現(xiàn)當代文學二》2023-2024學年第二學期期末試卷
- 【真題】2023年南京市中考語文試卷(含答案解析)
- 安徽安慶家鄉(xiāng)介紹
- 自動測試系統(tǒng)第1章第1節(jié)測試系統(tǒng)發(fā)展綜述
- 2024年河南省水務規(guī)劃設計研究有限公司人才招聘筆試參考題庫附帶答案詳解
- 山地光伏設計方案
- 2022廣州美術學院附屬中學(廣美附中)入學招生測試卷語文
- 北師大版(2019)選擇性必修第三冊Unit 7 Careers Topic Talk 導學案
- 春節(jié)復工復產(chǎn)安全教育培訓
- 2024年廣西公務員考試行測真題及答案解析
- 護理質量改進項目
- 《礦產(chǎn)地質勘查規(guī)范 花崗偉晶巖型高純石英原料》(征求意見稿)
評論
0/150
提交評論