zoukankan      html  css  js  c++  java
  • 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'"
  • 相关阅读:
    KafkaSpout 重复消费问题解决
    FastJson 输出值 首字母大小写问题
    Kafka0.7运行时报错 kafka/javaapi/consumer/ConsumerConnector : Unsupported major.minor version 51.0 解决
    Zookeeper原理与Curator使用
    Strom 消息处理机制 中英对照翻译 (Storm如何保证消息被完全处理)
    Mac安装 Storm 小结
    linux下实现ftp上传文件
    Task 0.0 in stage 1.0 (TID 1) had a not serializable result: org.apache.hadoop.hbase.client.Result
    Spark操作HBase
    maven-pom-project文件报错
  • 原文地址:https://www.cnblogs.com/Sincerity/p/5568766.html
Copyright © 2011-2022 走看看