1.criteria介紹
criteria與session繫結,其生命週期跟隨著session結束而結束,使用criteria時進行查詢時,每次都要於執行時期動態建立物件,並加入各種查詢條件,隨著session的**,criteria也跟著**。
org.hibernate.criteria實際上是個條件附加的容器,如果想要設定查詢條件,則要使用org.hibernate.criterion.restrictions的各種靜態方法傳回org.hibernate.criterion.criteria例項,傳回的每個org.hibernate.criterion.criteria例項代表著乙個條件。
2.criterion介紹
criterion 是 criteria 的查詢條件。criteria 提供了 add(criterion criterion) 方法來新增查詢條件。criterion 的例項可以通過 restrictions 工具類來建立,restrictions 提供了大量的靜態方法,如 eq (等於)、 ge (大於等於)、 between 等來方法的建立 criterion 查詢條件。除此之外, restrictions 還提供了方法來建立 conjunction 和disjunction 例項,通過往該例項的 add(criteria) 方法來增加查詢條件形成乙個查詢條件集合。
3.restrictions的幾個常用限定查詢方法如下表所示:
restrictions.eq --> equal,等於.
restrictions.alleq --> 引數為map物件,使用key/value進行多個等於的比對,相當於多個restrictions.eq 的效果
restrictions.gt --> great-than > 大於
restrictions.ge --> great-equal >= 大於等於
restrictions.lt --> less-than, < 小於
restrictions.le --> less-equal <= 小於等於
restrictions.between --> 對應sql的between子句
restrictions.like --> 對應sql的like子句
restrictions.in --> 對應sql的in子句
restrictions.and --> and 關係
restrictions.or --> or 關係
restrictions.isnull --> 判斷屬性是否為空,為空則返回true 相當於sql的 is null
restrictions.isnotnull --> 與isnull相反 相當於sql的 is not null
restrictions.sqlrestriction --> sql限定的查詢
order.asc --> 根據傳入的字段進行公升序排序
order.desc --> 根據傳入的字段進行降序排序
matchmode.exact --> 字串精確匹配.相當於"like 'value'"
matchmode.anywhere --> 字串在中間匹配.相當於"like '%value%'"
matchmode.start --> 字串在最前面的位置.相當於"like 'value%'"
matchmode.end --> 字串在最後面的位置.相當於"like '%value'"
4.例項應用
(1)多個條件("與"的方式進行查詢)
//利用hibernate對資料庫中的studententity表進行查詢,將查詢結果放入乙個集合返回
public
static listquerystudentbyuserinput(string studentname, string realaddress, string riskrank)
(2)多個條件("或"的方式進行查詢)
//利用hibernate對資料庫中的studententity表進行查詢,將查詢結果放入乙個集合返回
public
static listquerystudentbyuserinput(string studentname, string realaddress, string riskrank)
Hibernate中查詢資料庫的幾
hibernate中查詢資料庫的幾種方法 陽東明 hibernate中查詢資料庫中的資料是很重要的,我們有4種方法 1 get 和load 他們的用法完全一樣,都需要兩個引數,第1個是持久化物件類名.class,第2個是行號,也就是說這2個方法都只能返回固定的某一行的資料,但是需要注意的是 當輸入的...
hibernate資料庫分頁查詢
每頁顯示10條記錄,顯示書籍資訊的第二頁 public static void main string args hibernatesessionfactory.closesession 子查詢 查詢出從來沒有購買任何書籍的使用者public static void main string args...
Postgre資料庫中Hibernate主鍵配置
postgre資料庫表中,使用serial自增型字段作為資料表的主鍵時,hibernate在插入資料時會報 的異常。得知錯誤在 sql select nextval hibernate sequence 從提示可知得到hibernate sequence的下乙個值失敗,原來是資料庫中不存在hiber...