Hibernet DetachedCriteria 对象使用,Mark一下~

public QueryResult<T> findByMap(QueryResult<T> queryResult,Map<String, Object> map,DetachedCriteria detachedCriteria,List<String> colList) throws Exception {
        List<T> resultlist = new ArrayList<T>() ;
        if(null==queryResult){
            queryResult = new QueryResult<T>() ;
        }
        Criteria criteria = detachedCriteria.getExecutableCriteria(getSessionFactory().getCurrentSession()) ;
        String propertyPrefix = null ; 
        String propertySuffix = null ;
        List<Order> orders = new ArrayList<Order>();
        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<T>)criteria.list();
        }
        queryResult.setResultlist(resultlist) ;
        return queryResult;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值