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