Criteria Query常用的查詢限制方法
Restrictions.like(屬性名, 查詢條件的值, 匹配方式):
Restrictions.in(屬性名, 查詢條件的值, 匹配方式):
Restrictions.eq(屬性名, 查詢條件的值, 匹配方式):
CriteriaQuery常用的查詢限制方法
Restrictions.eq() equal,=
Restrictions.allEq() 引數為Map物件,使用key/value進行多個等於的對比,相當於多個Restrictions.eq()的效果
Restrictions.gt() greater-than, >
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,否則返回false
Restrictions.isNotNull() 與Restrictions.isNull()相反
Order.asc() 根據傳入的欄位進行升序排序
Order.desc() 根據傳入的欄位進行降序排序
MatchMode.EXACT 字串精確匹配,相當於“like 'value'”
MatchMode.ANYWHERE 字串在中間位置,相當於“like '%value%'”
MatchMode.START 字串在最前面的位置,相當於“like 'value%'”
MatchMode.END 字串在最後面的位置,相當於“like '%value'”
public List search(TblFwxx condition) { Session session = this.getSession(); Criteria c = session.createCriteria(TblFwxx.class); if (null != condition) { if (condition.getTitle() != null && !condition.getTitle().equals("")) { c.add(Restrictions.like("title", condition.getTitle(), MatchMode.ANYWHERE)); } } c.addOrder(Order.asc("fwid")); return c.list(); }
-------------------------------------------------------------------------------------
例1:
表中的資料為:
userid name username password age
------------------------------------------------
1 張三 zhang3 zhang3 20
</pre> <wbr> 2 <wbr> <wbr> <wbr>李四 <wbr> <wbr> li4 <wbr> <wbr> <wbr> <wbr> <wbr> li4 <wbr> <wbr> <wbr> <wbr> 21 <wbr> 3 <wbr> <wbr> <wbr> your <wbr> <wbr>test <wbr> <wbr> <wbr> <wbr> <wbr>test <wbr> <wbr> <wbr> <wbr> 30<p></p><p></p><p> </p><p></p><p>------------------------------------------------------------------------------------</p><p></p><p>例2:</p><p></p><p> <wbr></wbr></p><p><pre class="java" name="code"> private Session session; public List criteria(SalChance salChance) { List result = null; try { session = super.getSession(); Criteria cri = session.createCriteria(SalChance.class); if(salChance.getChcCustName()!=null&&salChance.getChcCustName()!=""){ cri.add(Restrictions.like("chcCustName", salChance.getChcCustName(),MatchMode.ANYWHERE)); } if(salChance.getChcTitle()!=null&&salChance.getChcTitle()!=""){ cri.add(Restrictions.like("chcTitle", salChance.getChcTitle(),MatchMode.ANYWHERE)); } if(salChance.getChcLinkman()!=null&&salChance.getChcLinkman()!=""){ cri.add(Restrictions.like("chcLinkman", salChance.getChcLinkman(),MatchMode.ANYWHERE)); } if(salChance.getChcStatus()!=null&&salChance.getChcStatus()!=""){ //System.out.println("狀態為:"+salChance.getChcStatus()); if(salChance.getChcStatus()=="all"){ cri.add(Restrictions.in("chcStatus", new Object[]{"2","3","4"})); }else{ cri.add(Restrictions.eq("chcStatus", salChance.getChcStatus())); } } result=cri.list(); } catch (Exception e) { e.printStackTrace(); }finally { session.close(); } return result;