前端修改条件,后端无须调整代码,动态拼接查询

一、条件对应符号

条件对应符号
包含like
不包含notLike
开始以likeLeft
结束以likeRight
介于between
不介于notBetween
在列表in
不在列表notIn
等于eq
不等于ne
小于lt
小于等于le
大于gt
大于等于ge

二、前端传值

把字段对应的条件放到请求头中

字段对应条件 组装为json对象的字符串

keyvalue
condition{ “name”:”eq”,”age”:”between”,”score”:”in”}

查询值正常传递

//json格式
{
    "name":"xxxx",
    "age":"12,15",  //介于
    "score":"60,65,53" //在列表
}

//表单格式
name:xxxx
age:12,15  //介于
score:60,65,53 //在列表

三、后端处理

使用对象接收 例:UserQuery

@Data
@EqualsAndHashCode(callSuper = true)
public class UserQuery extends Query implements Serializable {
    private static final long serialVersionUID = -6966068051596968967L;
    /**
     * 人员主键
     */
    private Long id;
    /**
     * 人员姓名
     */
    private String name;
    /**
     * 年龄
     */
    private Integer age;
    /**
     * 分数
     */
    private Float score;
}
//1、创建查询
QueryWrapper<User> queryWrapper = Wrappers.<User>query();
//2、获取请求头
String condition = request.getHeader("condition");
//3、组装queryWrapper,统一方法packageQuery
packageQuery(queryWrapper,userQuery,condition)
//查询数据库
IPage<User> page = this.page(Condition.getPage(userQuery), queryWrapper);

//packageQuery 逻辑
private void packageQuery(QueryWrapper queryWrapper, Query query, String condition) {
        //转换获取字段的条件map
        Map<String, Object> conditionMap = new HashMap<>();
        if (Func.isNotEmpty(condition)) {
            conditionMap = JsonUtil.toMap(condition);
        }
        //反射获取所有的字段
        Map<String, Object> finalConditionMap = conditionMap;

        Map<String, Method> methodMap = new HashMap<>();
        Method[] methods = QueryWrapper.class.getMethods();
        for (Method method : methods) {
            methodMap.put(method.getName(), method);
        }

        ReflectionUtils.doWithLocalFields(query.getClass(), field -> {
            //获取字段名
            String name = field.getName();
            if ("serialVersionUID".equals(name)) {
                return;
            }
            field.setAccessible(true);
            Class<?> typeClass = field.getType();
            //获取值
            Object o = field.get(query);
            if (o != null) {
                String methodName = (String) finalConditionMap.getOrDefault(name, "eq");
                Method method = methodMap.get(methodName);
                if (method == null) {
                    return;
                }
                try {
                    if ("in".equals(methodName) || "notIn".equals(methodName)) {
                        List list = Arrays.asList(((String) o).split(",")).stream().map(t -> Convert.convert(typeClass, t)).collect(Collectors.toList());
                        method.invoke(queryWrapper, name, list.toArray());
                    } else if ("between".equals(methodName) || "notBetween".equals(methodName)) {
                        String[] split = ((String) o).split(",");
                        method.invoke(queryWrapper, name, split[0], split[1]);
                    } else {
                        method.invoke(queryWrapper, name, Convert.convert(typeClass, o));
                    }
                } catch (Exception e) {
                    throw new ServiceException("参数有误执行query异常",e);
                }
            }
        });
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值