fix: apt generate code error if the entity class and field have the same name.

This commit is contained in:
开源海哥 2023-09-22 11:55:27 +08:00
parent bc93e99871
commit 0e1467a77c
3 changed files with 64 additions and 3 deletions

View File

@ -232,6 +232,10 @@ public class MybatisFlexProcessor extends AbstractProcessor {
return SourceVersion.latestSupported(); return SourceVersion.latestSupported();
} }
/**
* 通过 classElement 操作起所有字段生成 ColumnInfo 并填充 columnInfos 结合
*/
private void fillColumnInfoList(Set<ColumnInfo> columnInfos, List<String> defaultColumns, TypeElement baseElement, TypeElement classElement, boolean camelToUnderline) { private void fillColumnInfoList(Set<ColumnInfo> columnInfos, List<String> defaultColumns, TypeElement baseElement, TypeElement classElement, boolean camelToUnderline) {
for (Element fieldElement : classElement.getEnclosedElements()) { for (Element fieldElement : classElement.getEnclosedElements()) {

View File

@ -69,6 +69,9 @@ public class ContentBuilder {
content.append("import com.mybatisflex.core.table.TableDef;\n\n"); content.append("import com.mybatisflex.core.table.TableDef;\n\n");
content.append("// Auto generate by mybatis-flex, do not modify it.\n"); content.append("// Auto generate by mybatis-flex, do not modify it.\n");
content.append("public class ").append(tableDefClassName).append(" extends TableDef {\n\n"); content.append("public class ").append(tableDefClassName).append(" extends TableDef {\n\n");
//TableDef 类的属性名称
String tableDefPropertyName = null;
if (!allInTablesEnable) { if (!allInTablesEnable) {
String entityComment = tableInfo.getEntityComment(); String entityComment = tableInfo.getEntityComment();
if (!StrUtil.isBlank(entityComment)) { if (!StrUtil.isBlank(entityComment)) {
@ -76,9 +79,13 @@ public class ContentBuilder {
.append(" * ").append(entityComment.trim()).append("\n") .append(" * ").append(entityComment.trim()).append("\n")
.append(" */\n"); .append(" */\n");
} }
content.append(" public static final ").append(tableDefClassName).append(' ').append(StrUtil.buildFieldName(tableInfo.getEntitySimpleName().concat(tableDefInstanceSuffix != null ? tableDefInstanceSuffix.trim() : ""), tableDefPropertiesNameStyle)) tableDefPropertyName = StrUtil.buildFieldName(tableInfo.getEntitySimpleName().concat(tableDefInstanceSuffix != null ? tableDefInstanceSuffix.trim() : ""), tableDefPropertiesNameStyle);
content.append(" public static final ").append(tableDefClassName).append(' ').append(tableDefPropertyName)
.append(" = new ").append(tableDefClassName).append("();\n\n"); .append(" = new ").append(tableDefClassName).append("();\n\n");
} }
String finalTableDefPropertyName = tableDefPropertyName;
columnInfos.forEach(columnInfo -> { columnInfos.forEach(columnInfo -> {
String comment = columnInfo.getComment(); String comment = columnInfo.getComment();
if (!StrUtil.isBlank(comment)) { if (!StrUtil.isBlank(comment)) {
@ -86,8 +93,16 @@ public class ContentBuilder {
.append(" * ").append(comment.trim()).append("\n") .append(" * ").append(comment.trim()).append("\n")
.append(" */\n"); .append(" */\n");
} }
// QueryColumn 属性定义的名称
String columnPropertyName = StrUtil.buildFieldName(columnInfo.getProperty(), tableDefPropertiesNameStyle);
//当字段名称和表名一样时自动为字段添加一个小尾巴 "_"例如 account_
if (columnPropertyName.equals(finalTableDefPropertyName)) {
columnPropertyName = columnPropertyName + "_";
}
content.append(" public final QueryColumn ") content.append(" public final QueryColumn ")
.append(StrUtil.buildFieldName(columnInfo.getProperty(), tableDefPropertiesNameStyle)) .append(columnPropertyName)
.append(" = new QueryColumn(this, \"") .append(" = new QueryColumn(this, \"")
.append(columnInfo.getColumn()).append("\""); .append(columnInfo.getColumn()).append("\"");
if (columnInfo.getAlias() != null && columnInfo.getAlias().length > 0) { if (columnInfo.getAlias() != null && columnInfo.getAlias().length > 0) {
@ -102,7 +117,11 @@ public class ContentBuilder {
StringJoiner defaultColumnJoiner = new StringJoiner(", "); StringJoiner defaultColumnJoiner = new StringJoiner(", ");
columnInfos.forEach(columnInfo -> { columnInfos.forEach(columnInfo -> {
if (defaultColumns.contains(columnInfo.getColumn())) { if (defaultColumns.contains(columnInfo.getColumn())) {
defaultColumnJoiner.add(StrUtil.buildFieldName(columnInfo.getProperty(), tableDefPropertiesNameStyle)); String columnPropertyName = StrUtil.buildFieldName(columnInfo.getProperty(), tableDefPropertiesNameStyle);
if (columnPropertyName.equals(finalTableDefPropertyName)) {
columnPropertyName = columnPropertyName + "_";
}
defaultColumnJoiner.add(columnPropertyName);
} }
}); });
content.append("\n /**\n") content.append("\n /**\n")

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.test;
import com.mybatisflex.annotation.Table;
import java.io.Serializable;
@Table(value = "tb_account_4")
public class Account4 extends Account2 implements Serializable {
private static final long serialVersionUID = 1L;
//字段名和类名相同
private int account4;
public int getAccount4() {
return account4;
}
public void setAccount4(int account4) {
this.account4 = account4;
}
}