hql基本查詢
單屬性和多屬性查詢
listlist = session.createquery("select name,code from province").list();
for (int i = 0;i將多個查詢分裝成物件
listlist1 = session.createquery("select new province(id,name,code) from province").list();
for (int i = 0;i別名使用
listlist = session.createquery("select new province(p.id,p.name,p.code) from province as p").list();
for(province province:list)
listlist = session.createquery("select p from province p").list();
for(province province:list)
查詢所有列(不能使用*)
使用別名和使用from
listlist = session.createquery("select p from province p").list();
for(province province:list)
listlist = session.createquery("from province").list();
for(province province:list)
佔位符? 從0開始
使用setparameter和命名查詢詢,命名查詢–設定條件引數的名稱 以冒號開頭後更名稱 設定引數時 只需指定名
setinterger(0,4)跟setparameter(0,4)效果一樣
listlist = session.createquery("from province p where id<?")
.setparameter(0,4).list();
for(province province:list)
listlist = session.createquery("from province p where id<:id")
.setparameter("id",4).list();
for(province province:list)
分頁查詢
/*
setfirstresult():開始從這個下標開始,不包括這個下標
setmaxresults():設定每頁記錄數pagesize
*//* listlist = session.createquery("from province p")
.setfirstresult(3).setmaxresults(9).list();
for(province province:list) */
統計查詢
number count = (number) session.createquery("select max(p.id) from province p").uniqueresult();
system.out.println("總數:"+count.bytevalue());
分組查詢
listlist = session.createquery("select p.name,count(p.id) from province p group by p.name").list();
for (object objes:list)
排序
desc降序
asc公升序
listlist = session.createquery("from province order by id desc").list();
for (province province:list)
連線查詢
內連線
string hql = "from city c where c.province.name=:name";
hql = "select c from city c join c.province d where d.name=:name";
hql = "select c from city c inner join c.province d where d.name=:name";
listlist = session.createquery(hql).setstring("name","河南省").list();
for (city c:list
)
左外連線
string hql ="select p.name,c.name from city c left outer join c.province p";
query query = session.createquery(hql);
listlist = query.list();
for (object o:list
)
本地查詢 sql查詢
string sql="select name,price from book";
listlist =session.createsqlquery(sql)
.list();
for(object b:list)
hql簡單查詢語句
session中get 方法只能通過id來查詢結果 hibernate工具類的完善 hibernateutil public class hibernateutil static public static sessionfactory getsessionfactory public static...
hql語句查詢返回List Map
查詢其中幾個字段,新增new map 注意map裡的m是小寫的。也不需要匯入包,這樣通過query.list 出來的list裡存放的不再是預設的object陣列了,而是map集合了 string hql select new map name,passwd from users query quer...
hql語句 單錶查詢
在hibernate中,有下列比較常用的查詢語句 1 hql hibernate query language 2 criteria queries 條件查詢 3 native queries 原生態sql語句查詢 最常用的就是hql,那麼 使用hql進行查詢,又分為 單錶查詢,一對多和多對多查詢,...