mirror of
https://github.com/moshowgame/SpringBootCodeGenerator.git
synced 2025-12-26 05:48:33 +08:00
Merge pull request #151 from Nisus-Liu/bugfix/143-sqlcamel
fix: 大写下滑下列名转驼峰问题
This commit is contained in:
commit
6d1f29b17c
@ -36,10 +36,11 @@
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>-->
|
||||
|
||||
<!--<dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</dependency>-->
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
||||
@ -14,6 +14,9 @@ public class StringUtils {
|
||||
* @return
|
||||
*/
|
||||
public static String upperCaseFirst(String str) {
|
||||
if (str == null || str.trim().isEmpty()) {
|
||||
return str;
|
||||
}
|
||||
return str.substring(0, 1).toUpperCase() + str.substring(1);
|
||||
}
|
||||
|
||||
@ -89,12 +92,41 @@ public class StringUtils {
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* any str ==> lowerCamel
|
||||
*/
|
||||
public static String toLowerCamel(String str) {
|
||||
if (str == null || str.trim().isEmpty()) {
|
||||
return str;
|
||||
}
|
||||
|
||||
public static boolean isNotNull(String str){
|
||||
return org.apache.commons.lang3.StringUtils.isNotEmpty(str);
|
||||
StringBuilder result = new StringBuilder();
|
||||
char pre = '\0';
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
char ch = str.charAt(i);
|
||||
if (ch == '-' || ch == '—' || ch == '_') {
|
||||
ch = '_';
|
||||
pre = ch;
|
||||
continue;
|
||||
}
|
||||
char ch2 = ch;
|
||||
if (pre == '_') {
|
||||
ch2 = Character.toUpperCase(ch);
|
||||
pre = ch2;
|
||||
} else if (pre >= 'A' && pre <= 'Z') {
|
||||
pre = ch;
|
||||
ch2 = Character.toLowerCase(ch);
|
||||
} else {
|
||||
pre = ch;
|
||||
}
|
||||
result.append(ch2);
|
||||
}
|
||||
|
||||
return lowerCaseFirst(result.toString());
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
|
||||
public static boolean isNotNull(String str) {
|
||||
return org.apache.commons.lang3.StringUtils.isNotEmpty(str);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -164,7 +164,7 @@ public class TableParseUtil {
|
||||
// 2019-2-22 zhengkai 要在条件中使用复杂的表达式
|
||||
// 2019-4-29 zhengkai 优化对普通和特殊storage关键字的判断(感谢@AhHeadFloating的反馈 )
|
||||
// 2020-10-20 zhengkai 优化对fulltext/index关键字的处理(感谢@WEGFan的反馈)
|
||||
// 2023-8-27 zhangfei 改用工具方法判断, 且修改变量名(非特殊标识), 方法抽取
|
||||
// 2023-8-27 L&J 改用工具方法判断, 且修改变量名(非特殊标识), 方法抽取
|
||||
boolean notSpecialFlag = isNotSpecialColumnLine(columnLine, i);
|
||||
|
||||
if (notSpecialFlag) {
|
||||
@ -185,13 +185,11 @@ public class TableParseUtil {
|
||||
|
||||
// field Name
|
||||
// 2019-09-08 yj 添加是否下划线转换为驼峰的判断
|
||||
// 2023-8-27 zhangfei 支持原始列名任意命名风格, 不依赖用户是否输入下划线
|
||||
// 2023-8-27 L&J 支持原始列名任意命名风格, 不依赖用户是否输入下划线
|
||||
String fieldName = null;
|
||||
if (ParamInfo.NAME_CASE_TYPE.CAMEL_CASE.equals(nameCaseType)) {
|
||||
fieldName = StringUtils.lowerCaseFirst(StringUtils.underlineToCamelCase(columnName));
|
||||
if (fieldName.contains("_")) {
|
||||
fieldName = fieldName.replaceAll("_", "");
|
||||
}
|
||||
// 2024-1-27 L&J 适配任意(maybe)原始风格转小写驼峰
|
||||
fieldName = StringUtils.toLowerCamel(columnName);
|
||||
} else if (ParamInfo.NAME_CASE_TYPE.UNDER_SCORE_CASE.equals(nameCaseType)) {
|
||||
fieldName = StringUtils.toUnderline(columnName, false);
|
||||
} else if (ParamInfo.NAME_CASE_TYPE.UPPER_UNDER_SCORE_CASE.equals(nameCaseType)) {
|
||||
|
||||
@ -19,6 +19,8 @@ public class FooTest {
|
||||
System.out.println(StringUtils.toUnderline("UserName",true));
|
||||
System.out.println(StringUtils.toUnderline("user_NameGgg_x-UUU",true));
|
||||
System.out.println(StringUtils.toUnderline("username",true));
|
||||
|
||||
System.out.println(StringUtils.underlineToCamelCase("CREATE_TIME"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,81 @@
|
||||
package com.softdev.system.generator.util;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
public class StringUtilsTest {
|
||||
|
||||
@Test
|
||||
public void toLowerCamel() {
|
||||
System.out.println(StringUtils.toLowerCamel("hello_world"));
|
||||
System.out.println(StringUtils.toLowerCamel("HELLO_WO-RLD-IK"));
|
||||
System.out.println(StringUtils.toLowerCamel("HELLO_WORLD-IKabc"));
|
||||
System.out.println(StringUtils.toLowerCamel("HELLO-WORLD-IKabc"));
|
||||
System.out.println(StringUtils.toLowerCamel("HELLO-123WORLD-IKabc"));
|
||||
System.out.println(StringUtils.toLowerCamel("helloWorldOKla"));
|
||||
assertEquals("helloWorldChina", StringUtils.toLowerCamel("hello_-_world-cHina"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void upperCaseFirstShouldReturnStringWithFirstLetterCapitalized() {
|
||||
assertEquals("Hello", StringUtils.upperCaseFirst("hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void upperCaseFirstShouldReturnEmptyStringWhenInputIsEmpty() {
|
||||
assertEquals("", StringUtils.upperCaseFirst(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lowerCaseFirstShouldReturnStringWithFirstLetterLowercased() {
|
||||
assertEquals("hello", StringUtils.lowerCaseFirst("Hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lowerCaseFirstShouldReturnEmptyStringWhenInputIsEmpty() {
|
||||
assertEquals("", StringUtils.lowerCaseFirst(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void underlineToCamelCaseShouldReturnCamelCaseString() {
|
||||
assertEquals("helloWorld", StringUtils.underlineToCamelCase("hello_world"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void underlineToCamelCaseShouldReturnEmptyStringWhenInputIsEmpty() {
|
||||
assertEquals("", StringUtils.underlineToCamelCase(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toUnderlineShouldReturnUnderlinedString() {
|
||||
assertEquals("hello_world", StringUtils.toUnderline("helloWorld", false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toUnderlineShouldReturnEmptyStringWhenInputIsEmpty() {
|
||||
assertEquals("", StringUtils.toUnderline("", false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toCamelShouldReturnCamelCaseString() {
|
||||
assertEquals("helloWorld", StringUtils.toLowerCamel("hello_world"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toCamelShouldReturnEmptyStringWhenInputIsEmpty() {
|
||||
assertEquals("", StringUtils.toLowerCamel(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotNullShouldReturnTrueWhenStringIsNotEmpty() {
|
||||
assertTrue(StringUtils.isNotNull("hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotNullShouldReturnFalseWhenStringIsEmpty() {
|
||||
assertFalse(StringUtils.isNotNull(""));
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user