Sping 中包的方法WebUtils.getParametersStartingWith(request,String);

本文介绍了一种前端利用WebUtils获取特定前缀参数的方法,并展示了如何在后端使用这些参数构建动态查询条件,支持多种查询方式如模糊匹配、范围查询等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  在工作中会遇到,页面参数变化提交参数到后台去查询,现在可以不用改后台代码 ,直接改前天的代码就可以实现  WebUtils.getParametersStasrtingWith(request,String)

举个例子,比如页面上有 表单提交:
<input type="text" name="search_name"value="aileen">
<input type="text" name="search_age"value="12">
<input type="text" name=”search_school"value="而我却">

HttpServletRequest request

//得到所有提交字段中search_的字段

filterMap = WebUtils.getParametersStartingWith(request, "search_");
        for (String key : filterMap.keySet()){

//去掉一些特殊字符
            if (StringUtils.isNotBlank(filterMap.get(key).toString())){
                filterMap.put(key, filterMap.get(key).toString().trim().replace("@", "").replace("!", "").replace("?", "").replace("#", "").replace("'", "").replace("=", "").replace("\\", ""));
            }
        }

就可以得到页面传过来的参数数组,可以拼接sql 查询{还可以在这些数组的后面添加search_channelNo_like 这样的关键字去查询)


这个是Hibernate操作数据库 根据对象中的字段后缀 就可以去创建sql
    /**
     * 根据map条件创建Criterion[],辅助函数
     * @param filterMap
     * @return
     */
    @SuppressWarnings("unchecked")
    public Criterion[] getDefaultCriterion(Map<String, Object> filterMap) {
        Object[] c = {};
        String propertyType = null;
        if (!MapUtils.isEmpty(filterMap)) {
            
            for (Map.Entry<String, Object> entry : filterMap.entrySet()) {
                try {
                    propertyType = BeanUtils.getPropertyType(this.entityClass, entry.getKey()).getName();
                } catch (NoSuchFieldException e) {
                    logger.error("查询信息错误", e);
                    throw new HibernateException(e.getMessage());
                }
                if (entry.getValue() instanceof Map) {
                    Map<String, Object> map = (Map<String, Object>) entry.getValue();
                    if (map.containsKey("like")) {
                        if (propertyType.equals("java.lang.String")) {
                            c = ArrayUtils.add(c, Restrictions.like(entry.getKey(), (String) map.get("like"), MatchMode.ANYWHERE));
                        } else {
                            throw new HibernateException("the property type is error");
                        }
                    }
                    if (map.containsKey("in")) {
                        if (map.get("in") instanceof Object[]) {
                            c = ArrayUtils.add(c, Restrictions.in(entry.getKey(), (Object[]) map.get("in")));
                        } else {
                            throw new HibernateException("the property type is error");
                        }
                    }
                    if (map.containsKey("ge")) {
                        c = ArrayUtils.add(c, Restrictions.ge(entry.getKey(), map.get("ge")));
                    }
                    if (map.containsKey("le")) {
                        c = ArrayUtils.add(c, Restrictions.le(entry.getKey(), map.get("le")));
                    }
                    if (map.containsKey("gt")) {
                        c = ArrayUtils.add(c, Restrictions.gt(entry.getKey(), map.get("gt")));
                    }
                    if (map.containsKey("lt")) {
                        c = ArrayUtils.add(c, Restrictions.lt(entry.getKey(), map.get("lt")));
                    }
                    if (map.containsKey("between")) {
                        if (map.get("between") instanceof Object[]) {
                            Object[] s = (Object[]) map.get("between");
                            if (s[0] == null || s[1] == null) {
                                throw new HibernateException("between method must be two parameters");
                            }
                            c = ArrayUtils.add(c, Restrictions.between(entry.getKey(), s[0], s[1]));
                        } else {
                            throw new HibernateException("the property type is error");
                        }
                    }
                } else {
                    c = ArrayUtils.add(c, Restrictions.eq(entry.getKey(), entry.getValue()));
                }
            }
        }
        
        Criterion[] result = new Criterion[c.length];
        for (int i = 0; i < c.length; i++) {
            result[i] = (Criterion) c[i];
        }
        return result;
    }
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值