Hibernate 筆記 HQL查詢:條件查詢,聚集函式,子查詢,導航查詢
1 條件查詢
1.1 查詢 員工表emp中 年齡eage小於30,月薪esal大於20000的員工姓名ename
sql:select ename from emp where eage<? and esal >?;
hql: select ename from Emp where eage<? and esal >?
1.2 問號的設定與別名
問號(?)的設定使用.setParameter(位置, 屬性值) 在sql語句中問號的位置是從1開始,在hql中從零開始。
在hql中,問號(?)也可以起別用,:後面緊跟別名,setParameter(“age”, 30)方法也隨之改變,將原先的數字改成”別名”,如下所示
String hql=”select ename from Emp where eage<:age and esal >:sal”;
Query query= session.createQuery(hql).setParameter(“age”, 30).setParameter(“sal”, 20000f).list();
1.3 一組問號的設定
對於一組問號也可以設定別名:如 查詢在1,2部門的員工姓名
hql=”select ename from Emp where did in (?,?)”;
hql=”select ename from Emp where did in (:d)”;
Query query= session.createQuery(hql).setParameter(“d”, new Object[]{1,2}).list();
public void Test9() throws Exception{
Configuration config=new Configuration().configure();
SessionFactory sessionFactory= config.buildSessionFactory();
Session session=null;
Transaction tr=null;
try{
session= sessionFactory.openSession();
tr=session.beginTransaction();
String hql="select ename from Emp where eage<? and esal >?";
Query query= session.createQuery(hql).setParameter(0, 30).setParameter(1, 20000f); //設定問號,從0開始。
List<String> list = query.list();
for(String message:list){
System.out.println(message);
}
tr.commit();
}catch(Exception e){
tr.rollback();
}finally{
if(session!=null){
session.close();
}
if(sessionFactory!=null){
sessionFactory.close();
}
}
}
結果:
Hibernate: select emp0_.ename as col_0_0_ from emp emp0_ where emp0_.eage<? and emp0_.esal>?
劉亦菲
白百何
文章
林月如
2 條件查詢的常用關鍵字
2.1 distinct 過濾重複的值
查詢員工表中的所有員工姓名,並去掉重複值
hql=”select distinct ename from Emp “;
2.2 delete 刪除
刪除年齡大於25的員工
hql=”delete Emp where eage>25”; 刪除年齡大於25歲的使用者
Query query=session.createQuery(hql);
query.executeUpdate(); //執行 executeUpdate 方法返回int型別。
tx.commit(); //成功,則提交,對資料庫操作
2.3 update 更新
更新員工編號11的年齡為22
String hql=”update Emp s set s.eage=’22’ where s.eid=11”; //更新語句
Query query=session.createQuery(hql);
query.executeUpdate(); //執行
tx.commit(); //成功,則提交
2.4 between…and…和not between… and…確定查詢範圍
查詢員工表中年齡在20到30之間的員工姓名
hql=”select ename from Emp where eage between 20 and 30”
2.5 in和not in確定查詢集合
查詢員工屬於低1,2部門的員工姓名
hql=”select ename from Emp where did in(1,2)”
2.6 like進行模糊查詢
用like進行模糊查詢時有兩個可用的萬用字元:“%”和“_”。“%”代表長度大於等於0的字元,“_”代表長度為1的單個字元。
查詢員工表中姓名中帶有劉的員工
hql=”select ename from Emp where ename like ‘%劉%’”
2.7 邏輯與 and 邏輯或 or
查詢員工中薪水大於2000 同時年齡小於30的員工姓名
hql=”select ename from Emp where esal>2000 and eage<30”;
查詢員工中,年齡大於40或者年齡小於30的員工
hql=”select ename from Emp where eage>40 or eage<30”;
2.8 order by對結果進行排序
對薪水進行排序,從小到大
hql=”select esal from Emp where order by esal asc”;
對年齡進行排序,倒敘,
hql=”select ename from Emp where order by eage desc”;
2.9 group by對記錄進行分組
根據部門分組,求出各組的平均薪水
hql=” select avg(esal) from Emp group by did”;
2.10 having 對分組進行篩選
根據部門分組,查出員工所在組平均薪水大於10000的員工姓名
hql=” select ename from Emp group by did having avg(esal)>10000”;
3 聚集函式
查詢emp員工表中有多少個員工。
sql:select count(ename) from emp;
hql: select count(ename) from Emp;
聚集函式及含義:
hql: select avg(esal) from Emp; 薪水平均值
hql: select max(esal) from Emp; 薪水最大值
hql: select sum(esal) from Emp; 薪水最小值
public void Test91() throws Exception{
Configuration config=new Configuration().configure();
SessionFactory sessionFactory= config.buildSessionFactory();
Session session=null;
Transaction tr=null;
try{
session= sessionFactory.openSession();
tr=session.beginTransaction();
String hql="select count(ename) from Emp";
Object count=session.createQuery(hql).uniqueResult(); //uniqueResult()方法返回Object型別
System.out.println(count);
tr.commit();
}catch(Exception e){
tr.rollback();
}finally{
if(session!=null){
session.close();
}
if(sessionFactory!=null){
sessionFactory.close();
}
}
}
結果:
Hibernate: select count(emp0_.ename) as col_0_0_ from emp emp0_
15
4 子查詢
Hibernate 支援子查詢,所謂子查詢就是,要查詢的欄位及資訊在A表,條件在B表。語法與sql語句相似。
public void Test9() throws Exception{
Configuration config=new Configuration().configure();
SessionFactory sessionFactory= config.buildSessionFactory();
Session session=null;
Transaction tr=null;
try{
session= sessionFactory.openSession();
tr=session.beginTransaction();
String hql="select ename from Emp where did in (select did from Dept where daddress = 302)";
Query query= session.createQuery(hql);
List<String> list = query.list();
for(String message:list){
System.out.println(message);
}
tr.commit();
}catch(Exception e){
tr.rollback();
}finally{
if(session!=null){
session.close();
}
if(sessionFactory!=null){
sessionFactory.close();
}
}
}
結果:
Hibernate: select emp0_.ename as col_0_0_ from emp emp0_ where did in (select dept1_.did from dept dept1_ where dept1_.daddress=302)
李世民
曹操
和珅
劉詩詩
5 導航查詢
e.dept.daddress=301
員工所在部門的地址是301
public void Test9() throws Exception{
Configuration config=new Configuration().configure();
SessionFactory sessionFactory= config.buildSessionFactory();
Session session=null;
Transaction tr=null;
try{
session= sessionFactory.openSession();
tr=session.beginTransaction();
String hql="select ename from Emp e where e.dept.daddress=301 ";
Query query= session.createQuery(hql);
List<String> list = query.list();
for(String message:list){
System.out.println(message);
}
tr.commit();
}catch(Exception e){
tr.rollback();
}finally{
if(session!=null){
session.close();
}
if(sessionFactory!=null){
sessionFactory.close();
}
}
}
結果:
Hibernate: select emp0_.ename as col_0_0_ from emp emp0_, dept dept1_ where emp0_.did=dept1_.did and dept1_.daddress=301
劉德華
賀龍
白百何
文章
林月如