Hibernate 入門----Query,Criteria,SQLQuery
HQL(Hibernate Query Language)是一種Hibernate專用的查詢語句,基於面向對象的模式,將SQL語句轉化成對象的操作格式.
list() 查詢多條
@Test public void query(){ // sql : select 列名 from 表明; // hql : select 屬性名 from 類名; // select 別名.屬性 from 類名 as 別名; String hql = "from User"; Query query = session.createQuery(hql); List list = query.list(); System.out.println(list); }
查詢多個屬性時,返回的是對象數組;
其中對象中第一個為id,第二個元素為pwd
String hql = "select id,pwd from user";
List<Object[]> list = session.createQuery(hql);
uniqueResult() 查詢一條.
String hql = "select count(*) from User"; Query query = session.createQuery(hql); Object uniqueResult = query.uniqueResult();
分頁查詢
setFirstResult(N):查詢的第N條數據編號
setMaxResults(20):查詢的數據總量
String hql = "from User";
Query query = session.createQuery(hql);
query.setFirstResult(1);
query.setMaxResults(20);
List list = query.list();
System.out.println(list);
可變參數查詢(占位符)
String hql = "from User where name = ? and age = ?"; Query query = session.createQuery(hql); query.setParameter(0, "fy"); // query.setString(1, "fy"); query.setParameter(1, "18"); List list = query.list(); System.out.println(list); 常用寫法: String hql = "from User where name = :name and age = :age"; query.setString("name", "fy"); query.setString("age", "18");
聚合函數
count 返回的結果:Long
max/min 返回的結果:原始類型
sum 返回的結果:Long或Double
avg 返回的結果:Double
由於返回結果通常為單一數據,常使用uniqueResult()獲得結果
分組查詢
Query q = s.createQuery("select address,count(address) from UserModel group by address");
排序查詢
Query q = s.createQuery("from UserModel order by age desc");
投影數據封裝
前提: 對象模型中需要有UserModel(age,name)構造方法,參數順序必須與hql中投影一致.
s.createQuery("select new UserModel(age,name) from UserModel");
———華麗分割——–hibernate 的Criteria
Criteria查詢又名QBC查詢,以面向對象格式完成查詢操作,實現真正的自動生成SQL語句.
查詢單一屬性: 返回該屬性類型
Criteria criteria = session.createCriteria(User.class);
//Hibernate: select this_.name as y0_ from userdata this_
方式一:
Property pro = Property.forName("name");
criteria.setProjection(pro);
方式二:
PropertyProjection property = Projections.property("name");
criteria.setProjection(property);
List list = criteria.list();
criteria.setProjection(property);不能重復復制,如果重復設置的話會把上一個覆蓋.
查詢多個屬性: 查詢結果為對象數組
1.聲明查詢屬性的列表對象
ProjectionList li = Projections.projectionList();
2.聲明要查詢的屬性
PropertyProjection name = Projections.property("name");
PropertyProjection id = Projections.property("id");
3.將查詢的屬性放入屬性列表
li.add(id);
li.add(name);
4.為criteria設置屬性列表
criteria.setProjection(li);
List<Object[]> list = criteria.list();
聚合函數
count: 行數
Projection condition = Projections.rowCount();
max/min: 最大值
Projection conditionMax = Projections.max("age");
Projection conditionMin = Projections.min("age");
Sum : 和
Projection conditionSum = Projections.sum("age");
Avg : 平均數
Projection conditionAvg = Projections.avg("age");
分組查詢
Projections.groupProperty("address");
排序
criteria.addOrder(Order.desc("age"));
criteria.addOrder(Order.asc("age"));
條件查詢.
條件查詢使用
Restrictions.eq 等於= Restrictions.allEq 使用Map,使用key/value進行多個等於的判斷
Restrictions.gt 大於>
Restrictions.ge 大於等於>=
Restrictions.lt 小於<
Restrictions.le 小於等於<=
Restrictions.between 對應sql的between子句
Restrictions.like 對應sql的like子句
Restrictions.in 對應sql的in子句
Restrictions.and and 關系
Restrictions.or or關系
Restrictions.sqlRestriction Sql限定查詢
Criterion c = Restrictions.allEq(arg0);
criteria.add(c);
離線查詢 (DetachedCriteria)
將查詢字段在view層封裝到DetachedCriteria對象中,
DetachedCriteria不依賴session即可創建. 這樣session就不會暴露在view以及業務層.最後在Controller層,通過session把DetachedCriteria裝換成可以執行查詢的Criteria.
表現層:
DetachedCriteria dc = DetachedCriteria.forClass(UserModel.class);
dc.add(Restrictions.like("address", "%"+address+"%"));
dc.add(Restrictions.between("age", age1, age2));
數據層:
Criteria c = dc.getExecutableCriteria(session);
c.list();
———華麗分割——–hibernate 的SQLQuery查詢
直接執行原生的sql語句.
String sql = "select * from tbl_user";
SQLQuery sq = session.createSQLQuery(sql);
sq.list(); //uniqueResult()
通過別名為查詢的模型數據進行封裝.
String sql = "select u.* from tbl_user u";
SQLQuery sq = session.createSQLQuery(sql);
sq.addEntity("別名",模型類.class); // sq.addEntity("u",模型類.class);
———華麗分割——–配置型SQL/HQL語句
1.在hbm.xml的hibernate-mapping元素中配置
Hibernate 入門----Query,Criteria,SQLQuery