shiqian 4 жил өмнө
parent
commit
d349dd6451

+ 6 - 0
boman-common/boman-common-core/src/main/java/com/boman/common/core/utils/obj/ObjectUtils.java

@@ -114,6 +114,12 @@ public class ObjectUtils {
         return input;
     }
 
+    /**
+     * 功能描述: 需要判断数据库是什么类型,如果是VARCHAR则需要转,如果是数字则无需转
+     *
+     * @param input 输入
+     * @return java.lang.String
+     */
     public static String escapeStr(String input) {
         return "'" + input + "'";
     }

+ 53 - 0
boman-modules/boman-system/src/main/java/com/boman/system/common/FormDataConstant.java

@@ -1,5 +1,12 @@
 package com.boman.system.common;
 
+import org.apache.commons.compress.utils.Lists;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Stream;
+
 /**
  * @author shiqian
  * @date 2021年03月26日 09:47
@@ -22,7 +29,53 @@ public class FormDataConstant {
     public static final String SHOW_DATA = "showData";
 
 
+    /**
+     * equals
+     */
     public static final String EQ = "EQ";
 
+    /**
+     * like
+     */
     public static final String LIKE = "LIKE";
+
+    /**
+     * not equals
+     */
+    public static final String NE = "NE";
+
+    /**
+     * greater than
+     */
+    public static final String GT = "GT";
+
+    /**
+     * greater than or equal to
+     */
+    public static final String GTE = "GTE";
+
+    /**
+     * less than
+     */
+    public static final String LT = "LT";
+
+    /**
+     * less than or equal to
+     */
+    public static final String LTE = "LTE";
+
+    /**
+     * between and
+     */
+    public static final String BETWEEN = "BETWEEN";
+
+    /**
+     * 需要转义
+     */
+    public static final String VARCHAR = "varchar";
+
+    public static final String CHAR = "char";
+    public static final String DATETIME = "datetime";
+    public static final String TIMESTAMP = "timestamp";
+
 }

+ 15 - 6
boman-modules/boman-system/src/main/java/com/boman/system/common/TableServiceCmdService.java

@@ -10,11 +10,11 @@ import com.boman.common.redis.RedisKey;
 import com.boman.common.redis.service.RedisService;
 import com.boman.gen.domain.GenTable;
 import com.boman.gen.domain.GenTableColumn;
+import com.boman.system.mapper.StandardlyMapper;
 import com.boman.system.service.IBaseSelectService;
 import com.boman.system.service.impl.BaseDeleteService;
 import com.boman.system.service.impl.BaseSaveService;
 import com.boman.system.utils.IdUtils;
-import com.google.common.collect.Lists;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -148,7 +148,6 @@ public class TableServiceCmdService {
 
     /**
      * 功能描述: 封装成查询条件 key: 列名,  value:查询条件_查询类别
-     *              eg: {“columnName”: config_name, "value": "系统配置_like"}
      *              eg: [{"config_name":"系统配置_like"}, {"config_name":"_like"}]
      *
      * @param columns columns
@@ -164,7 +163,8 @@ public class TableServiceCmdService {
             for (GenTableColumn column : columns) {
                 // long string collection 暂时只作此三种类型判断
                 if (column.getColumnName().equalsIgnoreCase(key) && ObjectUtils.isNotEmpty(value)) {
-                    result.put(key, packValue(String.valueOf(value), column.getQueryType()));
+                    // columnType 作为判断需不需要转义的一个标准,防止索引失效
+                    result.put(key, packValue(String.valueOf(value), column.getQueryType(), column.getColumnType()));
                     break;
                 }
             }
@@ -173,9 +173,18 @@ public class TableServiceCmdService {
         return result;
     }
 
-    private String packValue(String value, String type){
+    /**
+     * 功能描述: 封装查询条件,在拼sql的时候按照此规则去取值
+     * {@link StandardlyMapper.SqlProvider#selectByCondition(java.util.Map)}
+     *
+     * @param value      查询的值
+     * @param queryType  queryType
+     * @param columnType columnType
+     * @return java.lang.String
+     */
+    public String packValue(String value, String queryType, String columnType) {
         requireNonNull(value);
-        requireNonNull(type);
-        return value + "_" + type;
+        requireNonNull(queryType);
+        return value + "_" + queryType + "_" + columnType;
     }
 }

+ 16 - 1
boman-modules/boman-system/src/main/java/com/boman/system/controller/ObjController.java

