zoukankan      html  css  js  c++  java
  • hibernate带条件的关联查询

    public List queryPage(Class clazz, String[] cascades, int start, int limit,
                List<Criterion> exs) {
            Criteria criteria = getSession().createCriteria(clazz);
            /**
             * 设置分页限制
             */
            if (cascades.length > 0) {
                for (String c : cascades) {
                    criteria = getSession().createCriteria(clazz).setFetchMode(c,
                            FetchMode.EAGER);
                    if (exs != null && exs.size() > 0) {
                        for (Criterion i : exs) {
                            criteria.add(i);
                        }
                    }
                    if (!String.valueOf(start).isEmpty()) {
                        criteria.setFirstResult(start);
                    }
                    if (!String.valueOf(limit).isEmpty()) {
                        criteria.setMaxResults(limit);
                    }
                }
            }
            return criteria.list();
    
        }
    /**
         * 条件查询
         * 
         * @return
         * @throws ParseException
         */
        public List<Criterion> completeCriterion() throws ParseException {
            List<Criterion> cris = new ArrayList<Criterion>();
            Enumeration params = (Enumeration) this.servletRequest
                    .getParameterNames();
            while (params.hasMoreElements()) {
                String param = (String) params.nextElement();
                if (param.contains("_")) {
                    String[] array = param.split("_");
                    String paramname = "";
                    String action = "";
                    String type = "";
                    if (array.length == 3) {
                        paramname = array[0];
                        action = array[1];
                        type = array[2];
                    }
                    if (array.length == 4) {
                        paramname = array[0] + "." + array[1];
                        action = array[2];
                        type = array[3];
                    }
                    if (Util.isValidSring(paramname) && Util.isValidSring(action)
                            && Util.isValidSring(type)) {
                        String paramvalue = this.servletRequest.getParameter(param);
                        if (Util.isValidSring(paramvalue) == true) {
                            Criterion c = null;
                            if (action.equals("LIKE")) {
                                c = Expression.like(paramname, this.value(type,
                                        paramvalue).toString(), MatchMode.ANYWHERE);
                            }
                            if (action.equals("GT")) {
                                c = Expression.gt(paramname, this.value(type,
                                        paramvalue));
                            }
                            if (action.equals("LT")) {
                                c = Expression.lt(paramname, this.value(type,
                                        paramvalue));
                            }
                            if (action.equals("EQ")) {
                                c = Expression.eq(paramname, this.value(type,
                                        paramvalue));
                            }
                            if (c != null) {
                                cris.add(c);
                            }
                        }
                    }
                }
            }
            return cris;
        }
    
        public Object value(String type, String paramvalue) throws ParseException {
            if (type.equals("DATE")) {
                return DateFormat.getDateInstance().parse(paramvalue);
            }
            if (type.equals("LONG")) {
                return Long.valueOf(paramvalue);
            }
            if (type.equals("INT")) {
                return Integer.valueOf(paramvalue);
            } else {
                return paramvalue;
            }
        }
    
        /**
         * 匹配公式符号
         * 
         * @param f
         * @return
         */
        public String completeFormula(String f) {
            if (f.equals("LIKE")) {
                return " like ";
            }
            if (f.equals("GT")) {
                return " > ";
            }
            if (f.equals("LT")) {
                return " < ";
            }
            if (f.equals("EQ")) {
                return " = ";
            } else {
                return "";
            }
        }

    用法

        int st = (getPageI() - 1) * getLimitI();
                List list = this.cservice.queryPage(Job.class,
                        new String[] { "depart" }, st, getLimitI(),
                        completeCriterion());

    参数的形式如下:

    name_EQ_STRING

    createDate_GT_DATE

    createDATE_LT_DATE

    sex_EQ_INT

    age_EQ_LONG

  • 相关阅读:
    Spring Security(09)——Filter
    Spring Security(08)——intercept-url配置
    Spring Security(07)——缓存UserDetails
    Spring Security(06)——AuthenticationProvider
    Spring Security(05)——异常信息本地化
    Spring Security(04)——认证简介
    xss小总结
    xss hack学习靶场 writeup
    sqlmap使用
    sqli-labs writeup(less1---less38)
  • 原文地址:https://www.cnblogs.com/tatame/p/2552524.html
Copyright © 2011-2022 走看看