今天对于前端传递到后端的数据,时使用编码后发送的,导致传递到后端后必须得先进行返还原值再使用,对我们测试阶段,就先不使用这种方法,

 最后在后端可以得到的数据是上述的示例,然后我先拆分成List类型,对于自己定义的四个变量 拆分成数组,然后定义一个函数进行拼接sql语句

拆分字符串

复制代码
package com.example.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Item {
    String logicalOperator;
    String field;
    String value;
    String matchType;

    public static List<Item> parseStringToList(String dp) {
        List<Item> items = new ArrayList<>();
        String[] clauses = dp.split(",");
        for (String clause : clauses) {
            Item item = new Item();

            // Determine logical operator and adjust clause for further parsing
            if (clause.startsWith("AND")) {
                item.setLogicalOperator("AND");
                clause = clause.substring(3).trim();
            } else if (clause.startsWith("OR")) {
                item.setLogicalOperator("OR");
                clause = clause.substring(2).trim();
            }

            // Split the adjusted clause by '=' to get field, value, and matchType
            String[] parts = clause.split("=");
            if (parts.length >= 3) {
                item.setField(parts[0].trim());

                // Combine value and matchType since they come together in the new format
                StringBuilder combinedValueMatchType = new StringBuilder();
                for (int i = 1; i < parts.length; i++) {
                    combinedValueMatchType.append(parts[i]).append("=");
                }
                // Remove the trailing '='
                String combined = combinedValueMatchType.deleteCharAt(combinedValueMatchType.length() - 1).toString();

                // Split combined string into value and matchType
                String[] valueMatchParts = combined.split("matchType=");
                item.setValue(valueMatchParts[0].trim());
                item.setMatchType(valueMatchParts[1].trim().replace("matchType=", ""));
                System.out.println(item.getLogicalOperator()+' '+item.getField()+' '+item.getValue()+' '+item.getMatchType());
                items.add(item);
            }
        }
        return items;
    }

}
复制代码

拼接sql语句

复制代码
    public static String buildSqlConditions(List<Item> items) {
        StringBuilder sqlBuilder = new StringBuilder();

        boolean isFirst = true;
        for (Item item : items) {
            if (isFirst) {
                isFirst = false;
            } else {
                switch (item.getLogicalOperator()) {
                    case "AND":
                        sqlBuilder.append(" AND ");
                        break;
                    case "OR":
                        sqlBuilder.append(" OR ");
                        break;
                }
            }
            sqlBuilder.append("`").append(item.getField()).append("`");
            if ("精确".equals(item.getMatchType())) {
                sqlBuilder.append(" = '").append(item.getValue()).append("'");
            } else if ("模糊".equals(item.getMatchType())) {
                sqlBuilder.append(" LIKE concat(").append(" '%' ,'").append(item.getValue()).append("','%' )");
            }
        }
        return sqlBuilder.toString();
    }
复制代码