更新時(shí)間:2022-02-11 10:32:34 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽697次
• HQL(Hibernate Query Language)提供了豐富靈活的查詢(xún)方式,使用HQL進(jìn)行查詢(xún)也是Hibernate官方推薦使用的查詢(xún)方式。
• HQL在語(yǔ)法結(jié)構(gòu)上和SQL語(yǔ)句十分的相同,所以可以很快的上手進(jìn)行使用。使用HQL需要用到Hibernate中的Query對(duì)象,該對(duì)象專(zhuān)門(mén)執(zhí)行HQL方式的操作。
查詢(xún)所有示例
session.beginTransaction();
String hql = "from User"; // from 后跟的是要查詢(xún)的對(duì)象,而不是表
Query query = session.createQuery(hql);
List<User> userList = query.list();
for(User user:userList){
System.out.println(user.getUserName());
}
session.getTransaction().commit();
帶where的查詢(xún)示例
session.beginTransaction();
String hql = "from User where userName = 'James'";
Query query = session.createQuery(hql);
List<User> userList = query.list();
for(User user:userList){
System.out.println(user.getUserName());
}
session.getTransaction().commit();
/*
在HQL中where語(yǔ)句中使用的是持久化對(duì)象的屬性名,如上面示例中的userName。當(dāng)然在HQL中也可以使用別名
*/
String hql = "from User as u where u.userName = 'James'";
/*
過(guò)濾條件
在where語(yǔ)句中還可以使用各種過(guò)濾條件,如:=、<>、<、>、>=、<=、between、not between、
in、not in、is、like、and、or等
*/
獲取一個(gè)不完整的對(duì)象
session.beginTransaction();
String hql = "select userName from User";
Query query = session.createQuery(hql);
List<Object> nameList = query.list();
for(Object obj:nameList){
String name=(String)obj;
System.out.println(name);
}
session.getTransaction().commit();
// 多個(gè)屬性的話(huà),需要用object[]接收
session.beginTransaction();
String hql = "select userName,userPwd from User";
Query query = session.createQuery(hql);
List nameList = query.list();
for(Object obj:nameList){
Object[] array = (Object[]) obj; // 轉(zhuǎn)成object[]
System.out.println("name:" + array[0]);
System.out.println("pwd:" + array[1]);
}
session.getTransaction().commit();
統(tǒng)計(jì)和分組查詢(xún)
session.beginTransaction();
String hql = "select count(*),max(id) from User";
Query query = session.createQuery(hql);
List nameList = query.list();
for(Object obj:nameList){
Object[] array = (Object[]) obj;
System.out.println("count:" + array[0]);
System.out.println("max:" + array[1]);
}
session.getTransaction().commit();
/*
該條sql語(yǔ)句返回的是單條數(shù)據(jù),所以還可以這樣寫(xiě)
單列數(shù)據(jù)用Object,多列數(shù)據(jù)用Object[]
*/
Object[] object = (Object[]) query.uniqueResult();
System.out.println("count:" + object[0]);
System.out.println("max:" + object[1]);
更多寫(xiě)法
select distinct name from Student;
select max(age) from Student;
select count(age),age from Student group by age;
from Student order by age;
HQL占位符
session.beginTransaction();
String hql = "from User where userName = ?";
Query query = session.createQuery(hql);
// 索引從0開(kāi)始
query.setString(0, "James");
List<User> userList = query.list();
for(User user:userList){
System.out.println(user.getUserName());
}
session.getTransaction().commit();
HQL引用占位符
session.beginTransaction();
String hql = "from User where userName = :name";
Query query = session.createQuery(hql);
query.setParameter("name", "James");
List<User> userList = query.list();
for(User user:userList){
System.out.println(user.getUserName());
}
session.getTransaction().commit();
HQL分頁(yè)
session.beginTransaction();
String hql = "from User";
Query query = session.createQuery(hql);
query.setFirstResult(0);
query.setMaxResults(2);
List<User> userList = query.list();
for(User user:userList){
System.out.println(user.getUserName());
}
session.getTransaction().commit();
• Criteria對(duì)象提供了一種面向?qū)ο蟮姆绞讲樵?xún)數(shù)據(jù)庫(kù)。Criteria對(duì)象需要使用Session對(duì)象來(lái)獲得。
• 一個(gè)Criteria對(duì)象表示對(duì)一個(gè)持久化類(lèi)的查詢(xún)。
查詢(xún)所有
session.beginTransaction();
Criteria c = session.createCriteria(User.class);
List<User> userList = c.list();
for(User user:userList){
System.out.println(user.getUserName());
}
session.getTransaction().commit();
where
session.beginTransaction();
Criteria c = session.createCriteria(User.class);
c.add(Restrictions.eq("userName", "James"));
List<User> userList = c.list();
for(User user:userList){
System.out.println(user.getUserName());
}
session.getTransaction().commit();
Restrictions對(duì)象
方法名稱(chēng) 對(duì)應(yīng)SQL中的表達(dá)式
----------------------------------------------------------
Restrictions.eq field = value
Restrictions.gt field > value
Restrictions.lt field < value
Restrictions.ge field >= value
Restrictions.le field <= value
Restrictions.between field between value1 and value2
Restrictions.in field in(…)
Restrictions.and and
Restrictions.or or
Restrictions.like field like value
示例
Criteria c = session.createCriteria(User.class);
c.add(Restrictions.like("userName", "J"));
c.add(Restrictions.eq("id", 120));
c.add(Restrictions.or(Restrictions.eq("userName", "James"),
Restrictions.eq("userName", "Alex")));
獲取唯一記錄
session.beginTransaction();
Criteria c = session.createCriteria(User.class);
c.add(Restrictions.eq("id", 120));
User user = (User) c.uniqueResult();
System.out.println(user.getUserName());
session.getTransaction().commit();
分頁(yè)
Criteria c = session.createCriteria(User.class);
c.setFirstResult(0);
c.setMaxResults(5);
分組與統(tǒng)計(jì)
session.beginTransaction();
Criteria c = session.createCriteria(User.class);
c.setProjection(Projections.sum("id"));
Object obj = c.uniqueResult();
System.out.println(obj);
session.getTransaction().commit();
Projections對(duì)象
方法名稱(chēng) 描述
-------------------------------------------------------
Projections.sum 等于SQL中聚合函數(shù)sum
Projections.avg 等于SQL中聚合函數(shù)avg
Projections.count 等于SQL中聚合函數(shù)count
Projections .distinct 去除重復(fù)記錄
Projections.max 等于SQL中聚合函數(shù)max
Projections.min 等于SQL中聚合函數(shù)min
Projections .groupProperty 對(duì)指定的屬性進(jìn)行分組查詢(xún)
多個(gè)統(tǒng)計(jì)與分組
session.beginTransaction();
Criteria c = session.createCriteria(User.class);
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.sum("id"));
projectionList.add(Projections.min("id"));
c.setProjection(projectionList);
// 和HQL一樣,單列用Object,多列用Object[]
Object[] obj = (Object[]) c.uniqueResult();
System.out.println("sum:" + obj[0]);
System.out.println("min:" + obj[1]);
排序
Criteria c = session.createCriteria(User.class);
c.addOrder(Order.desc("id"));
示例
session.beginTransaction();
String sql = "select id,username,userpwd from t_user";
List list = session.createSQLQuery(sql).list();
for(Object item : list){
Object[] rows = (Object[]) item;
System.out.println("id:" + rows[0] + "username:"
+ rows[1] + "userpwd:" + rows[2]);
}
session.getTransaction().commit();
addEntity()示例
session.beginTransaction();
String sql = "select id,username,userpwd from t_user";
// addEntity()可以告訴Hibernate你想要封裝成對(duì)象的類(lèi)型,然后自動(dòng)為你封裝
SQLQuery query = session.createSQLQuery(sql).addEntity(User.class);
List<User> list = query.list();
for(User user : list){
System.out.println(user.getUserName());
}
session.getTransaction().commit();
uniqueResult示例
session.beginTransaction();
String sql = "select id,username,userpwd from t_user where id = 2";
SQLQuery query = session.createSQLQuery(sql).addEntity(User.class);
User user = (User) query.uniqueResult();
System.out.println(user.getUserName());
session.getTransaction().commit();
以上就是關(guān)于“Hibernate查詢(xún)的幾種方式”的介紹,大家如果想了解更多相關(guān)知識(shí),不妨來(lái)關(guān)注一下動(dòng)力節(jié)點(diǎn)的Hibernate視頻教程,教程內(nèi)容細(xì)致全面,通俗易懂,適合小白學(xué)習(xí),希望對(duì)大家能夠有所幫助。
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話(huà)與您溝通安排學(xué)習(xí)
初級(jí) 202925
初級(jí) 203221
初級(jí) 202629
初級(jí) 203743