mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 09:08:24 +08:00
feat: 代码生成器添加生成 BaseEntity 的支持 close #I7JH7K
This commit is contained in:
parent
22625247a2
commit
34a82dda4c
@ -87,6 +87,25 @@ public class EntityConfig implements Serializable {
|
|||||||
*/
|
*/
|
||||||
private int jdkVersion;
|
private int jdkVersion;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当开启这个配置后,Entity 会生成两个类,比如 Account 表会生成 Account.java 以及 AccountBase.java
|
||||||
|
* 这样的好处是,自动生成的 getter setter 字段等都在 Base 类里,而开发者可以在 Account.java 中添加自己的业务代码
|
||||||
|
* 此时,再次生成代码时,不会覆盖掉 Account.java 中的业务代码
|
||||||
|
*/
|
||||||
|
private boolean withBaseClassEnable = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base 类的后缀
|
||||||
|
*/
|
||||||
|
private String withBaseClassSuffix = "Base";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base 类所在的包,默认情况下是在 entity 包下,添加一个 base 文件夹。
|
||||||
|
*/
|
||||||
|
private String withBasePackage;
|
||||||
|
|
||||||
|
|
||||||
public String getSourceDir() {
|
public String getSourceDir() {
|
||||||
return sourceDir;
|
return sourceDir;
|
||||||
}
|
}
|
||||||
@ -262,6 +281,31 @@ public class EntityConfig implements Serializable {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isWithBaseClassEnable() {
|
||||||
|
return withBaseClassEnable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWithBaseClassEnable(boolean withBaseClassEnable) {
|
||||||
|
this.withBaseClassEnable = withBaseClassEnable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getWithBaseClassSuffix() {
|
||||||
|
return withBaseClassSuffix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWithBaseClassSuffix(String withBaseClassSuffix) {
|
||||||
|
this.withBaseClassSuffix = withBaseClassSuffix;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String getWithBasePackage() {
|
||||||
|
return withBasePackage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWithBasePackage(String withBasePackage) {
|
||||||
|
this.withBasePackage = withBasePackage;
|
||||||
|
}
|
||||||
|
|
||||||
public enum SwaggerVersion {
|
public enum SwaggerVersion {
|
||||||
|
|
||||||
FOX("FOX"),
|
FOX("FOX"),
|
||||||
|
|||||||
@ -37,6 +37,9 @@ public class EntityGenerator implements IGenerator {
|
|||||||
|
|
||||||
private String templatePath;
|
private String templatePath;
|
||||||
|
|
||||||
|
private String entityWithBaseTemplatePath = "/templates/enjoy/entityWithBase.tpl";
|
||||||
|
|
||||||
|
|
||||||
public EntityGenerator() {
|
public EntityGenerator() {
|
||||||
this(TemplateConst.ENTITY);
|
this(TemplateConst.ENTITY);
|
||||||
}
|
}
|
||||||
@ -52,15 +55,26 @@ public class EntityGenerator implements IGenerator {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//生成 entity 类
|
||||||
|
genEntityClass(table, globalConfig);
|
||||||
|
|
||||||
|
//生成 base 类
|
||||||
|
genBaseClass(table, globalConfig);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void genEntityClass(Table table, GlobalConfig globalConfig) {
|
||||||
PackageConfig packageConfig = globalConfig.getPackageConfig();
|
PackageConfig packageConfig = globalConfig.getPackageConfig();
|
||||||
EntityConfig entityConfig = globalConfig.getEntityConfig();
|
EntityConfig entityConfig = globalConfig.getEntityConfig();
|
||||||
|
|
||||||
|
|
||||||
String sourceDir = StringUtil.isNotBlank(entityConfig.getSourceDir()) ? entityConfig.getSourceDir() : packageConfig.getSourceDir();
|
String sourceDir = StringUtil.isNotBlank(entityConfig.getSourceDir()) ? entityConfig.getSourceDir() : packageConfig.getSourceDir();
|
||||||
|
|
||||||
String entityPackagePath = packageConfig.getEntityPackage().replace(".", "/");
|
String entityPackagePath = packageConfig.getEntityPackage().replace(".", "/");
|
||||||
File entityJavaFile = new File(sourceDir, entityPackagePath + "/" +
|
String entityClassName = table.buildEntityClassName();
|
||||||
table.buildEntityClassName() + ".java");
|
|
||||||
|
|
||||||
|
File entityJavaFile = new File(sourceDir, entityPackagePath + "/" + entityClassName + ".java");
|
||||||
|
|
||||||
if (entityJavaFile.exists() && !entityConfig.isOverwriteEnable()) {
|
if (entityJavaFile.exists() && !entityConfig.isOverwriteEnable()) {
|
||||||
return;
|
return;
|
||||||
@ -70,15 +84,84 @@ public class EntityGenerator implements IGenerator {
|
|||||||
table.getColumns().removeIf(column -> globalConfig.getStrategyConfig().getIgnoreColumns().contains(column.getName().toLowerCase()));
|
table.getColumns().removeIf(column -> globalConfig.getStrategyConfig().getIgnoreColumns().contains(column.getName().toLowerCase()));
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, Object> params = new HashMap<>(4);
|
Map<String, Object> params = new HashMap<>(6);
|
||||||
params.put("table", table);
|
params.put("table", table);
|
||||||
|
params.put("entityPackageName", packageConfig.getEntityPackage());
|
||||||
|
params.put("entityConfig", entityConfig);
|
||||||
|
params.put("entityClassName", table.buildEntityClassName());
|
||||||
|
params.put("packageConfig", packageConfig);
|
||||||
|
params.put("javadocConfig", globalConfig.getJavadocConfig());
|
||||||
|
|
||||||
|
params.putAll(globalConfig.getCustomConfig());
|
||||||
|
|
||||||
|
String templatePath = this.templatePath;
|
||||||
|
|
||||||
|
//开启生成 baseClass
|
||||||
|
if (entityConfig.isWithBaseClassEnable()) {
|
||||||
|
templatePath = this.entityWithBaseTemplatePath;
|
||||||
|
|
||||||
|
String baseClassName = table.buildEntityClassName() + entityConfig.getWithBaseClassSuffix();
|
||||||
|
params.put("baseClassName", baseClassName);
|
||||||
|
|
||||||
|
String baseClassPackage = StringUtil.isNotBlank(entityConfig.getWithBasePackage())
|
||||||
|
? entityConfig.getWithBasePackage() : packageConfig.getEntityPackage() + ".base";
|
||||||
|
params.put("baseClassPackage", baseClassPackage);
|
||||||
|
|
||||||
|
params.put("entityClassName", table.buildEntityClassName());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, entityJavaFile);
|
||||||
|
|
||||||
|
System.out.println("Entity ---> " + entityJavaFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void genBaseClass(Table table, GlobalConfig globalConfig) {
|
||||||
|
PackageConfig packageConfig = globalConfig.getPackageConfig();
|
||||||
|
EntityConfig entityConfig = globalConfig.getEntityConfig();
|
||||||
|
|
||||||
|
//不需要生成 baseClass
|
||||||
|
if (!entityConfig.isWithBaseClassEnable()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String sourceDir = StringUtil.isNotBlank(entityConfig.getSourceDir()) ? entityConfig.getSourceDir() : packageConfig.getSourceDir();
|
||||||
|
|
||||||
|
String baseEntityPackagePath = packageConfig.getEntityPackage().replace(".", "/");
|
||||||
|
baseEntityPackagePath = StringUtil.isNotBlank(entityConfig.getWithBasePackage()) ? entityConfig.getWithBasePackage().replace(".", "")
|
||||||
|
: baseEntityPackagePath + "/base";
|
||||||
|
|
||||||
|
String baseEntityClassName = table.buildEntityClassName() + entityConfig.getWithBaseClassSuffix();
|
||||||
|
|
||||||
|
File baseEntityJavaFile = new File(sourceDir, baseEntityPackagePath + "/" + baseEntityClassName + ".java");
|
||||||
|
|
||||||
|
|
||||||
|
//排除忽略列
|
||||||
|
if (globalConfig.getStrategyConfig().getIgnoreColumns() != null) {
|
||||||
|
table.getColumns().removeIf(column -> globalConfig.getStrategyConfig().getIgnoreColumns().contains(column.getName().toLowerCase()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, Object> params = new HashMap<>(6);
|
||||||
|
params.put("table", table);
|
||||||
|
params.put("entityPackageName", baseEntityPackagePath.replace("/", "."));
|
||||||
|
params.put("entityClassName", baseEntityClassName);
|
||||||
params.put("entityConfig", entityConfig);
|
params.put("entityConfig", entityConfig);
|
||||||
params.put("packageConfig", packageConfig);
|
params.put("packageConfig", packageConfig);
|
||||||
params.put("javadocConfig", globalConfig.getJavadocConfig());
|
params.put("javadocConfig", globalConfig.getJavadocConfig());
|
||||||
params.putAll(globalConfig.getCustomConfig());
|
params.putAll(globalConfig.getCustomConfig());
|
||||||
globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, entityJavaFile);
|
|
||||||
|
|
||||||
System.out.println("Entity ---> " + entityJavaFile);
|
globalConfig.getTemplateConfig().getTemplate().generate(params, templatePath, baseEntityJavaFile);
|
||||||
|
|
||||||
|
System.out.println("Entity ---> " + baseEntityJavaFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String getEntityWithBaseTemplatePath() {
|
||||||
|
return entityWithBaseTemplatePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEntityWithBaseTemplatePath(String entityWithBaseTemplatePath) {
|
||||||
|
this.entityWithBaseTemplatePath = entityWithBaseTemplatePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -3,8 +3,7 @@
|
|||||||
#set(swaggerVersion = entityConfig.getSwaggerVersion())
|
#set(swaggerVersion = entityConfig.getSwaggerVersion())
|
||||||
#set(withActiveRecord = entityConfig.isWithActiveRecord())
|
#set(withActiveRecord = entityConfig.isWithActiveRecord())
|
||||||
#set(jdkVersion = entityConfig.getJdkVersion())
|
#set(jdkVersion = entityConfig.getJdkVersion())
|
||||||
#set(entityClassName = table.buildEntityClassName())
|
package #(entityPackageName);
|
||||||
package #(packageConfig.entityPackage);
|
|
||||||
|
|
||||||
#for(importClass : table.buildImports())
|
#for(importClass : table.buildImports())
|
||||||
import #(importClass);
|
import #(importClass);
|
||||||
|
|||||||
@ -0,0 +1,6 @@
|
|||||||
|
package #(entityPackageName);
|
||||||
|
|
||||||
|
import #(baseClassPackage).#(baseClassName);
|
||||||
|
|
||||||
|
public class #(entityClassName) extends #(baseClassName) {
|
||||||
|
}
|
||||||
@ -20,14 +20,14 @@ import java.util.Date;
|
|||||||
|
|
||||||
public class BaseEntity {
|
public class BaseEntity {
|
||||||
|
|
||||||
private Date createTime;
|
// private Date createTime;
|
||||||
|
//
|
||||||
public Date getCreateTime() {
|
// public Date getCreateTime() {
|
||||||
return createTime;
|
// return createTime;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
public void setCreateTime(Date createTime) {
|
// public void setCreateTime(Date createTime) {
|
||||||
this.createTime = createTime;
|
// this.createTime = createTime;
|
||||||
}
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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.codegen.test;
|
||||||
|
|
||||||
|
import com.mybatisflex.codegen.Generator;
|
||||||
|
import com.mybatisflex.codegen.config.ColumnConfig;
|
||||||
|
import com.mybatisflex.codegen.config.GlobalConfig;
|
||||||
|
import com.mybatisflex.codegen.config.TableConfig;
|
||||||
|
import com.mybatisflex.codegen.config.TableDefConfig;
|
||||||
|
import com.mybatisflex.codegen.constant.TemplateConst;
|
||||||
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.function.UnaryOperator;
|
||||||
|
|
||||||
|
public class WithBaseGeneratorTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCodeGen1() {
|
||||||
|
//配置数据源
|
||||||
|
HikariDataSource dataSource = new HikariDataSource();
|
||||||
|
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/flex_test?characterEncoding=utf-8");
|
||||||
|
dataSource.setUsername("root");
|
||||||
|
dataSource.setPassword("123456");
|
||||||
|
|
||||||
|
GlobalConfig globalConfig = new GlobalConfig();
|
||||||
|
|
||||||
|
//用户信息表,用于存放用户信息。 -> 用户信息
|
||||||
|
UnaryOperator<String> tableFormat = (e) -> e.split(",")[0].replace("表", "");
|
||||||
|
|
||||||
|
//设置注解生成配置
|
||||||
|
globalConfig.setAuthor("Michael Yang");
|
||||||
|
globalConfig.setTableCommentFormat(tableFormat);
|
||||||
|
|
||||||
|
//设置生成文件目录和根包
|
||||||
|
globalConfig.setSourceDir(System.getProperty("user.dir") + "/src/test/java");
|
||||||
|
globalConfig.setMapperXmlPath(System.getProperty("user.dir") + "/src/test/java/resources/mapper");
|
||||||
|
globalConfig.setBasePackage("com.test");
|
||||||
|
|
||||||
|
//设置表前缀和只生成哪些表
|
||||||
|
globalConfig.setTablePrefix("sys_", "tb_");
|
||||||
|
// globalConfig.setGenerateTable("sys_user","tb_account");
|
||||||
|
// globalConfig.setGenerateTable("tb_account");
|
||||||
|
|
||||||
|
//设置模板路径
|
||||||
|
// globalConfig.setEntityTemplatePath("D:\\Documents\\配置文件\\entity.tpl");
|
||||||
|
|
||||||
|
//配置生成 entity
|
||||||
|
globalConfig.setEntityGenerateEnable(true);
|
||||||
|
// globalConfig.getEntityConfig().setWithBaseClassEnable(true);
|
||||||
|
// globalConfig.setEntityWithLombok(true);
|
||||||
|
globalConfig.setEntitySuperClass(BaseEntity.class);
|
||||||
|
|
||||||
|
//配置生成 mapper
|
||||||
|
globalConfig.setMapperGenerateEnable(true);
|
||||||
|
//配置生成 service
|
||||||
|
globalConfig.setServiceGenerateEnable(true);
|
||||||
|
//配置生成 serviceImpl
|
||||||
|
globalConfig.setServiceImplGenerateEnable(true);
|
||||||
|
//配置生成 controller
|
||||||
|
globalConfig.setControllerGenerateEnable(true);
|
||||||
|
//配置生成 tableDef
|
||||||
|
// globalConfig.setTableDefGenerateEnable(true);
|
||||||
|
// //配置生成 mapperXml
|
||||||
|
// globalConfig.setMapperXmlGenerateEnable(true);
|
||||||
|
// //配置生成 package-info.java
|
||||||
|
// globalConfig.setPackageInfoGenerateEnable(true);
|
||||||
|
|
||||||
|
//通过 datasource 和 globalConfig 创建代码生成器
|
||||||
|
Generator generator = new Generator(dataSource, globalConfig);
|
||||||
|
|
||||||
|
//开始生成代码
|
||||||
|
generator.generate();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user