fixed: 修复数据库字段已下划线 _ 开头或者结尾时,生成的 entity 字段配置不正确的问题

This commit is contained in:
开源海哥 2023-06-14 09:41:35 +08:00
parent d8fa3b19bd
commit 8dfd971214
2 changed files with 32 additions and 4 deletions

View File

@ -35,6 +35,8 @@ public class Column {
private boolean isPrimaryKey = false; private boolean isPrimaryKey = false;
private Boolean isAutoIncrement; private Boolean isAutoIncrement;
private boolean needGenColumnAnnotation = false;
private ColumnConfig columnConfig; private ColumnConfig columnConfig;
@ -45,6 +47,7 @@ public class Column {
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
this.property = buildPropertyName(); this.property = buildPropertyName();
this.needGenColumnAnnotation = !name.equalsIgnoreCase(StringUtil.camelToUnderline(property));
} }
public String getProperty() { public String getProperty() {
@ -104,10 +107,10 @@ public class Column {
} }
public String buildComment(){ public String buildComment() {
if (StringUtil.isBlank(comment)){ if (StringUtil.isBlank(comment)) {
return ""; return "";
}else { } else {
StringBuilder sb = new StringBuilder("/**\n") StringBuilder sb = new StringBuilder("/**\n")
.append(" * ").append(comment).append("\n") .append(" * ").append(comment).append("\n")
.append(" */"); .append(" */");
@ -164,10 +167,17 @@ public class Column {
|| columnConfig.getJdbcType() != null || columnConfig.getJdbcType() != null
|| columnConfig.getTypeHandler() != null || columnConfig.getTypeHandler() != null
|| columnConfig.getTenantId() != null || columnConfig.getTenantId() != null
|| needGenColumnAnnotation
) { ) {
annotations.append("@Column("); annotations.append("@Column(");
boolean needComma = false; boolean needComma = false;
if (needGenColumnAnnotation) {
annotations.append("value = \"" + name + "\"");
needComma = true;
}
if (columnConfig.getOnInsertValue() != null) { if (columnConfig.getOnInsertValue() != null) {
addComma(annotations, needComma);
annotations.append("onInsertValue = \"" + columnConfig.getOnInsertValue() + "\""); annotations.append("onInsertValue = \"" + columnConfig.getOnInsertValue() + "\"");
needComma = true; needComma = true;
} }
@ -265,7 +275,8 @@ public class Column {
|| columnConfig.getVersion() != null || columnConfig.getVersion() != null
|| columnConfig.getJdbcType() != null || columnConfig.getJdbcType() != null
|| columnConfig.getTypeHandler() != null || columnConfig.getTypeHandler() != null
|| Boolean.TRUE.equals(columnConfig.getTenantId()) || Boolean.TRUE.equals(columnConfig.getTenantId()
|| needGenColumnAnnotation)
) { ) {
importClasses.add(com.mybatisflex.annotation.Column.class.getName()); importClasses.add(com.mybatisflex.annotation.Column.class.getName());
} }

View File

@ -0,0 +1,17 @@
package com.mybatisflex.coretest;
import com.mybatisflex.core.util.StringUtil;
import org.junit.Test;
public class StringUtilTest {
@Test
public void underlineToCamel() {
String testString1 = "aa_bb_";
String underlineToCamel = StringUtil.underlineToCamel(testString1);
System.out.println(underlineToCamel);
String underline = StringUtil.camelToUnderline(underlineToCamel);
System.out.println(underline);
}
}