!135 辅助类字段添加注释

Merge pull request !135 from 王帅/main
This commit is contained in:
Michael Yang 2023-07-14 03:38:48 +00:00 committed by Gitee
commit fd2a120d30
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
8 changed files with 234 additions and 58 deletions

View File

@ -13,13 +13,24 @@ import com.mybatisflex.core.table.TableDef;
*/
public class #(tableDefClassName) extends TableDef {
/**
* #(table.getComment())
*/
public static final #(tableDefClassName) #(tableDefConfig.buildFieldName(table.buildEntityClassName() + tableDefConfig.instanceSuffix)) = new #(tableDefClassName)();
#for(column: table.columns)
#(column.buildComment())
public final QueryColumn #(tableDefConfig.buildFieldName(column.property)) = new QueryColumn(this, "#(column.name)");
#end
#end
/**
* 所有字段。
*/
public final QueryColumn #(tableDefConfig.buildFieldName("allColumns")) = new QueryColumn(this, "*");
/**
* 默认字段,不包含逻辑删除或者 large 等字段。
*/
public final QueryColumn[] #(tableDefConfig.buildFieldName("defaultColumns")) = new QueryColumn[]{#for(column: table.columns)#if(column.isDefaultColumn())#(tableDefConfig.buildFieldName(column.property))#if(for.index + 1 != for.size), #end#end#end};
public #(tableDefClassName)() {

View File

