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