@@ -65,7 +65,22 @@ public class ObjController {
 
     /**
      * 功能描述: 获取单表单数据
-     *
+     *                    eg:{
+     *                          "table": "sys_config",
+     *                          "limit": 0,
+     *                          "offset": 10,
+     *                          "orderBy": "create_time asc",
+     *                          "fixedData": {
+     *                              "condition": {
+     *                                  "CONFIG_NAME": "主框架",
+     *                                  "CONFIG_VALUE": "skin-blue",
+     *                                  "CREATE_TIME": ["2017-01-01", "2019-02-02"]
+     *                              },
+     *                              "showData": [
+     *                                  "CONFIG_ID", "CONFIG_NAME", "CONFIG_VALUE"
+     *                              ]
+     *                            }
+     *                        }
      * @param condition condition
      * @return com.boman.system.common.ValueHolder
      */

+ 46 - 9
boman-modules/boman-system/src/main/java/com/boman/system/mapper/StandardlyMapper.java

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.boman.common.core.utils.obj.ObjectUtils;
 import com.boman.system.common.FormDataConstant;
+import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.ibatis.annotations.*;
 import org.apache.ibatis.annotations.Param;
@@ -15,6 +16,9 @@ import org.springframework.stereotype.Component;
 import java.util.*;
 import java.util.function.Consumer;
 
+import static com.boman.common.core.utils.obj.ObjectUtils.*;
+import static com.boman.system.common.FormDataConstant.*;
+
 /**
  * @author shiqian
  * @description
@@ -412,7 +416,8 @@ public interface StandardlyMapper {
         }
 
         public String selectByCondition(Map<String, Object> para) {
-//            JSONObject condition = (JSONObject) para.get("condition");
+            JSONObject condition = (JSONObject) para.get("condition");
+            LOGGER.info("前台传过来的查询条件: {}", condition.toJSONString());
             JSONObject packCondition = (JSONObject) para.get("packCondition");
             String tableName = (String) para.get("tableName");
             String orderBy = (String) para.get("orderBy");
@@ -440,8 +445,9 @@ public interface StandardlyMapper {
                 String valueStr = ((String) valueObj);
                 String[] split = valueStr.split("_");
                 String value = split[0];
-                String type = split[1];
-                conditionSql.append(key).append(covert(type, key, value)).append(" and ");
+                String queryType = split[1];
+                String columnType = split[2];
+                conditionSql.append(key).append(covert(queryType, columnType, key, value)).append(" and ");
             }
 
             wholeSql.append(StringUtils.substringBeforeLast(conditionSql.toString(), " and"));
@@ -455,17 +461,48 @@ public interface StandardlyMapper {
             return result;
         }
 
-        private String covert(String type, String key, String value) {
-            switch (type) {
+        private String covert(String queryType, String columnType, String key, String value) {
+            // false 不需要转义
+            boolean needEscape = columnType.contains(VARCHAR) || columnType.contains(CHAR)
+                    || columnType.contains(DATETIME) || columnType.contains(TIMESTAMP);
+            switch (queryType) {
                 case FormDataConstant.EQ:
-                    return " = " + ObjectUtils.escapeStr(value);
+                    value = needEscape ? escapeStr(value) : value;
+                    return " = " + value;
                 case FormDataConstant.LIKE:
                     return " like " + "concat('%', #{condition." + key + "}, '%')";
+                case FormDataConstant.NE:
+                    value = needEscape ? escapeStr(value) : value;
+                    return " != " + value;
+                case FormDataConstant.GT:
+                    value = needEscape ? escapeStr(value) : value;
+                    return " &gt; " + value;
+                case FormDataConstant.GTE:
+                    value = needEscape ? escapeStr(value) : value;
+                    return " &gt;= " + value;
+                case FormDataConstant.LT:
+                    value = needEscape ? escapeStr(value) : value;
+                    return " &lt; " + value;
+                case FormDataConstant.LTE:
+                    value = needEscape ? escapeStr(value) : value;
+                    return " &lt;= " + value;
                 default:
-                    throw new IllegalArgumentException("参数非法");
+                    String[] split = value.split(",");
+                    String front = split[0].replace("[", "");
+                    String back =  split[1].replace("]", "");
+                    String max, min;
+                    if (front.compareTo(back) > 0) {
+                        max = back;
+                        min = front;
+                    } else {
+                        max = front;
+                        min = back;
+                    }
+
+                    max = needEscape ? escapeStr(max) : max;
+                    min = needEscape ? escapeStr(min) : min;
+                    return " between " + min + " and " + max;
             }
         }
-
-
     }
 }