Explorar o código

sql 封装条件

shiqian %!s(int64=4) %!d(string=hai) anos
pai
achega
2c62ecdcea

+ 17 - 26
boman-web-core/src/main/java/com/boman/web/core/mapper/StandardlyMapper.java

@@ -2,6 +2,7 @@ package com.boman.web.core.mapper;
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.boman.web.core.utils.ColumnUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.ibatis.annotations.*;
 import org.apache.ibatis.annotations.Param;
@@ -168,6 +169,7 @@ public interface StandardlyMapper {
      * @param tableName     tableName
      * @param condition     属性名和属性值
      * @param packCondition 属性名和属性值 条件
+     * @param limitOne      true sql加上limit 1,   false 则不加
      * @return java.util.List<com.alibaba.fastjson.JSONObject>
      */
     @SelectProvider(type = SqlProvider.class, method = "getByMap")
@@ -176,6 +178,7 @@ public interface StandardlyMapper {
             , @Param("packCondition") JSONObject packCondition
             , @Param("limitOne") boolean limitOne);
 
+    @SuppressWarnings("unchecked")
     public static class SqlProvider {
         static final String[] READONLY_COLUMNS = new String[]{"OWNERID", "OWNERNAME", "OWNERENAME", "CREATIONDATE", "ID"};
 
@@ -644,50 +647,38 @@ public interface StandardlyMapper {
          * @param queryType  like > < =
          * @param columnType varchar char textarea timestamp
          * @param key        key
-         * @param value      value
+         * @param valueObj   valueObj
          * @return java.lang.String
          */
-        private String covert(String queryType, String columnType, String key, String value) {
+        private String covert(String queryType, String columnType, String key, Object valueObj) {
             // false 不需要转义
-            boolean needEscape = columnType.contains(VARCHAR) || columnType.contains(CHAR)
-                    || columnType.contains(DATETIME) || columnType.contains(TIMESTAMP);
+            boolean needEscape = columnType.contains(VARCHAR) || columnType.contains(CHAR)|| columnType.contains(DATETIME) || columnType.contains(TIMESTAMP);
+            Object value;
             switch (queryType) {
                 case EQ:
-                    value = needEscape ? escapeStr(value) : value;
+                    value = needEscape ? escapeStr((String) valueObj) : valueObj;
                     return " = " + value;
                 case LIKE:
-                    return " like " + "concat('%', #{condition." + key + "}, '%')";
+                    return " like concat('%', #{condition." + key + "}, '%')";
                 case NE:
-                    value = needEscape ? escapeStr(value) : value;
+                    value = needEscape ? escapeStr((String) valueObj) : valueObj;
                     return " != " + value;
                 case GT:
-                    value = needEscape ? escapeStr(value) : value;
+                    value = needEscape ? escapeStr((String) valueObj) : valueObj;
                     return " &gt; " + value;
                 case GTE:
-                    value = needEscape ? escapeStr(value) : value;
+                    value = needEscape ? escapeStr((String) valueObj) : valueObj;
                     return " &gt;= " + value;
                 case LT:
-                    value = needEscape ? escapeStr(value) : value;
+                    value = needEscape ? escapeStr((String) valueObj) : valueObj;
                     return " &lt; " + value;
                 case LTE:
-                    value = needEscape ? escapeStr(value) : value;
+                    value = needEscape ? escapeStr((String) valueObj) : valueObj;
                     return " &lt;= " + value;
                 default:
-                    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;
+                    // in
+                    List<Object> list = ((List<Object>) valueObj);
+                    return  " in (" + ColumnUtils.joinList(list) + ")";
             }
         }
     }