mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
commit
fd2a120d30
@ -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)() {
|
||||
|
||||
@ -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;
|
||||
@ -77,12 +79,14 @@ public class MybatisFlexProcessor extends AbstractProcessor {
|
||||
|
||||
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,10 +178,17 @@ 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
|
||||
String tableDefContent = ContentBuilder.buildTableDef(tableInfo, allInTablesEnable, tableDefPackage, tableDefClassName
|
||||
, tableDefPropertiesNameStyle, tableDefInstanceSuffix, columnInfos, defaultColumns);
|
||||
processGenClass(genPath, tableDefPackage, tableDefClassName, tableDefContent);
|
||||
|
||||
@ -185,7 +196,7 @@ public class MybatisFlexProcessor extends AbstractProcessor {
|
||||
// 标记 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 文件
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -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,10 +73,22 @@ 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))
|
||||
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, \"")
|
||||
@ -84,22 +96,28 @@ public class ContentBuilder {
|
||||
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 schema = !StrUtil.isBlank(tableInfo.getSchema())
|
||||
? tableInfo.getSchema()
|
||||
: "";
|
||||
String tableName = !StrUtil.isBlank(table.value())
|
||||
? table.value()
|
||||
: StrUtil.firstCharToLowerCase(entityClassName);
|
||||
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");
|
||||
@ -124,13 +142,19 @@ public class ContentBuilder {
|
||||
/**
|
||||
* 构建 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(StrUtil.buildFieldName(tableInfo.getEntitySimpleName().concat(tableDefInstanceSuffix != null ? tableDefInstanceSuffix.trim() : ""), tableDefPropertiesNameStyle))
|
||||
.append(" = new ").append(tableDefClassName).append("();\n");
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -27,6 +27,9 @@ import java.io.Serializable;
|
||||
*/
|
||||
public class IdEntity<T> implements Serializable {
|
||||
|
||||
/**
|
||||
* 主键。
|
||||
*/
|
||||
@Id(keyType = KeyType.Auto)
|
||||
protected T id;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user