| | |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * 入参的list拼接数据 |
| | | * |
| | | * 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,直到不重复,这个递归是针对同一个sqlMap,防止同方法多个查询时存在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的sql语句 |
| | | * @param tableColumn 含别名表字段,例如:user AS u 根据id查询时传递u.id |
| | | * @param dataList 要循环的数值,List集合 |
| | | * @param sqlMap sql语句对应的参数Map |
| | | */ |
| | | 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的sql语句 |
| | | * @param tableColumn 含别名表字段,例如:user AS u 根据id查询时传递u.id |
| | | * @param dataList 要循环的数值,List集合 |
| | | * @param sqlMap sql语句对应的参数Map |
| | | */ |
| | | 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的sql语句 |
| | | * @param tableColumn 含别名表字段,例如:user AS u 根据id查询时传递u.id |
| | | * @param dataList 要循环的数值,String数组 |
| | | * @param sqlMap sql语句对应的参数Map |
| | | */ |
| | | 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的sql语句 |
| | | * @param tableColumn 含别名表字段,例如:user AS u 根据id查询时传递u.id |
| | | * @param dataList 要循环的数值,String数组 |
| | | * @param sqlMap sql语句对应的参数Map |
| | | */ |
| | | 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的sql语句 |
| | | * @param tableColumn 含别名表字段,例如:user AS u 根据id查询时传递u.id |
| | | * @param dataList 要循环的数值,Map数组对象 |
| | | * @param dataListKey Map数组对象中要循环值对应的key |
| | | * @param sqlMap sql语句对应的参数Map |
| | | */ |
| | | 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的sql语句 |
| | | * @param tableColumn 含别名表字段,例如:user AS u 根据id查询时传递u.id |
| | | * @param dataList 要循环的数值,Map数组对象 |
| | | * @param dataListKey Map数组对象中要循环值对应的key |
| | | * @param sqlMap sql语句对应的参数Map |
| | | */ |
| | | 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); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | |