From 03cc06f009f0645faea85297e8660afc1bf82846 Mon Sep 17 00:00:00 2001
From: fwq <582742538@qq.com>
Date: 星期二, 24 九月 2024 11:29:38 +0800
Subject: [PATCH] Merge branch 'master-jiayan' into master-jiayan-test

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

diff --git a/src/main/java/com/hx/mybatisTool/SqlStringTool.java b/src/main/java/com/hx/mybatisTool/SqlStringTool.java
new file mode 100644
index 0000000..1941897
--- /dev/null
+++ b/src/main/java/com/hx/mybatisTool/SqlStringTool.java
@@ -0,0 +1,172 @@
+package com.hx.mybatisTool;
+
+import com.hx.util.StringUtils;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 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 = " ) ";
+
+    /**
+     * 閫掑綊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;
+        }
+        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));
+        }
+        sql.deleteCharAt(sql.length() - 1).append(IN_RIGHT);
+    }
+
+
+    /**
+     * 鎷兼帴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;
+        }
+        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));
+        }
+        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