Hibernate HQL基礎 限定查詢條件(這裏面有各種條件查詢)

在HQL中可以使用表達式完成指定的運算或者作爲限制查詢結果的條件。如下表所示

|-----------------------------------------------------------|
|   類別                    |           運算符              |
|---------------------------|-------------------------------|
|邏輯運算符                 |or、and及not                   |
|---------------------------|-------------------------------|
|數學運算符                 |+、-、*、/                     |
|---------------------------|-------------------------------|
|                           |=、<>、!=、<、>、<=、>=、like、|
|                           |not like、between、not between |
|比較運算符                 |in、not in、is null、          |
|                           |is not null、is empty、        |
|                           |is not empty、member of、      |
|                           |not member of                  |
|---------------------------|-------------------------------|
|字符串連接                 | ||                            |
|---------------------------|-------------------------------|
|分支選擇                   |case及case...when...then...else|
|                           |....end                        |
|---------------------------|-------------------------------|
|集合運算                   |some、exists、all、any         |
|---------------------------|-------------------------------|

1、where子句

   在HQL中使用where子句來限定查詢條件,該子句後跟表示限定查詢條件的表達式。如:

   Query query = session.createQuery("from Guestbook where name = 'XXX'");

2、between子句

   在HQL的where子句中使用between可以返回屬性值在指定範圍之內的記錄。如:

   Query query = session.createQuery("from Guestbook where id between 1 and 10");

3、not between子句

   在HQL中的where子句使用not between可以返回屬性值不在指定範圍之內的記錄。如:
   
   Query query = session.createQuery("from Guestbook where id not between 1 and 10");

4、in子句

   在HQL中的where子句使用in可以返回屬性值在指定集合中的記錄。如:

   Query query = session.createQuery("from Guestbook where name in ('XXX','XXX')");

5、not in子句

   在HQL中的where子句使用not in可以返回屬性值不在指定集合中的記錄。如:

   Query query = session.createQuery("from Guestbook where name not in ('XXX','XXX')");

6、like子句
  
   在HQL中的where子句使用like子句可以模糊查找屬性值。"_"可以匹配一個字符,“%”可以匹配0個    多個字符。如:

   Query query = session.createQuery("from Guestbook where name like 'X%'");

7、not like子句
  
   在HQL中的where子句中使用not like子句可以模糊查詢屬性值的取非。如:

   Query query = session.createQuery("from Guestbook where name not like 'X%'");

8、and子句

   在HQL中的where子句中使用and表示查詢條件之間與的關係。如:
 
   Query query = session.createQuery("from Guestbook where name = 'XXX' and id<10");

9、or子句

   在HQL中的where子句中使用or表示查詢條件之間或的關係。如:

   Query query = session.createQuery("from Guestbook where name='XX' or name = ’XXX‘");

10、not子句

   在HQL中的where子句中使用not表示查詢條件的非。如:

   Query query = session.createQuery("from Guestbook where not(name='XXX')");

11、is null子句

   在HQLwhere子句中,is null用來判斷實體類的某些屬性值是否爲空。如:

   Query query = session.createQuery("from Guestbook where email is null");

11、is not null子句

   在HQLwhere子句中,is null用來判斷實體類的某些屬性值是否不爲空。如:

   Query query = session.createQuery("from Guestbook where email is not null"); 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章