mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 09:08: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;
|
private boolean columnCommentEnable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否总是生成 @Column 注解。
|
||||||
|
*/
|
||||||
|
private boolean alwaysGenColumnAnnotation;
|
||||||
|
|
||||||
public String getSourceDir() {
|
public String getSourceDir() {
|
||||||
return sourceDir;
|
return sourceDir;
|
||||||
@ -341,6 +345,15 @@ public class EntityConfig implements Serializable {
|
|||||||
this.columnCommentEnable = columnCommentEnable;
|
this.columnCommentEnable = columnCommentEnable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isAlwaysGenColumnAnnotation() {
|
||||||
|
return alwaysGenColumnAnnotation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EntityConfig setAlwaysGenColumnAnnotation(boolean alwaysGenColumnAnnotation) {
|
||||||
|
this.alwaysGenColumnAnnotation = alwaysGenColumnAnnotation;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public enum SwaggerVersion {
|
public enum SwaggerVersion {
|
||||||
|
|
||||||
FOX("FOX"),
|
FOX("FOX"),
|
||||||
|
|||||||
@ -65,11 +65,6 @@ public class Column {
|
|||||||
*/
|
*/
|
||||||
private boolean isAutoIncrement;
|
private boolean isAutoIncrement;
|
||||||
|
|
||||||
// /**
|
|
||||||
// * 是否需要生成 @Column 注解。
|
|
||||||
// */
|
|
||||||
// private boolean needGenColumnAnnotation = false;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据库的字段类型,比如 varchar/tinyint 等
|
* 数据库的字段类型,比如 varchar/tinyint 等
|
||||||
*/
|
*/
|
||||||
@ -210,6 +205,30 @@ public class Column {
|
|||||||
return StringUtil.firstCharToLowerCase(StringUtil.underlineToCamel(entityJavaFileName));
|
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() {
|
public String buildAnnotations() {
|
||||||
StringBuilder annotations = new StringBuilder();
|
StringBuilder annotations = new StringBuilder();
|
||||||
|
|
||||||
@ -241,8 +260,11 @@ public class Column {
|
|||||||
|
|
||||||
if (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.isNotBlank(comment)) {
|
if (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.isNotBlank(comment)) {
|
||||||
addComma(annotations, needComma);
|
addComma(annotations, needComma);
|
||||||
String comment = this.comment.replace("\n", "").replace("\"", "\\\"").trim();
|
annotations.append("comment = \"")
|
||||||
annotations.append("comment = \"" + comment + "\"");
|
.append(this.comment.replace("\n", "")
|
||||||
|
.replace("\"", "\\\"")
|
||||||
|
.trim())
|
||||||
|
.append("\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (annotations.length() == 4) {
|
if (annotations.length() == 4) {
|
||||||
@ -250,11 +272,18 @@ public class Column {
|
|||||||
} else {
|
} else {
|
||||||
annotations.append(")");
|
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);
|
|| (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.isNotBlank(this.comment) && annotations.length() == 0);
|
||||||
|
|
||||||
|
StringBuilder columnAnnotation = new StringBuilder("@Column(");
|
||||||
|
|
||||||
//@Column 注解
|
//@Column 注解
|
||||||
if (columnConfig.getOnInsertValue() != null
|
if (columnConfig.getOnInsertValue() != null
|
||||||
|| columnConfig.getOnUpdateValue() != null
|
|| columnConfig.getOnUpdateValue() != null
|
||||||
@ -266,59 +295,73 @@ public class Column {
|
|||||||
|| columnConfig.getTenantId() != null
|
|| columnConfig.getTenantId() != null
|
||||||
|| needGenColumnAnnotation
|
|| needGenColumnAnnotation
|
||||||
) {
|
) {
|
||||||
annotations.append("@Column(");
|
|
||||||
boolean needComma = false;
|
boolean needComma = false;
|
||||||
if (!name.equalsIgnoreCase(StringUtil.camelToUnderline(property))) {
|
if (entityConfig != null && entityConfig.isAlwaysGenColumnAnnotation()
|
||||||
annotations.append("value = \"").append(name).append("\"");
|
|| !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))) {
|
||||||
|
columnAnnotation.append("value = \"").append(name).append("\"");
|
||||||
needComma = true;
|
needComma = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (columnConfig.getOnInsertValue() != null) {
|
if (columnConfig.getOnInsertValue() != null) {
|
||||||
addComma(annotations, needComma);
|
addComma(columnAnnotation, needComma);
|
||||||
annotations.append("onInsertValue = \"").append(columnConfig.getOnInsertValue()).append("\"");
|
columnAnnotation.append("onInsertValue = \"").append(columnConfig.getOnInsertValue()).append("\"");
|
||||||
needComma = true;
|
needComma = true;
|
||||||
}
|
}
|
||||||
if (columnConfig.getOnUpdateValue() != null) {
|
if (columnConfig.getOnUpdateValue() != null) {
|
||||||
addComma(annotations, needComma);
|
addComma(columnAnnotation, needComma);
|
||||||
annotations.append("onUpdateValue = \"").append(columnConfig.getOnUpdateValue()).append("\"");
|
columnAnnotation.append("onUpdateValue = \"").append(columnConfig.getOnUpdateValue()).append("\"");
|
||||||
needComma = true;
|
needComma = true;
|
||||||
}
|
}
|
||||||
if (columnConfig.getLarge() != null) {
|
if (columnConfig.getLarge() != null) {
|
||||||
addComma(annotations, needComma);
|
addComma(columnAnnotation, needComma);
|
||||||
annotations.append("isLarge = ").append(columnConfig.getLarge());
|
columnAnnotation.append("isLarge = ").append(columnConfig.getLarge());
|
||||||
needComma = true;
|
needComma = true;
|
||||||
}
|
}
|
||||||
if (columnConfig.getLogicDelete() != null) {
|
if (columnConfig.getLogicDelete() != null) {
|
||||||
addComma(annotations, needComma);
|
addComma(columnAnnotation, needComma);
|
||||||
annotations.append("isLogicDelete = ").append(columnConfig.getLogicDelete());
|
columnAnnotation.append("isLogicDelete = ").append(columnConfig.getLogicDelete());
|
||||||
needComma = true;
|
needComma = true;
|
||||||
}
|
}
|
||||||
if (columnConfig.getVersion() != null) {
|
if (columnConfig.getVersion() != null) {
|
||||||
addComma(annotations, needComma);
|
addComma(columnAnnotation, needComma);
|
||||||
annotations.append("version = ").append(columnConfig.getVersion());
|
columnAnnotation.append("version = ").append(columnConfig.getVersion());
|
||||||
needComma = true;
|
needComma = true;
|
||||||
}
|
}
|
||||||
if (columnConfig.getJdbcType() != null) {
|
if (columnConfig.getJdbcType() != null) {
|
||||||
addComma(annotations, needComma);
|
addComma(columnAnnotation, needComma);
|
||||||
annotations.append("jdbcType = JdbcType.").append(columnConfig.getJdbcType().name());
|
columnAnnotation.append("jdbcType = JdbcType.").append(columnConfig.getJdbcType().name());
|
||||||
needComma = true;
|
needComma = true;
|
||||||
}
|
}
|
||||||
if (columnConfig.getTypeHandler() != null) {
|
if (columnConfig.getTypeHandler() != null) {
|
||||||
addComma(annotations, needComma);
|
addComma(columnAnnotation, needComma);
|
||||||
annotations.append("typeHandler = ").append(columnConfig.getTypeHandler().getSimpleName()).append(".class");
|
columnAnnotation.append("typeHandler = ").append(columnConfig.getTypeHandler().getSimpleName()).append(".class");
|
||||||
needComma = true;
|
needComma = true;
|
||||||
}
|
}
|
||||||
if (Boolean.TRUE.equals(columnConfig.getTenantId())) {
|
if (Boolean.TRUE.equals(columnConfig.getTenantId())) {
|
||||||
addComma(annotations, needComma);
|
addComma(columnAnnotation, needComma);
|
||||||
annotations.append("tenantId = true");
|
columnAnnotation.append("tenantId = true");
|
||||||
needComma = true;
|
needComma = true;
|
||||||
}
|
}
|
||||||
if (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.isNotBlank(comment)) {
|
if (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.isNotBlank(comment)) {
|
||||||
addComma(annotations, needComma);
|
addComma(columnAnnotation, needComma);
|
||||||
String comment = this.comment.replace("\n", "").replace("\"", "\\\"").trim();
|
columnAnnotation.append("comment = \"")
|
||||||
annotations.append("comment = \"" + 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 注解
|
//@ColumnMask 注解
|
||||||
@ -329,12 +372,6 @@ public class Column {
|
|||||||
return annotations.toString();
|
return annotations.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addComma(StringBuilder annotations, boolean needComma) {
|
|
||||||
if (needComma) {
|
|
||||||
annotations.append(", ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Set<String> getImportClasses() {
|
public Set<String> getImportClasses() {
|
||||||
Set<String> importClasses = new LinkedHashSet<>();
|
Set<String> importClasses = new LinkedHashSet<>();
|
||||||
|
|
||||||
@ -363,7 +400,8 @@ public class Column {
|
|||||||
addImportClass(importClasses, columnConfig.getTypeHandler().getName());
|
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));
|
|| (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.isNotBlank(this.comment));
|
||||||
|
|
||||||
if (columnConfig.getOnInsertValue() != null
|
if (columnConfig.getOnInsertValue() != null
|
||||||
@ -383,24 +421,6 @@ public class Column {
|
|||||||
return importClasses;
|
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() {
|
public boolean isDefaultColumn() {
|
||||||
if (columnConfig == null) {
|
if (columnConfig == null) {
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user