mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
feat: 添加 “总是生成 @Column 注解” 选项。
This commit is contained in:
parent
09c1955da8
commit
0c1b59e662
@ -116,6 +116,10 @@ public class EntityConfig implements Serializable {
|
||||
*/
|
||||
private boolean columnCommentEnable;
|
||||
|
||||
/**
|
||||
* 是否总是生成 @Column 注解。
|
||||
*/
|
||||
private boolean alwaysGenColumnAnnotation;
|
||||
|
||||
public String getSourceDir() {
|
||||
return sourceDir;
|
||||
@ -341,6 +345,15 @@ public class EntityConfig implements Serializable {
|
||||
this.columnCommentEnable = columnCommentEnable;
|
||||
}
|
||||
|
||||
public boolean isAlwaysGenColumnAnnotation() {
|
||||
return alwaysGenColumnAnnotation;
|
||||
}
|
||||
|
||||
public EntityConfig setAlwaysGenColumnAnnotation(boolean alwaysGenColumnAnnotation) {
|
||||
this.alwaysGenColumnAnnotation = alwaysGenColumnAnnotation;
|
||||
return this;
|
||||
}
|
||||
|
||||
public enum SwaggerVersion {
|
||||
|
||||
FOX("FOX"),
|
||||
|
||||
@ -65,11 +65,6 @@ public class Column {
|
||||
*/
|
||||
private boolean isAutoIncrement;
|
||||
|
||||
// /**
|
||||
// * 是否需要生成 @Column 注解。
|
||||
// */
|
||||
// private boolean needGenColumnAnnotation = false;
|
||||
|
||||
/**
|
||||
* 数据库的字段类型,比如 varchar/tinyint 等
|
||||
*/
|
||||
@ -210,6 +205,30 @@ public class Column {
|
||||
return StringUtil.firstCharToLowerCase(StringUtil.underlineToCamel(entityJavaFileName));
|
||||
}
|
||||
|
||||
/**
|
||||
* importClass为类的全限定名
|
||||
*/
|
||||
private static void addImportClass(Set<String> importClasses, String importClass) {
|
||||
importClass = importClass.trim();
|
||||
|
||||
// java.util.List<String> >>>>> java.util.List
|
||||
if (importClass.contains("<") && importClass.endsWith(">")) {
|
||||
importClass = importClass.substring(0, importClass.indexOf("<"));
|
||||
}
|
||||
|
||||
// 不包含“.”则认为是原始类型,不需要import
|
||||
// lang 包不需要显式导入
|
||||
if (importClass.contains(".") && !importClass.startsWith("java.lang.")) {
|
||||
importClasses.add(importClass);
|
||||
}
|
||||
}
|
||||
|
||||
private void addComma(StringBuilder annotations, boolean needComma) {
|
||||
if (needComma) {
|
||||
annotations.append(", ");
|
||||
}
|
||||
}
|
||||
|
||||
public String buildAnnotations() {
|
||||
StringBuilder annotations = new StringBuilder();
|
||||
|
||||
@ -241,8 +260,11 @@ public class Column {
|
||||
|
||||
if (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.isNotBlank(comment)) {
|
||||
addComma(annotations, needComma);
|
||||
String comment = this.comment.replace("\n", "").replace("\"", "\\\"").trim();
|
||||
annotations.append("comment = \"" + comment + "\"");
|
||||
annotations.append("comment = \"")
|
||||
.append(this.comment.replace("\n", "")
|
||||
.replace("\"", "\\\"")
|
||||
.trim())
|
||||
.append("\"");
|
||||
}
|
||||
|
||||
if (annotations.length() == 4) {
|
||||
@ -250,11 +272,18 @@ public class Column {
|
||||
} else {
|
||||
annotations.append(")");
|
||||
}
|
||||
|
||||
if (entityConfig != null && entityConfig.isAlwaysGenColumnAnnotation()) {
|
||||
annotations.append("\n\t");
|
||||
}
|
||||
}
|
||||
|
||||
boolean needGenColumnAnnotation = !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))
|
||||
boolean needGenColumnAnnotation = (entityConfig != null && entityConfig.isAlwaysGenColumnAnnotation())
|
||||
|| !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))
|
||||
|| (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.isNotBlank(this.comment) && annotations.length() == 0);
|
||||
|
||||
StringBuilder columnAnnotation = new StringBuilder("@Column(");
|
||||
|
||||
//@Column 注解
|
||||
if (columnConfig.getOnInsertValue() != null
|
||||
|| columnConfig.getOnUpdateValue() != null
|
||||
@ -266,59 +295,73 @@ public class Column {
|
||||
|| columnConfig.getTenantId() != null
|
||||
|| needGenColumnAnnotation
|
||||
) {
|
||||
annotations.append("@Column(");
|
||||
boolean needComma = false;
|
||||
if (!name.equalsIgnoreCase(StringUtil.camelToUnderline(property))) {
|
||||
annotations.append("value = \"").append(name).append("\"");
|
||||
if (entityConfig != null && entityConfig.isAlwaysGenColumnAnnotation()
|
||||
|| !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))) {
|
||||
columnAnnotation.append("value = \"").append(name).append("\"");
|
||||
needComma = true;
|
||||
}
|
||||
|
||||
if (columnConfig.getOnInsertValue() != null) {
|
||||
addComma(annotations, needComma);
|
||||
annotations.append("onInsertValue = \"").append(columnConfig.getOnInsertValue()).append("\"");
|
||||
addComma(columnAnnotation, needComma);
|
||||
columnAnnotation.append("onInsertValue = \"").append(columnConfig.getOnInsertValue()).append("\"");
|
||||
needComma = true;
|
||||
}
|
||||
if (columnConfig.getOnUpdateValue() != null) {
|
||||
addComma(annotations, needComma);
|
||||
annotations.append("onUpdateValue = \"").append(columnConfig.getOnUpdateValue()).append("\"");
|
||||
addComma(columnAnnotation, needComma);
|
||||
columnAnnotation.append("onUpdateValue = \"").append(columnConfig.getOnUpdateValue()).append("\"");
|
||||
needComma = true;
|
||||
}
|
||||
if (columnConfig.getLarge() != null) {
|
||||
addComma(annotations, needComma);
|
||||
annotations.append("isLarge = ").append(columnConfig.getLarge());
|
||||
addComma(columnAnnotation, needComma);
|
||||
columnAnnotation.append("isLarge = ").append(columnConfig.getLarge());
|
||||
needComma = true;
|
||||
}
|
||||
if (columnConfig.getLogicDelete() != null) {
|
||||
addComma(annotations, needComma);
|
||||
annotations.append("isLogicDelete = ").append(columnConfig.getLogicDelete());
|
||||
addComma(columnAnnotation, needComma);
|
||||
columnAnnotation.append("isLogicDelete = ").append(columnConfig.getLogicDelete());
|
||||
needComma = true;
|
||||
}
|
||||
if (columnConfig.getVersion() != null) {
|
||||
addComma(annotations, needComma);
|
||||
annotations.append("version = ").append(columnConfig.getVersion());
|
||||
addComma(columnAnnotation, needComma);
|
||||
columnAnnotation.append("version = ").append(columnConfig.getVersion());
|
||||
needComma = true;
|
||||
}
|
||||
if (columnConfig.getJdbcType() != null) {
|
||||
addComma(annotations, needComma);
|
||||
annotations.append("jdbcType = JdbcType.").append(columnConfig.getJdbcType().name());
|
||||
addComma(columnAnnotation, needComma);
|
||||
columnAnnotation.append("jdbcType = JdbcType.").append(columnConfig.getJdbcType().name());
|
||||
needComma = true;
|
||||
}
|
||||
if (columnConfig.getTypeHandler() != null) {
|
||||
addComma(annotations, needComma);
|
||||
annotations.append("typeHandler = ").append(columnConfig.getTypeHandler().getSimpleName()).append(".class");
|
||||
addComma(columnAnnotation, needComma);
|
||||
columnAnnotation.append("typeHandler = ").append(columnConfig.getTypeHandler().getSimpleName()).append(".class");
|
||||
needComma = true;
|
||||
}
|
||||
if (Boolean.TRUE.equals(columnConfig.getTenantId())) {
|
||||
addComma(annotations, needComma);
|
||||
annotations.append("tenantId = true");
|
||||
addComma(columnAnnotation, needComma);
|
||||
columnAnnotation.append("tenantId = true");
|
||||
needComma = true;
|
||||
}
|
||||
if (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.isNotBlank(comment)) {
|
||||
addComma(annotations, needComma);
|
||||
String comment = this.comment.replace("\n", "").replace("\"", "\\\"").trim();
|
||||
annotations.append("comment = \"" + comment + "\"");
|
||||
addComma(columnAnnotation, needComma);
|
||||
columnAnnotation.append("comment = \"")
|
||||
.append(this.comment.replace("\n", "")
|
||||
.replace("\"", "\\\"")
|
||||
.trim())
|
||||
.append("\"");
|
||||
}
|
||||
annotations.append(")");
|
||||
columnAnnotation.append(")");
|
||||
|
||||
// @Column(value = "user_name") -> @Column("user_name")
|
||||
int index = columnAnnotation.indexOf(",");
|
||||
if (index == -1) {
|
||||
int start = columnAnnotation.indexOf("value");
|
||||
if (start != -1) {
|
||||
columnAnnotation.delete(start, start + 8);
|
||||
}
|
||||
}
|
||||
|
||||
annotations.append(columnAnnotation);
|
||||
}
|
||||
|
||||
//@ColumnMask 注解
|
||||
@ -329,12 +372,6 @@ public class Column {
|
||||
return annotations.toString();
|
||||
}
|
||||
|
||||
private void addComma(StringBuilder annotations, boolean needComma) {
|
||||
if (needComma) {
|
||||
annotations.append(", ");
|
||||
}
|
||||
}
|
||||
|
||||
public Set<String> getImportClasses() {
|
||||
Set<String> importClasses = new LinkedHashSet<>();
|
||||
|
||||
@ -363,7 +400,8 @@ public class Column {
|
||||
addImportClass(importClasses, columnConfig.getTypeHandler().getName());
|
||||
}
|
||||
|
||||
boolean needGenColumnAnnotation = !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))
|
||||
boolean needGenColumnAnnotation = (entityConfig != null && entityConfig.isAlwaysGenColumnAnnotation())
|
||||
|| !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))
|
||||
|| (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.isNotBlank(this.comment));
|
||||
|
||||
if (columnConfig.getOnInsertValue() != null
|
||||
@ -383,24 +421,6 @@ public class Column {
|
||||
return importClasses;
|
||||
}
|
||||
|
||||
/**
|
||||
* importClass为类的全限定名
|
||||
*/
|
||||
private static void addImportClass(Set<String> importClasses, String importClass) {
|
||||
importClass = importClass.trim();
|
||||
|
||||
//java.util.List<String> >>>>> java.util.List
|
||||
if (importClass.contains("<") && importClass.endsWith(">")) {
|
||||
importClass = importClass.substring(0, importClass.indexOf("<"));
|
||||
}
|
||||
|
||||
// 不包含“.”则认为是原始类型,不需要import
|
||||
// lang 包不需要显式导入
|
||||
if (importClass.contains(".") && !importClass.startsWith("java.lang.")) {
|
||||
importClasses.add(importClass);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isDefaultColumn() {
|
||||
if (columnConfig == null) {
|
||||
return true;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user