Hibernet DetachedCriteria 对象使用,Mark一下~

public QueryResult findByMap(QueryResult queryResult,Map map,DetachedCriteria detachedCriteria,List colList) throws Exception {
        List resultlist = new ArrayList() ;
        if(null==queryResult){
            queryResult = new QueryResult() ;
        }
        Criteria criteria = detachedCriteria.getExecutableCriteria(getSessionFactory().getCurrentSession()) ;
        String propertyPrefix = null ; 
        String propertySuffix = null ;
        List orders = new ArrayList();
        String[] orarr = null ;
        Disjunction disjunction = null;
        for (String key : map.keySet()) {
            if(null==map.get(key)||"".equals(map.get(key))){
                continue;
            }
            if(key.contains("||")){
                disjunction = Restrictions.disjunction();
                orarr = key.split("\\|\\|");
                for (String strOr : orarr) {
                    strOr = strOr.trim();
                    if(strOr.contains(".")){
                        propertyPrefix = StringUtils.substringBeforeLast(strOr,".").trim();
                        propertySuffix = StringUtils.substringAfterLast(strOr,".").trim();
                        //多类型查询条件处理
                        if(propertySuffix.equals("like")){
                            if(((String)map.get(key)).indexOf("%")>-1){
                                disjunction.add(Restrictions.like(propertyPrefix, map.get(key)));
                            }else{
                                disjunction.add(Restrictions.like(propertyPrefix, "%"+map.get(key)+"%"));
                            }
                        }else if(propertySuffix.equals("=")){
                            disjunction.add(Restrictions.eq(propertyPrefix, map.get(key)));
                        }else if(propertySuffix.equals("!=")){
                            disjunction.add(Restrictions.ne(propertyPrefix, map.get(key)));
                        }else if(propertySuffix.equals(">")){
                            disjunction.add(Restrictions.gt(propertyPrefix, map.get(key)));
                        }else if(propertySuffix.equals(">=")){
                            disjunction.add(Restrictions.ge(propertyPrefix, map.get(key)));
                        }else if(propertySuffix.equals("<")){
                            disjunction.add(Restrictions.lt(propertyPrefix, map.get(key)));
                        }else if(propertySuffix.equals("<=")){
                            disjunction.add(Restrictions.le(propertyPrefix, map.get(key)));
                        }
                    }else{
                        disjunction.add(Restrictions.eq(strOr, map.get(key))) ;
                    }
                }
                criteria.add(disjunction);
            }else if(key.contains(".")){
                propertyPrefix = StringUtils.substringBeforeLast(key,".").trim();
                propertySuffix = StringUtils.substringAfterLast(key,".").trim();
                //排序处理
                if(propertyPrefix.equalsIgnoreCase("orderby")&&propertySuffix.equals("asc")){
                    orders.add(Order.asc((String)map.get(key))) ;
                }else if(propertyPrefix.equalsIgnoreCase("orderby")&&propertySuffix.equals("desc")){
                    orders.add(Order.desc((String)map.get(key)));
                }
                //多类型查询条件处理
                if((map.get(key) instanceof String)&&((String)map.get(key)).contains("||")){
                    disjunction = Restrictions.disjunction();
                    String[] valarr = ((String)map.get(key)).split("\\|\\|");
                    for (String val : valarr) {
                        if(propertySuffix.equals("like")){
                            if(((String)map.get(key)).indexOf("%")>-1){
                                disjunction.add(Restrictions.like(propertyPrefix, val));
                            }else{
                                disjunction.add(Restrictions.like(propertyPrefix, "%"+val+"%"));
                            }
                        }else if(propertySuffix.equals("=")){
                            disjunction.add(Restrictions.eq(propertyPrefix, val));
                        }else if(propertySuffix.equals("!=")){
                            disjunction.add(Restrictions.ne(propertyPrefix, val));
                        }else if(propertySuffix.equals(">")){
                            disjunction.add(Restrictions.gt(propertyPrefix, val));
                        }else if(propertySuffix.equals(">=")){
                            disjunction.add(Restrictions.ge(propertyPrefix, val));
                        }else if(propertySuffix.equals("<")){
                            disjunction.add(Restrictions.lt(propertyPrefix, val));
                        }else if(propertySuffix.equals("<=")){
                            disjunction.add(Restrictions.le(propertyPrefix, val));
                        }
                    }
                    criteria.add(disjunction);
                }else{
                    if(propertySuffix.equals("like")){
                        if(((String)map.get(key)).indexOf("%")>-1){
                            criteria.add(Restrictions.like(propertyPrefix, map.get(key)));
                        }else{
                            criteria.add(Restrictions.like(propertyPrefix, "%"+map.get(key)+"%"));
                        }
                    }else if(propertySuffix.equals("=")){
                        criteria.add(Restrictions.eq(propertyPrefix, map.get(key)));
                    }else if(propertySuffix.equals("!=")){
                        criteria.add(Restrictions.ne(propertyPrefix, map.get(key)));
                    }else if(propertySuffix.equals(">")){
                        criteria.add(Restrictions.gt(propertyPrefix, map.get(key)));
                    }else if(propertySuffix.equals(">=")){
                        criteria.add(Restrictions.ge(propertyPrefix, map.get(key)));
                    }else if(propertySuffix.equals("<")){
                        criteria.add(Restrictions.lt(propertyPrefix, map.get(key)));
                    }else if(propertySuffix.equals("<=")){
                        criteria.add(Restrictions.le(propertyPrefix, map.get(key)));
                    }
                }
            }else{
                if(key.equalsIgnoreCase("orderby")){
                    orders.add(Order.asc((String)map.get(key))) ;
                }else{
                    criteria.add(Restrictions.eq(key, map.get(key)));
                }
            }
        }
        Integer totalCount = ((Long)criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
        queryResult.setRecordtotal(totalCount) ;
        if(queryResult.getIndex()>queryResult.getPageCount()){
            return queryResult;
        }
        if(totalCount>0){
            for (Order o : orders) {
                criteria.addOrder(o) ;
            }
            ProjectionList  proList = null;//设置投影集合
            if(null!=colList&&colList.size()>0){
                proList = Projections.projectionList();//设置投影集合
                for (String colName : colList) {
                    proList.add(Projections.alias(Projections.property(colName), colName)); 
                }
            }
            criteria.setProjection(proList);
            criteria.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
            criteria.setFirstResult((queryResult.getIndex()-1)*queryResult.getPageSize()) ;
            criteria.setMaxResults(queryResult.getPageSize()) ;
            resultlist = (List)criteria.list();
        }
        queryResult.setResultlist(resultlist) ;
        return queryResult;
    }

你可能感兴趣的:(Hibernate,Criteria,hibernate)