JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Object Relational Mapping  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
作者 Hibernate對象模糊查詢自動生成方法
panjacke





發文: 17
積分: 0
於 2010-05-31 17:24 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這方法是從網頁表彰收集到的數據,STRUTS2會幫你封裝,傳入這個方法,再給個別名就能自動 生成模糊查詢HQL語句,日期時間段查詢屬性名我是開始時間屬性名+“2”=結束時間段的屬性名
完全自主研發,如有雷同,那是不可能的

請高手指點指點

public static String spellHql(Object targetClass(根據該對象查詢),String oo(別名)) throws Exception{
  String hql = "";
  String hql2 = hql;
  Field[] f = targetClass.getClass().getDeclaredFields();
  String andWord = "=";
  Map<String, String> map = new HashMap<String, String>();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
  for(int i=0;i<f.length;i++){//循环目标对象每一个属性取其值
    String methodName = f[i].getName();
    String properties = methodName;
    Method method;
    Object r;
    try{
      methodName = "get"+methodName.substring(0,1).toUpperCase()+methodName.substring(1);
      method = targetClass.getClass().getMethod(methodName);
      r = method.invoke(targetClass, new Object[]{});
    }catch(NoSuchMethodException e){
      continue;
    }
    if(null!=r&&!r.toString().trim().equals("")){
      String rr = r.toString();
      if(isMyType(r)){//如果是我的类型则要调用本方法先把我的类型语句生成(该类中包含的属性是我定义的类型)
        String rrr = spellHql(r, oo+"."+properties);
        if(!rrr.trim().equals("")){
          if(!hql.equals(""))
            hql = hql + " and " + rrr;//生成后加上一个and继续不是我的类型的属性的生成
          else
            hql = hql + rrr;//如果是第一个属性生成后不加and继续不是我的类型的属性的生成
        }
        continue;
      }else if(rr.equals("0")||rr.equals("0.0")||rr.trim().equals("")||rr.trim().equals("0L")){//数字型没值
        continue;//如果没值跳到下一属性
      }else if(r instanceof Integer || r instanceof Short || r instanceof Double){//数字型有值/布尔肯定有值
        andWord = "=";
      }else if(r instanceof java.lang.String){//字符串就要判断是日期还是不是
        rr = "'"+rr+"'";
        String mn = methodName.toUpperCase();
        if(mn.indexOf("DATE")>0){//如果属性名中有date说明是日期informixt的table命名
          andWord = " between ";
          map.put(properties.toUpperCase(),rr);
            String firstDate = map.get(properties.toUpperCase().substring(0,properties.length()-1));
            String endDate = map.get(properties.toUpperCase()+2);
            String propertyName = properties.substring(0,properties.length()-1);
            if(null!=firstDate){//如果开始日期的值早已保存到一个map里那么把两个值加到sql语句中
              r = firstDate +" and " + rr;
              properties = propertyName;
            }else if(null!=endDate){// 与上一个if 相反
              r = rr +" and " + endDate;
            }else {
              if(properties.toUpperCase().indexOf("DATE2")>0){//如果属性名中有DATE2的字说明是结束日期
                method = targetClass.getClass().getMethod(methodName.substring(0,methodName.length()-1));
                Object fd = method.invoke(targetClass, new Object[]{});
                String date = sdf.format(new Date());
                // 如果结束日期有值而开始日期没值,则会生成小等于这个日期的查询语句
                if((null==fd||fd.toString().trim().equals(""))&&!r.toString().trim().equals(date)){
                  andWord = " <= ";
                  properties = propertyName;
                  r = rr;
                }else continue;
                      
              }else continue;
              
            } //如果没值跳到下一属性
        }else {
          andWord = " like ";
          r = "'%"+r+"%'";
        }
      }else continue;//如果没值跳到下一属性
      if(!hql.equals(hql2)){
        hql = hql+" and ";
      }
      hql = hql +oo+"."+ properties +andWord+r;
    }else{
      continue;
    }
  }
  return hql;
}


panjacke edited on 2010-05-31 17:28
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
961 Hibernate對象模糊查詢自動生成方法 panjacke 3434 2010-05-31 17:24
» JWorld@TW »  Object Relational Mapping

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8