hibernate一值多字段模糊查詢


  1. public Page getCoordByPage(Page queryHandler, TCoordinate conditions) {  
  2.         DetachedCriteria dc = DetachedCriteria.forClass(TCoordinate.class);  
  3.         if (conditions.getTAxis() != null) {  
  4.             dc.add(Restrictions.eq("TAxis", conditions.getTAxis()));  
  5.         }  
  6.           
  7.         if(conditions.getCoordinateName()!=null){  
  8.             dc.add(Restrictions.like("coordinateName", conditions.getCoordinateName(),MatchMode.ANYWHERE).ignoreCase());  
  9.         }  
  10.   
  11.         if(conditions.getParentCoordinate().getId()!=null){  
  12.             dc.add(Restrictions.eq("parentCoordinate.id", conditions.getParentCoordinate().getId()));  
  13.         }  
  14.   
  15.         dc.addOrder(Order.desc("addDate"));  
  16.         Page page = this.coordinateDao.getPageByCriteria(queryHandler, dc);  
  17.         return page;  
  18.     }  

 

兩個條件之間or的用法【Restrictions.or(lhs, rhs)】:

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. dc.add(Restrictions.or(Restrictions.like("country", comm.getCountry(),MatchMode.ANYWHERE).ignoreCase(),   
  2.                     Restrictions.like("pinyin", comm.getCountry(),MatchMode.ANYWHERE).ignoreCase()));  



 

 

超過兩個,比如三個條件之間的or【Disjunction disjunction = Restrictions.disjunction();】:

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
    1. if(!ValidateUtils.isEmpty(comm.getCountry())){  
    2.             Disjunction disjunction = Restrictions.disjunction();  
    3.               
    4.             disjunction.add(Restrictions.like("country", comm.getCountry(),MatchMode.ANYWHERE).ignoreCase());  
    5.             disjunction.add(Restrictions.like("pinyin", comm.getCountry(),MatchMode.ANYWHERE).ignoreCase());  
    6.             disjunction.add(Restrictions.like("jianpin", comm.getCountry(),MatchMode.ANYWHERE).ignoreCase());  
    7.               
    8.             dc.add(disjunction);  
    9.         }  
    10.  

      hibernate 查詢match mode的四種模式

       
      Hibernate 查詢MatchMode的四種模式

      MatchMode.START:字符串在最前面的位置.相當於"like 'key%'"
      MatchMode.END:字符串在最后面的位置.相當於"like '%key'"
      MatchMode.ANYWHERE:字符串在中間匹配.相當於"like '%key%'"
      MatchMode.EXACT:字符串精確匹配.相當於"like 'key'"


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM