| 8 | import java.util.stream.Collectors; |
| 9 | |
| 10 | public class SqlHump { |
| 11 | public static void main(String[] args) { |
| 12 | String str = "a.id,\n" + |
| 13 | "a.id as order_material_id,\n" + |
| 14 | "k.payment_date as balance_payment_date"; |
| 15 | String[] strings = str.split(","); |
| 16 | List<String> stringList = new ArrayList<>(); |
| 17 | for (String data : strings){ |
| 18 | |
| 19 | if(data.indexOf("a.material_id") >=0){ |
| 20 | // System.out.println(data); |
| 21 | } |
| 22 | if(data.indexOf(" as ")>=0){ |
| 23 | String[] strings1 = data.split("as"); |
| 24 | String j = underlineToHump(strings1[1]); |
| 25 | data = strings1[0] +" as " + j; |
| 26 | |
| 27 | }else { |
| 28 | String j = data.substring(data.indexOf(".")+1); |
| 29 | j = underlineToHump(j); |
| 30 | data = data +" as " + j; |
| 31 | |
| 32 | } |
| 33 | stringList.add(data); |
| 34 | } |
| 35 | |
| 36 | String sql =stringList.stream().collect(Collectors.joining(",")); |
| 37 | System.out.println(sql); |
| 38 | } |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | private static Pattern UNDERLINE_PATTERN = Pattern.compile("_([a-z])"); |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * 根据传入的带下划线的字符串转化为驼峰格式 |
| 48 | * @param str |
| 49 | * @author mrf |
| 50 | * @return |
| 51 | */ |
| 52 | |
| 53 | public static String underlineToHump(String str) { |
| 54 | //正则匹配下划线及后一个字符,删除下划线并将匹配的字符转成大写 |
| 55 | Matcher matcher = UNDERLINE_PATTERN.matcher(str); |
| 56 | StringBuffer sb = new StringBuffer(str); |
| 57 | if (matcher.find()) { |
| 58 | sb = new StringBuffer(); |
| 59 | //将当前匹配的子串替换成指定字符串,并且将替换后的子串及之前到上次匹配的子串之后的字符串添加到StringBuffer对象中 |
| 60 | //正则之前的字符和被替换的字符 |
| 61 | matcher.appendReplacement(sb, matcher.group(1).toUpperCase()); |
| 62 | //把之后的字符串也添加到StringBuffer对象中 |
| 63 | matcher.appendTail(sb); |
| 64 | } else { |
| 65 | //去除除字母之外的前面带的下划线 |
| 66 | return sb.toString().replaceAll("_", ""); |
| 67 | } |
nothing calls this directly
no outgoing calls
no test coverage detected