天天看點

第十一章 Hibernate的查詢 Query by Criteria(QBC)

QBC(QueryByCriteria)查詢方式是Hibernate提供的“更加面向對象”的一種檢索方式。QBC在條件查詢上比HQL查詢更為靈活,而且支援運作時動态生成查詢語句。 

Hibernate中Restrictions的方法       說明 

Restrictions.eq                                    = 

Restrictions.allEq                               利用Map來進行多個等于的限制 

Restrictions.gt                                     > 

Restrictions.ge                                    

>= 

Restrictions.lt                                      <

Restrictions.le                                      <= 

Restrictions.between                           BETWEEN

Restrictions.like                                   LIKE

Restrictions.in                                     in

Restrictions.and                                  and

Restrictions.or                                     or

Restrictions.sqlRestriction                 用SQL限定查詢 

查詢id大于100,小于200的使用者:

通過Example工具類,判斷Example.create(example)傳進來的example對象裡面各個屬性是否與資料庫中配置。

Order.asc(StringpropertyName)

升序排序

Order.desc(StringpropertyName)

降序排

Porjections.avg(StringpropertyName)

求某屬性的平均值

Projections.count(StringpropertyName)

統計某屬性的數量

Projections.countDistinct(StringpropertyName)

統計某屬性的不同值的數量

Projections.groupProperty(StringpropertyName)

指定一組屬性值

Projections.max(StringpropertyName)

某屬性的最大值

Projections.min(StringpropertyName)

某屬性的最小值

Projections.projectionList()

建立一個新的projectionList對象

Projections.rowCount()

查詢結果集中記錄的條數

Projections.sum(StringpropertyName)

傳回某屬性值的合計

7、QBC分頁查詢