From 3d11b773dc156e626ba8fde74f1042976a73fe48 Mon Sep 17 00:00:00 2001
From: fwq <582742538@qq.com>
Date: 星期五, 20 九月 2024 17:48:58 +0800
Subject: [PATCH] Merge branch 'master-stander' into master-xlk-test

---
 src/main/java/com/hx/mybatisTool/SqlStringTool.java |  151 +++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 135 insertions(+), 16 deletions(-)

diff --git a/src/main/java/com/hx/mybatisTool/SqlStringTool.java b/src/main/java/com/hx/mybatisTool/SqlStringTool.java
index e66a562..1941897 100644
--- a/src/main/java/com/hx/mybatisTool/SqlStringTool.java
+++ b/src/main/java/com/hx/mybatisTool/SqlStringTool.java
@@ -6,48 +6,167 @@
 import java.util.Map;
 
 /**
- * 鍏ュ弬鐨刲ist鎷兼帴鏁版嵁
- *
+ * Sql宸ュ叿绫�
  * @author fwq
  */
 public class SqlStringTool {
 
+    /**鍩虹key*/
+    private static final String BASE_KEY = "k";
+    /**AND绗﹀彿*/
+    private static final String AND_STR = " AND ";
+    /**宸﹁竟鎷彿鎷兼帴*/
+    private static final String LEFT_STR = "#{m.";
+    /**鍙宠竟鎷彿鎷兼帴*/
+    private static final String RIGHT_STR = "}";
+    /**閫楀彿鍒嗛殧*/
+    private static final String SIGN_STR = ",";
+    /**IN宸︾鍙�*/
+    private static final String IN_LEFT = " IN ( ";
+    /**IN鍙崇鍙�*/
+    private static final String IN_RIGHT = " ) ";
+
     /**
-     * @param sql         sql璇彞
-     * @param tableColumn 鍚埆鍚嶈〃瀛楁
-     * @param dataList    瑕佸惊鐜殑鏁板��
+     * 閫掑綊key,鐩村埌涓嶉噸澶�,杩欎釜閫掑綊鏄拡瀵瑰悓涓�涓猻qlMap,闃叉鍚屾柟娉曞涓煡璇㈡椂瀛樺湪key瑕嗙洊闂銆�
+     * PS:寤鸿鏌ヨ瀹屼笂涓�娆�,娓呯悊sqlMap鍚庡啀浼犻�掕繘鏉�,鍑忓皯閫掑綊鐨勬搷浣�
+     */
+    private static String getKey(String key, Map<String, Object> sqlMap) {
+        if (sqlMap.get(key) != null) {
+            getKey(BASE_KEY + key, sqlMap);
+        }
+        return key;
+    }
+
+    /**
+     * 鎷兼帴sql璇彞
+     * @param sql         StringBuilder鐨剆ql璇彞
+     * @param tableColumn 鍚埆鍚嶈〃瀛楁,渚嬪:user AS u 鏍规嵁id鏌ヨ鏃朵紶閫抲.id
+     * @param dataList    瑕佸惊鐜殑鏁板��,List闆嗗悎
      * @param sqlMap      sql璇彞瀵瑰簲鐨勫弬鏁癕ap
      */
     public static void handleList(StringBuilder sql, String tableColumn, List<String> dataList, Map<String, Object> sqlMap) {
         if (sql == null || StringUtils.isEmpty(tableColumn) || dataList == null || dataList.size() < 1) {
             return;
         }
-        sql.append(" AND ").append(tableColumn).append(" IN ( ");
+        String key = null;
+        sql.append(AND_STR).append(tableColumn).append(IN_LEFT);
         for (int i = 0; i < dataList.size(); i++) {
-            sql.append("#{m.").append("key").append(i).append("}").append(",");
-            sqlMap.put("key" + i, dataList.get(i));
+            key = getKey(BASE_KEY + i, sqlMap);
+            sql.append(LEFT_STR).append(key).append(RIGHT_STR).append(SIGN_STR);
+            sqlMap.put(key, dataList.get(i));
         }
-        sql.deleteCharAt(sql.length() - 1).append(" ) ");
+        sql.deleteCharAt(sql.length() - 1).append(IN_RIGHT);
     }
 
+
     /**
-     * @param sql         sql璇彞
-     * @param tableColumn 鍚埆鍚嶈〃瀛楁
-     * @param dataList    瑕佸惊鐜殑鏁板��
+     * 鎷兼帴sql璇彞
+     * @param sql         StringBuffer鐨剆ql璇彞
+     * @param tableColumn 鍚埆鍚嶈〃瀛楁,渚嬪:user AS u 鏍规嵁id鏌ヨ鏃朵紶閫抲.id
+     * @param dataList    瑕佸惊鐜殑鏁板��,List闆嗗悎
      * @param sqlMap      sql璇彞瀵瑰簲鐨勫弬鏁癕ap
      */
     public static void handleList(StringBuffer sql, String tableColumn, List<String> dataList, Map<String, Object> sqlMap) {
         if (sql == null || StringUtils.isEmpty(tableColumn) || dataList == null || dataList.size() < 1) {
             return;
         }
-        sql.append(" AND ").append(tableColumn).append(" IN ( ");
+        String key = null;
+        sql.append(AND_STR).append(tableColumn).append(IN_LEFT);
         for (int i = 0; i < dataList.size(); i++) {
-            sql.append("#{m.").append("key").append(i).append("}").append(",");
-            sqlMap.put("key" + i, dataList.get(i));
+            key = getKey(BASE_KEY + i, sqlMap);
+            sql.append(LEFT_STR).append(key).append(RIGHT_STR).append(SIGN_STR);
+            sqlMap.put(key, dataList.get(i));
         }
-        sql.deleteCharAt(sql.length() - 1).append(" ) ");
+        sql.deleteCharAt(sql.length() - 1).append(IN_RIGHT);
     }
 
+    /**
+     * 鎷兼帴sql璇彞
+     * @param sql         StringBuilder鐨剆ql璇彞
+     * @param tableColumn 鍚埆鍚嶈〃瀛楁,渚嬪:user AS u 鏍规嵁id鏌ヨ鏃朵紶閫抲.id
+     * @param dataList    瑕佸惊鐜殑鏁板��,String鏁扮粍
+     * @param sqlMap      sql璇彞瀵瑰簲鐨勫弬鏁癕ap
+     */
+    public static void handleList(StringBuilder sql, String tableColumn, String[] dataList, Map<String, Object> sqlMap) {
+        if (sql == null || StringUtils.isEmpty(tableColumn) || dataList == null || dataList.length < 1) {
+            return;
+        }
+        String key = null;
+        sql.append(AND_STR).append(tableColumn).append(IN_LEFT);
+        for (int i = 0; i < dataList.length; i++) {
+            key = getKey(BASE_KEY + i, sqlMap);
+            sql.append(LEFT_STR).append(key).append(RIGHT_STR).append(SIGN_STR);
+            sqlMap.put(key, dataList[i]);
+        }
+        sql.deleteCharAt(sql.length() - 1).append(IN_RIGHT);
+    }
+
+    /**
+     * 鎷兼帴sql璇彞
+     * @param sql         StringBuffer鐨剆ql璇彞
+     * @param tableColumn 鍚埆鍚嶈〃瀛楁,渚嬪:user AS u 鏍规嵁id鏌ヨ鏃朵紶閫抲.id
+     * @param dataList    瑕佸惊鐜殑鏁板��,String鏁扮粍
+     * @param sqlMap      sql璇彞瀵瑰簲鐨勫弬鏁癕ap
+     */
+    public static void handleList(StringBuffer sql, String tableColumn, String[] dataList, Map<String, Object> sqlMap) {
+        if (sql == null || StringUtils.isEmpty(tableColumn) || dataList == null || dataList.length < 1) {
+            return;
+        }
+        String key = null;
+        sql.append(AND_STR).append(tableColumn).append(IN_LEFT);
+        for (int i = 0; i < dataList.length; i++) {
+            key = getKey(BASE_KEY + i, sqlMap);
+            sql.append(LEFT_STR).append(key).append(RIGHT_STR).append(SIGN_STR);
+            sqlMap.put(key, dataList[i]);
+        }
+        sql.deleteCharAt(sql.length() - 1).append(IN_RIGHT);
+    }
+
+    /**
+     * 鎷兼帴sql璇彞
+     * @param sql         StringBuilder鐨剆ql璇彞
+     * @param tableColumn 鍚埆鍚嶈〃瀛楁,渚嬪:user AS u 鏍规嵁id鏌ヨ鏃朵紶閫抲.id
+     * @param dataList    瑕佸惊鐜殑鏁板��,Map鏁扮粍瀵硅薄
+     * @param dataListKey Map鏁扮粍瀵硅薄涓寰幆鍊煎搴旂殑key
+     * @param sqlMap      sql璇彞瀵瑰簲鐨勫弬鏁癕ap
+     */
+    public static void handleList(StringBuilder sql, String tableColumn, List<Map<String, Object>> dataList, String dataListKey, Map<String, Object> sqlMap) {
+        if (sql == null || StringUtils.isEmpty(tableColumn) || dataList == null || dataList.size() < 1) {
+            return;
+        }
+        String key = null;
+        sql.append(AND_STR).append(tableColumn).append(IN_LEFT);
+        for (int i = 0; i < dataList.size(); i++) {
+            key = getKey(BASE_KEY + i, sqlMap);
+            sql.append(LEFT_STR).append(key).append(RIGHT_STR).append(SIGN_STR);
+            sqlMap.put(key, dataList.get(i).get(dataListKey));
+        }
+        sql.deleteCharAt(sql.length() - 1).append(IN_RIGHT);
+    }
+
+    /**
+     * 鎷兼帴sql璇彞
+     * @param sql         StringBuffer鐨剆ql璇彞
+     * @param tableColumn 鍚埆鍚嶈〃瀛楁,渚嬪:user AS u 鏍规嵁id鏌ヨ鏃朵紶閫抲.id
+     * @param dataList    瑕佸惊鐜殑鏁板��,Map鏁扮粍瀵硅薄
+     * @param dataListKey Map鏁扮粍瀵硅薄涓寰幆鍊煎搴旂殑key
+     * @param sqlMap      sql璇彞瀵瑰簲鐨勫弬鏁癕ap
+     */
+    public static void handleList(StringBuffer sql, String tableColumn, List<Map<String, Object>> dataList, String dataListKey, Map<String, Object> sqlMap) {
+        if (sql == null || StringUtils.isEmpty(tableColumn) || dataList == null || dataList.size() < 1) {
+            return;
+        }
+        String key = null;
+        sql.append(AND_STR).append(tableColumn).append(IN_LEFT);
+        for (int i = 0; i < dataList.size(); i++) {
+            key = getKey(BASE_KEY + i, sqlMap);
+            sql.append(LEFT_STR).append(key).append(RIGHT_STR).append(SIGN_STR);
+            sqlMap.put(key, dataList.get(i).get(dataListKey));
+        }
+        sql.deleteCharAt(sql.length() - 1).append(IN_RIGHT);
+    }
+
+
 }
 
 

--
Gitblit v1.8.0