@ -23,6 +23,7 @@ import com.mybatisflex.processor.builder.ContentBuilder;
import com.mybatisflex.processor.config.ConfigurationKey;
import com.mybatisflex.processor.config.MybatisFlexConfig;
import com.mybatisflex.processor.entity.ColumnInfo;
import com.mybatisflex.processor.entity.TableInfo;
import com.mybatisflex.processor.util.FileUtil;
import com.mybatisflex.processor.util.StrUtil;
@ -35,6 +36,7 @@ import javax.lang.model.element.*;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.JavaFileObject;
import java.io.File;
@ -60,29 +62,31 @@ import java.util.function.BiConsumer;
public class MybatisFlexProcessor extends AbstractProcessor {
private static final List<String> DEFAULT_SUPPORT_COLUMN_TYPES = Arrays.asList(
int.class.getName(), Integer.class.getName(),
short.class.getName(), Short.class.getName(),
long.class.getName(), Long.class.getName(),
float.class.getName(), Float.class.getName(),
double.class.getName(), Double.class.getName(),
boolean.class.getName(), Boolean.class.getName(),
Date.class.getName(), java.sql.Date.class.getName(), Time.class.getName(), Timestamp.class.getName(),
Instant.class.getName(), LocalDate.class.getName(), LocalDateTime.class.getName(), LocalTime.class.getName(),
OffsetDateTime.class.getName(), OffsetTime.class.getName(), ZonedDateTime.class.getName(),
Year.class.getName(), Month.class.getName(), YearMonth.class.getName(), JapaneseDate.class.getName(),
byte[].class.getName(), Byte[].class.getName(), Byte.class.getName(),
BigInteger.class.getName(), BigDecimal.class.getName(),
char.class.getName(), String.class.getName(), Character.class.getName()
int.class.getName(), Integer.class.getName(),
short.class.getName(), Short.class.getName(),
long.class.getName(), Long.class.getName(),
float.class.getName(), Float.class.getName(),
double.class.getName(), Double.class.getName(),
boolean.class.getName(), Boolean.class.getName(),
Date.class.getName(), java.sql.Date.class.getName(), Time.class.getName(), Timestamp.class.getName(),
Instant.class.getName(), LocalDate.class.getName(), LocalDateTime.class.getName(), LocalTime.class.getName(),
OffsetDateTime.class.getName(), OffsetTime.class.getName(), ZonedDateTime.class.getName(),
Year.class.getName(), Month.class.getName(), YearMonth.class.getName(), JapaneseDate.class.getName(),
byte[].class.getName(), Byte[].class.getName(), Byte.class.getName(),
BigInteger.class.getName(), BigDecimal.class.getName(),
char.class.getName(), String.class.getName(), Character.class.getName()
);
private Filer filer;
private Types typeUtils;
private Elements elementUtils;
private MybatisFlexConfig configuration;
@Override
public synchronized void init(ProcessingEnvironment processingEnvironment) {
super.init(processingEnvironment);
this.filer = processingEnvironment.getFiler();
this.elementUtils = processingEnvironment.getElementUtils();
this.typeUtils = processingEnvironment.getTypeUtils();
this.configuration = new MybatisFlexConfig(filer);
}
@ -174,18 +178,25 @@ public class MybatisFlexProcessor extends AbstractProcessor {
}
}
TableInfo tableInfo = new TableInfo();
tableInfo.setEntityName(entityClass);
tableInfo.setEntitySimpleName(entityClassName);
tableInfo.setTableName(table.value());
tableInfo.setSchema(table.schema());
tableInfo.setEntityComment(elementUtils.getDocComment(entityClassElement));
// 生成 TableDef 文件
String tableDefPackage = StrUtil.buildTableDefPackage(entityClass);
String tableDefClassName = entityClassName.concat(tableDefClassSuffix);
String tableDefContent = ContentBuilder.buildTableDef(table, entityClass, entityClassName, allInTablesEnable, tableDefPackage, tableDefClassName
, tableDefPropertiesNameStyle, tableDefInstanceSuffix, columnInfos, defaultColumns);
String tableDefContent = ContentBuilder.buildTableDef(tableInfo, allInTablesEnable, tableDefPackage, tableDefClassName
, tableDefPropertiesNameStyle, tableDefInstanceSuffix, columnInfos, defaultColumns);
processGenClass(genPath, tableDefPackage, tableDefClassName, tableDefContent);
if (allInTablesEnable) {
// 标记 entity 如果没有配置 Tables 生成位置 entity 位置为准
entityClassReference = entityClass;
// 构建 Tables 常量属性及其导包
ContentBuilder.buildTablesField(importBuilder, fieldBuilder, table, entityClass, entityClassName, tableDefClassSuffix, tableDefPropertiesNameStyle, tableDefInstanceSuffix);
ContentBuilder.buildTablesField(importBuilder, fieldBuilder, tableInfo, tableDefClassSuffix, tableDefPropertiesNameStyle, tableDefInstanceSuffix);
}
// 是否生成 Mapper 文件
@ -267,8 +278,8 @@ public class MybatisFlexProcessor extends AbstractProcessor {
// 未配置 typeHandler 的情况下只支持基本数据类型不支持比如 list set 或者自定义的类等
if ((column == null || "org.apache.ibatis.type.UnknownTypeHandler".equals(typeHandlerClass[0]))
&& !DEFAULT_SUPPORT_COLUMN_TYPES.contains(typeString)
&& (typeElement != null && ElementKind.ENUM != typeElement.getKind())
&& !DEFAULT_SUPPORT_COLUMN_TYPES.contains(typeString)
&& (typeElement != null && ElementKind.ENUM != typeElement.getKind())
) {
continue;
}
@ -298,6 +309,7 @@ public class MybatisFlexProcessor extends AbstractProcessor {
columnInfo.setProperty(property);
columnInfo.setColumn(columnName);
columnInfo.setAlias(alias);
columnInfo.setComment(elementUtils.getDocComment(fieldElement));
columnInfos.add(columnInfo);

View File

@ -16,8 +16,8 @@
package com.mybatisflex.processor.builder;
import com.mybatisflex.annotation.Table;
import com.mybatisflex.processor.entity.ColumnInfo;
import com.mybatisflex.processor.entity.TableInfo;
import com.mybatisflex.processor.util.StrUtil;
import java.util.Collection;
@ -62,7 +62,7 @@ public class ContentBuilder {
/**
* 构建 TableDef 文件内容
*/
public static String buildTableDef(Table table, String entityClass, String entityClassName, boolean allInTablesEnable,
public static String buildTableDef(TableInfo tableInfo, boolean allInTablesEnable,
String tableDefPackage, String tableDefClassName,
String tableDefPropertiesNameStyle, String tableDefInstanceSuffix,
Collection<ColumnInfo> columnInfos, List<String> defaultColumns) {
@ -73,36 +73,54 @@ public class ContentBuilder {
content.append("// Auto generate by mybatis-flex, do not modify it.\n");
content.append("public class ").append(tableDefClassName).append(" extends TableDef {\n\n");
if (!allInTablesEnable) {
content.append(" public static final ").append(tableDefClassName).append(' ').append(StrUtil.buildFieldName(entityClassName.concat(tableDefInstanceSuffix != null ? tableDefInstanceSuffix.trim() : ""), tableDefPropertiesNameStyle))
.append(" = new ").append(tableDefClassName).append("();\n\n");
String entityComment = tableInfo.getEntityComment();
if (!StrUtil.isBlank(entityComment)) {
content.append(" /**\n")
.append(" * ").append(entityComment.trim()).append("\n")
.append(" */\n");
}
content.append(" public static final ").append(tableDefClassName).append(' ').append(StrUtil.buildFieldName(tableInfo.getEntitySimpleName().concat(tableDefInstanceSuffix != null ? tableDefInstanceSuffix.trim() : ""), tableDefPropertiesNameStyle))
.append(" = new ").append(tableDefClassName).append("();\n\n");
}
columnInfos.forEach((columnInfo) -> {
String comment = columnInfo.getComment();
if (!StrUtil.isBlank(comment)) {
content.append(" /**\n")
.append(" * ").append(comment.trim()).append("\n")
.append(" */\n");
}
content.append(" public final QueryColumn ")
.append(StrUtil.buildFieldName(columnInfo.getProperty(), tableDefPropertiesNameStyle))
.append(" = new QueryColumn(this, \"")
.append(columnInfo.getColumn()).append("\"");
.append(StrUtil.buildFieldName(columnInfo.getProperty(), tableDefPropertiesNameStyle))
.append(" = new QueryColumn(this, \"")
.append(columnInfo.getColumn()).append("\"");
if (columnInfo.getAlias() != null && columnInfo.getAlias().length > 0) {
content.append(", \"").append(columnInfo.getAlias()[0]).append("\"");
}
content.append(");\n");
content.append(");\n\n");
});
content.append("\n public final QueryColumn ").append(StrUtil.buildFieldName("allColumns", tableDefPropertiesNameStyle)).append(" = new QueryColumn(this, \"*\");\n");
content.append(" /**\n")
.append(" * 所有字段。\n")
.append(" */\n");
content.append(" public final QueryColumn ").append(StrUtil.buildFieldName("allColumns", tableDefPropertiesNameStyle)).append(" = new QueryColumn(this, \"*\");\n");
StringJoiner defaultColumnJoiner = new StringJoiner(", ");
columnInfos.forEach((columnInfo) -> {
if (defaultColumns.contains(columnInfo.getColumn())) {
defaultColumnJoiner.add(StrUtil.buildFieldName(columnInfo.getProperty(), tableDefPropertiesNameStyle));
}
});
content.append("\n /**\n")
.append(" * 默认字段,不包含逻辑删除或者 large 等字段。\n")
.append(" */\n");
content.append(" public final QueryColumn[] ").append(StrUtil.buildFieldName("defaultColumns", tableDefPropertiesNameStyle)).append(" = new QueryColumn[]{").append(defaultColumnJoiner).append("};\n\n");
String schema = !StrUtil.isBlank(table.schema())
? table.schema()
: "";
String tableName = !StrUtil.isBlank(table.value())
? table.value()
: StrUtil.firstCharToLowerCase(entityClassName);
String schema = !StrUtil.isBlank(tableInfo.getSchema())
? tableInfo.getSchema()
: "";
String tableName = !StrUtil.isBlank(tableInfo.getTableName())
? tableInfo.getTableName()
: StrUtil.firstCharToLowerCase(tableInfo.getEntitySimpleName());
content.append(" public ").append(tableDefClassName).append("() {\n")
.append(" super").append("(\"").append(schema).append("\", \"").append(tableName).append("\");\n")
.append(" }\n\n}\n");
.append(" super").append("(\"").append(schema).append("\", \"").append(tableName).append("\");\n")
.append(" }\n\n}\n");
return content.toString();
}
@ -112,26 +130,32 @@ public class ContentBuilder {
public static String buildTables(StringBuilder importBuilder, StringBuilder fieldBuilder,
String tablesPackage, String tablesClassName) {
return "package " + tablesPackage + ";\n\n" +
importBuilder.toString() +
"\n// Auto generate by mybatis-flex, do not modify it.\n" +
"public class " + tablesClassName + " {\n\n" +
" private " + tablesClassName + "() {\n" +
" }\n\n" +
fieldBuilder.toString() +
"\n}\n";
importBuilder.toString() +
"\n// Auto generate by mybatis-flex, do not modify it.\n" +
"public class " + tablesClassName + " {\n\n" +
" private " + tablesClassName + "() {\n" +
" }\n\n" +
fieldBuilder.toString() +
"\n}\n";
}
/**
* 构建 Tables 文件常量属性
*/
public static void buildTablesField(StringBuilder importBuilder, StringBuilder fieldBuilder, Table table,
String entityClass, String entityClassName, String tableDefClassSuffix, String tableDefPropertiesNameStyle, String tableDefInstanceSuffix) {
String tableDefPackage = StrUtil.buildTableDefPackage(entityClass);
String tableDefClassName = entityClassName.concat(tableDefClassSuffix);
public static void buildTablesField(StringBuilder importBuilder, StringBuilder fieldBuilder, TableInfo tableInfo,
String tableDefClassSuffix, String tableDefPropertiesNameStyle, String tableDefInstanceSuffix) {
String tableDefPackage = StrUtil.buildTableDefPackage(tableInfo.getEntityName());
String tableDefClassName = tableInfo.getEntitySimpleName().concat(tableDefClassSuffix);
importBuilder.append("import ").append(tableDefPackage).append('.').append(tableDefClassName).append(";\n");
String entityComment = tableInfo.getEntityComment();
if (!StrUtil.isBlank(entityComment)) {
fieldBuilder.append(" /**\n")
.append(" * ").append(entityComment).append("\n")
.append(" */\n");
}
fieldBuilder.append(" public static final ").append(tableDefClassName).append(' ')
.append(StrUtil.buildFieldName(entityClassName.concat(tableDefInstanceSuffix != null ? tableDefInstanceSuffix.trim() : ""), tableDefPropertiesNameStyle))
.append(" = new ").append(tableDefClassName).append("();\n");
.append(StrUtil.buildFieldName(tableInfo.getEntitySimpleName().concat(tableDefInstanceSuffix != null ? tableDefInstanceSuffix.trim() : ""), tableDefPropertiesNameStyle))
.append(" = new ").append(tableDefClassName).append("();\n");
}
}

View File

@ -31,6 +31,11 @@ public class ColumnInfo implements Comparable<ColumnInfo> {
*/
private String property;
/**
* 注释
*/
private String comment;
/**
* 列名
*/
@ -49,6 +54,14 @@ public class ColumnInfo implements Comparable<ColumnInfo> {
this.property = property;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getColumn() {
return column;
}

View File

@ -0,0 +1,92 @@
/*
* 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.processor.entity;
/**
* 表详细信息
*
* @author 王帅
* @since 2023-07-13
*/
public class TableInfo {
/**
* 实体类全类名
*/
private String entityName;
/**
* 实体类简单类名
*/
private String entitySimpleName;
/**
* 实体类注释
*/
private String entityComment;
/**
* 表名称
*/
private String tableName;
/**
* Schema 模式
*/
private String schema;
public String getEntityName() {
return entityName;
}
public void setEntityName(String entityName) {
this.entityName = entityName;
}
public String getEntitySimpleName() {
return entitySimpleName;
}
public void setEntitySimpleName(String entitySimpleName) {
this.entitySimpleName = entitySimpleName;
}
public String getEntityComment() {
return entityComment;
}
public void setEntityComment(String entityComment) {
this.entityComment = entityComment;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getSchema() {
return schema;
}
public void setSchema(String schema) {
this.schema = schema;
}
}

View File

@ -20,14 +20,28 @@ import com.mybatisflex.annotation.Table;
import java.util.Date;
/**
* 账户信息
*/
@Table(value = "tb_account", onSet = AccountOnSetListener.class)
public class Account extends BaseEntity<String, Long, String> {
/*@Id(keyType = KeyType.Auto)
private Long id;*/
//private String userName;
/**
* 年龄
*/
private Integer age;
/**
* 生日
*/
private Date birthday;
/**
* 逻辑删除
*/
@Column(isLogicDelete = true)
private Boolean isDelete;
@ -84,12 +98,12 @@ public class Account extends BaseEntity<String, Long, String> {
@Override
public String toString() {
return "Account{" +
"id=" + id +
", userName='" + userName + '\'' +
", age=" + age +
", birthday=" + birthday +
", isDelete=" + isDelete +
"id=" + id +
", userName='" + userName + '\'' +
", age=" + age +
", birthday=" + birthday +
", isDelete=" + isDelete +
// ", roles=" + roles +
'}';
'}';
}
}

View File

@ -26,7 +26,14 @@ import java.util.List;
*/
public class BaseEntity<T, ID, L> extends IdEntity<ID> {
/**
* 用户名
*/
protected T userName;
/**
* 用户角色
*/
@Column(ignore = true)
protected List<L> roles;

View File

@ -27,6 +27,9 @@ import java.io.Serializable;
*/
public class IdEntity<T> implements Serializable {
/**
* 主键
*/
@Id(keyType = KeyType.Auto)
protected T id;