Skip to content

Commit

Permalink
支持toString方法控制(默认lombok开启ToString生成).
Browse files Browse the repository at this point in the history
  • Loading branch information
nieqiurong committed Jan 9, 2025
1 parent c8aa180 commit 52d35a5
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ public List<ClassAnnotationAttributes> handle(TableInfo tableInfo, Entity entity
if (entity.isChain()) {
annotationAttributesList.add(new ClassAnnotationAttributes("@Accessors(chain = true)", "lombok.experimental.Accessors"));
}
if (entity.isLombok() && entity.isDefaultLombok()) {
// 原先lombok默认只有这两个
annotationAttributesList.add(new ClassAnnotationAttributes("@Getter", "lombok.Getter"));
annotationAttributesList.add(new ClassAnnotationAttributes("@Setter", "lombok.Setter"));
if (entity.isLombok()) {
if (entity.isDefaultLombok()) {
// 原先lombok默认只有这两个
annotationAttributesList.add(new ClassAnnotationAttributes("@Getter", "lombok.Getter"));
annotationAttributesList.add(new ClassAnnotationAttributes("@Setter", "lombok.Setter"));
if (entity.isToString()) {
annotationAttributesList.add(new ClassAnnotationAttributes("@ToString", "lombok.ToString"));
}
}
}
}
if (tableInfo.isConvert()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,24 @@ private Entity() {
private boolean generate = true;

/**
* 默认lombok (兼容属性只有Getter和Setter)
* 默认lombok(低版本属性默认只有Getter和Setter)
* <p>当升级至3.5.10后,默认启用@ToString,如果不需要,可通过{@link Builder#toString(boolean)}关闭</p>
*
* @since 3.5.10
*/
@Getter
private boolean defaultLombok = true;

/**
* 是否生成ToString
* <p>默认情况下,非lombok下会自动生成ToString方法,但lombok下没有处理此</p>
* <p>支持控制toString方法是否生成</p>
*
* @since 3.5.10
*/
@Getter
private boolean toString = true;

/**
* 实体类注解
*
Expand Down Expand Up @@ -381,17 +394,17 @@ public Map<String, Object> renderData(@NotNull TableInfo tableInfo) {
data.put("superEntityClass", ClassUtils.getSimpleName(this.superClass));
Set<String> importPackages = new HashSet<>(tableInfo.getImportPackages());
GlobalConfig globalConfig = tableInfo.getGlobalConfig();
List<ClassAnnotationAttributes> classAnnotationAttributes = new ArrayList<>();
List<ClassAnnotationAttributes> classAnnotationAttributes = new ArrayList<>(this.getClassAnnotations());
if (tableAnnotationHandler != null) {
List<ClassAnnotationAttributes> classAnnotationAttributesList = tableAnnotationHandler.handle(tableInfo, this);
if (classAnnotationAttributesList != null && !classAnnotationAttributesList.isEmpty()) {
classAnnotationAttributes = classAnnotationAttributesList;
classAnnotationAttributesList.forEach(attributes -> {
attributes.handleDisplayName(tableInfo);
importPackages.addAll(attributes.getImportPackages());
});
classAnnotationAttributes.addAll(classAnnotationAttributesList);
}
}
classAnnotationAttributes.forEach(attributes -> {
attributes.handleDisplayName(tableInfo);
importPackages.addAll(attributes.getImportPackages());
});
if (tableFieldAnnotationHandler != null) {
tableInfo.getFields().forEach(tableField -> {
List<AnnotationAttributes> annotationAttributes = tableFieldAnnotationHandler.handle(tableInfo, tableField);
Expand All @@ -406,6 +419,7 @@ public Map<String, Object> renderData(@NotNull TableInfo tableInfo) {
data.put("entityClassAnnotations", classAnnotationAttributes.stream()
.sorted(Comparator.comparingInt(s -> s.getDisplayName().length())).collect(Collectors.toList()));
data.put("importEntityPackages", importPackages.stream().sorted().collect(Collectors.toList()));
data.put("entityToString", this.toString);
return data;
}

Expand Down Expand Up @@ -485,6 +499,7 @@ public Builder enableChainModel() {

/**
* 开启lombok模型 (默认添加Getter和Setter)
* <p>自3.5.10开始,默认添加ToString搭配,如果想关闭可通过{@link #toString(boolean)}关闭</p>
*
* @return this
* @since 3.5.0
Expand All @@ -495,7 +510,10 @@ public Builder enableLombok() {
}

/**
* 开启lombok模型 (这里会把注解属性都加入进去,无论是否启用{@link GlobalConfig#isKotlin()})
* 开启lombok模型 (会把注解属性都加入进去,无论是否启用{@link GlobalConfig#isKotlin()})
* <p>注意如果通过此方法开启lombok模型,默认的lombok注解(get,set,toString)都将不会生成,请自行控制添加</p>
* <p>由{@link #toString(boolean)}控制的也会失效</p>
* 使用@Data示例: enableLombok(new ClassAnnotationAttributes("@Data","lombok.Data"))
*
* @param attributes 注解属性集合
* @return this
Expand Down Expand Up @@ -791,6 +809,19 @@ public Builder tableAnnotationHandler(@NotNull ITableAnnotationHandler tableAnno
return this;
}

/**
* 设置是否生成ToString方法
*
* @param toString 是否生成
* @return this
* @since 3.5.10
*/
public Builder toString(boolean toString) {
this.entity.toString = toString;
return this;
}


public Entity get() {
String superClass = this.entity.superClass;
if (StringUtils.isNotBlank(superClass)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public class ${entity} {
<% } %>
}
<% } %>
<% if(!entityLombokModel){ %>
<% if(!entityLombokModel && entityToString){ %>

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public class #(entity) {
#end
}
#end
#if(!entityLombokModel)
#if(!entityLombokModel && entityToString)

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public class ${entity} {
</#if>
}
</#if>
<#if !entityLombokModel>
<#if !entityLombokModel && entityToString>

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public class ${entity} {
#end
}
#end
#if(!${entityLombokModel})
#if(!${entityLombokModel} && ${entityToString})

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class ${entity} {
}

<% } %>
<% if(!entityLombokModel){ %>
<% if(entityToString){ %>
@Override
override fun toString(): String {
return "${entity}{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class #(entity) {
}

#end
#if(entityToString)
override fun toString(): String {
return "#(entity){" +
#for(field : table.fields)
Expand All @@ -79,4 +80,5 @@ class #(entity) {
#end
"}"
}
#end
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class ${entity} {
}

</#if>
<#if entityToString>
override fun toString(): String {
return "${entity}{" +
<#list table.fields as field>
Expand All @@ -79,4 +80,5 @@ class ${entity} {
</#list>
"}"
}
</#if>
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class ${entity} {
}

#end
#if(${entityToString})
override fun toString(): String {
return "${entity}{" +
#foreach($field in ${table.fields})
Expand All @@ -79,4 +80,5 @@ class ${entity} {
#end
"}"
}
#end
}

0 comments on commit 52d35a5

Please sign in to comment.