Skip to content

Commit 6d1f29b

Browse files
authored
Merge pull request #151 from Nisus-Liu/bugfix/143-sqlcamel
fix: 大写下滑下列名转驼峰问题
2 parents 412aa51 + 48054f3 commit 6d1f29b

File tree

5 files changed

+125
-11
lines changed

5 files changed

+125
-11
lines changed

generator-web/pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@
3636
<artifactId>spring-boot-starter-data-jpa</artifactId>
3737
</dependency>-->
3838

39-
<!--<dependency>
39+
<dependency>
4040
<groupId>junit</groupId>
4141
<artifactId>junit</artifactId>
42-
</dependency>-->
42+
<scope>test</scope>
43+
</dependency>
4344

4445
<dependency>
4546
<groupId>org.springframework.boot</groupId>

generator-web/src/main/java/com/softdev/system/generator/util/StringUtils.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public class StringUtils {
1414
* @return
1515
*/
1616
public static String upperCaseFirst(String str) {
17+
if (str == null || str.trim().isEmpty()) {
18+
return str;
19+
}
1720
return str.substring(0, 1).toUpperCase() + str.substring(1);
1821
}
1922

@@ -89,12 +92,41 @@ public static String toUnderline(String str, boolean upperCase) {
8992
return result.toString();
9093
}
9194

95+
/**
96+
* any str ==> lowerCamel
97+
*/
98+
public static String toLowerCamel(String str) {
99+
if (str == null || str.trim().isEmpty()) {
100+
return str;
101+
}
92102

93-
public static boolean isNotNull(String str){
94-
return org.apache.commons.lang3.StringUtils.isNotEmpty(str);
103+
StringBuilder result = new StringBuilder();
104+
char pre = '\0';
105+
for (int i = 0; i < str.length(); i++) {
106+
char ch = str.charAt(i);
107+
if (ch == '-' || ch == '—' || ch == '_') {
108+
ch = '_';
109+
pre = ch;
110+
continue;
111+
}
112+
char ch2 = ch;
113+
if (pre == '_') {
114+
ch2 = Character.toUpperCase(ch);
115+
pre = ch2;
116+
} else if (pre >= 'A' && pre <= 'Z') {
117+
pre = ch;
118+
ch2 = Character.toLowerCase(ch);
119+
} else {
120+
pre = ch;
121+
}
122+
result.append(ch2);
123+
}
124+
125+
return lowerCaseFirst(result.toString());
95126
}
96-
public static void main(String[] args) {
97127

128+
public static boolean isNotNull(String str) {
129+
return org.apache.commons.lang3.StringUtils.isNotEmpty(str);
98130
}
99131

100132
}

generator-web/src/main/java/com/softdev/system/generator/util/TableParseUtil.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo)
164164
// 2019-2-22 zhengkai 要在条件中使用复杂的表达式
165165
// 2019-4-29 zhengkai 优化对普通和特殊storage关键字的判断(感谢@AhHeadFloating的反馈 )
166166
// 2020-10-20 zhengkai 优化对fulltext/index关键字的处理(感谢@WEGFan的反馈)
167-
// 2023-8-27 zhangfei 改用工具方法判断, 且修改变量名(非特殊标识), 方法抽取
167+
// 2023-8-27 L&J 改用工具方法判断, 且修改变量名(非特殊标识), 方法抽取
168168
boolean notSpecialFlag = isNotSpecialColumnLine(columnLine, i);
169169

170170
if (notSpecialFlag) {
@@ -185,13 +185,11 @@ public static ClassInfo processTableIntoClassInfo(ParamInfo paramInfo)
185185

186186
// field Name
187187
// 2019-09-08 yj 添加是否下划线转换为驼峰的判断
188-
// 2023-8-27 zhangfei 支持原始列名任意命名风格, 不依赖用户是否输入下划线
188+
// 2023-8-27 L&J 支持原始列名任意命名风格, 不依赖用户是否输入下划线
189189
String fieldName = null;
190190
if (ParamInfo.NAME_CASE_TYPE.CAMEL_CASE.equals(nameCaseType)) {
191-
fieldName = StringUtils.lowerCaseFirst(StringUtils.underlineToCamelCase(columnName));
192-
if (fieldName.contains("_")) {
193-
fieldName = fieldName.replaceAll("_", "");
194-
}
191+
// 2024-1-27 L&J 适配任意(maybe)原始风格转小写驼峰
192+
fieldName = StringUtils.toLowerCamel(columnName);
195193
} else if (ParamInfo.NAME_CASE_TYPE.UNDER_SCORE_CASE.equals(nameCaseType)) {
196194
fieldName = StringUtils.toUnderline(columnName, false);
197195
} else if (ParamInfo.NAME_CASE_TYPE.UPPER_UNDER_SCORE_CASE.equals(nameCaseType)) {

generator-web/src/test/java/FooTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public static void main(String[] args) {
1919
System.out.println(StringUtils.toUnderline("UserName",true));
2020
System.out.println(StringUtils.toUnderline("user_NameGgg_x-UUU",true));
2121
System.out.println(StringUtils.toUnderline("username",true));
22+
23+
System.out.println(StringUtils.underlineToCamelCase("CREATE_TIME"));
2224
}
2325

2426

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.softdev.system.generator.util;
2+
3+
import org.junit.Test;
4+
5+
import static junit.framework.TestCase.assertEquals;
6+
import static junit.framework.TestCase.assertTrue;
7+
import static org.junit.Assert.assertFalse;
8+
9+
public class StringUtilsTest {
10+
11+
@Test
12+
public void toLowerCamel() {
13+
System.out.println(StringUtils.toLowerCamel("hello_world"));
14+
System.out.println(StringUtils.toLowerCamel("HELLO_WO-RLD-IK"));
15+
System.out.println(StringUtils.toLowerCamel("HELLO_WORLD-IKabc"));
16+
System.out.println(StringUtils.toLowerCamel("HELLO-WORLD-IKabc"));
17+
System.out.println(StringUtils.toLowerCamel("HELLO-123WORLD-IKabc"));
18+
System.out.println(StringUtils.toLowerCamel("helloWorldOKla"));
19+
assertEquals("helloWorldChina", StringUtils.toLowerCamel("hello_-_world-cHina"));
20+
}
21+
22+
@Test
23+
public void upperCaseFirstShouldReturnStringWithFirstLetterCapitalized() {
24+
assertEquals("Hello", StringUtils.upperCaseFirst("hello"));
25+
}
26+
27+
@Test
28+
public void upperCaseFirstShouldReturnEmptyStringWhenInputIsEmpty() {
29+
assertEquals("", StringUtils.upperCaseFirst(""));
30+
}
31+
32+
@Test
33+
public void lowerCaseFirstShouldReturnStringWithFirstLetterLowercased() {
34+
assertEquals("hello", StringUtils.lowerCaseFirst("Hello"));
35+
}
36+
37+
@Test
38+
public void lowerCaseFirstShouldReturnEmptyStringWhenInputIsEmpty() {
39+
assertEquals("", StringUtils.lowerCaseFirst(""));
40+
}
41+
42+
@Test
43+
public void underlineToCamelCaseShouldReturnCamelCaseString() {
44+
assertEquals("helloWorld", StringUtils.underlineToCamelCase("hello_world"));
45+
}
46+
47+
@Test
48+
public void underlineToCamelCaseShouldReturnEmptyStringWhenInputIsEmpty() {
49+
assertEquals("", StringUtils.underlineToCamelCase(""));
50+
}
51+
52+
@Test
53+
public void toUnderlineShouldReturnUnderlinedString() {
54+
assertEquals("hello_world", StringUtils.toUnderline("helloWorld", false));
55+
}
56+
57+
@Test
58+
public void toUnderlineShouldReturnEmptyStringWhenInputIsEmpty() {
59+
assertEquals("", StringUtils.toUnderline("", false));
60+
}
61+
62+
@Test
63+
public void toCamelShouldReturnCamelCaseString() {
64+
assertEquals("helloWorld", StringUtils.toLowerCamel("hello_world"));
65+
}
66+
67+
@Test
68+
public void toCamelShouldReturnEmptyStringWhenInputIsEmpty() {
69+
assertEquals("", StringUtils.toLowerCamel(""));
70+
}
71+
72+
@Test
73+
public void isNotNullShouldReturnTrueWhenStringIsNotEmpty() {
74+
assertTrue(StringUtils.isNotNull("hello"));
75+
}
76+
77+
@Test
78+
public void isNotNullShouldReturnFalseWhenStringIsEmpty() {
79+
assertFalse(StringUtils.isNotNull(""));
80+
}
81+
}

0 commit comments

Comments
 (0)