refactor: 重构代码生成器,使配置分项。

This commit is contained in:
Suomm 2023-05-15 19:42:14 +08:00
parent e0648ad471
commit c37e96898e
23 changed files with 875 additions and 764 deletions

View File

@ -16,6 +16,7 @@
package com.mybatisflex.codegen;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.StrategyConfig;
import com.mybatisflex.codegen.dialect.IDialect;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.generator.GeneratorFactory;
@ -83,17 +84,18 @@ public class Generator {
private List<Table> buildTables() throws SQLException {
StrategyConfig strategyConfig = globalConfig.getStrategyConfig();
List<Table> tables = new ArrayList<>();
try (ResultSet rs = getTablesResultSet()) {
while (rs.next()) {
String tableName = rs.getString("TABLE_NAME");
if (!globalConfig.isSupportGenerate(tableName)) {
if (!strategyConfig.isSupportGenerate(tableName)) {
continue;
}
Table table = new Table();
table.setGlobalConfig(globalConfig);
table.setTableConfig(globalConfig.getTableConfig(tableName));
table.setTableConfig(strategyConfig.getTableConfig(tableName));
table.setName(tableName);
@ -113,7 +115,7 @@ public class Generator {
protected ResultSet getTablesResultSet() throws SQLException {
if (globalConfig.isGenerateForView()) {
if (globalConfig.getStrategyConfig().isGenerateForView()) {
return dialect.getTablesResultSet(dbMeta, conn, new String[]{"TABLE", "VIEW"});
} else {
return dialect.getTablesResultSet(dbMeta, conn, new String[]{"TABLE"});

View File

@ -0,0 +1,59 @@
/**
* 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.config;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* 生成 Controller 的配置
*
* @author 王帅
* @since 2023-05-15
*/
@Data
@Accessors(chain = true)
public class ControllerConfig {
/**
* Controller 类的前缀
*/
private String classPrefix = "";
/**
* Controller 类的后缀
*/
private String classSuffix = "Controller";
/**
* 自定义 Controller 的父类
*/
private Class<?> supperClass;
/**
* 生成 REST 风格的 Controller
*/
private boolean restStyle = true;
public String buildSuperClassImport() {
return supperClass.getName();
}
public String buildSuperClassName() {
return supperClass.getSimpleName();
}
}

View File

@ -0,0 +1,58 @@
/**
* 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.config;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* 生成 Entity 的配置
*
* @author 王帅
* @since 2023-05-15
*/
@Data
@Accessors(chain = true)
public class EntityConfig {
/**
* Entity 类的前缀
*/
private String classPrefix = "";
/**
* Entity 类的后缀
*/
private String classSuffix = "";
/**
* Entity 类的父类可以自定义一些 BaseEntity
*/
private Class<?> supperClass;
/**
* Entity 默认实现的接口
*/
private Class<?>[] implInterfaces = {Serializable.class};
/**
* Entity 是否使用 Lombok 注解
*/
private boolean withLombok;
}

View File

@ -15,692 +15,154 @@
*/
package com.mybatisflex.codegen.config;
import com.mybatisflex.codegen.template.EnjoyTemplate;
import com.mybatisflex.codegen.template.ITemplate;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.util.StringUtil;
import com.mybatisflex.spring.service.IService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.Getter;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* 代码生成全局配置类
*
* @author 王帅
* @since 2023-05-15
*/
@Getter
@SuppressWarnings("unused")
public class GlobalConfig {
//代码生成目录
private String sourceDir;
// === 必须配置 ===
//根包名
private String basePackage = "com.mybatisflex";
private final PackageConfig packageConfig;
private final StrategyConfig strategyConfig;
//entity 的包名
private String entityPackage;
// === 可选配置 ===
//entity 类的前缀
private String entityClassPrefix;
private EntityConfig entityConfig;
private MapperConfig mapperConfig;
private ServiceConfig serviceConfig;
private ServiceImplConfig serviceImplConfig;
private ControllerConfig controllerConfig;
private TableDefConfig tableDefConfig;
//entity 类的后缀
private String entityClassSuffix;
// === 其他配置 ===
//entity 类的父类可以自定义一些 BaseEntity
private Class<?> entitySupperClass;
private Map<String, Object> customConfig;
//entity 默认实现的接口
private Class<?>[] entityInterfaces = {Serializable.class};
// === 是否启用生成 ===
//entity 是否使用 Lombok
private boolean entityWithLombok = false;
private boolean entityGenerateEnable;
private boolean mapperGenerateEnable;
private boolean serviceGenerateEnable;
private boolean serviceImplGenerateEnable;
private boolean controllerGenerateEnable;
private boolean tableDefGenerateEnable;
private boolean tableDefGenerateEnable = false;
public GlobalConfig() {
this.packageConfig = new PackageConfig();
this.strategyConfig = new StrategyConfig();
}
//tableDef 的包名
private String tableDefPackage;
//tableDef 类的前缀
private String tableDefClassPrefix;
//tableDef 类的后缀
private String tableDefClassSuffix = "Def";
//是否生成 mapper
private boolean mapperGenerateEnable = false;
//是否覆盖已经存在的 mapper
private boolean mapperOverwriteEnable = false;
//mapper 类的前缀
private String mapperClassPrefix;
//mapper 类的后缀
private String mapperClassSuffix = "Mapper";
//mapper 的包名
private String mapperPackage;
//自定义 mapper 的父类
private Class<?> mapperSupperClass = BaseMapper.class;
//是否生成 service
private boolean serviceGenerateEnable = false;
//是否覆盖已经存在的 service
private boolean serviceOverwriteEnable = false;
//service 类的前缀
private String serviceClassPrefix;
//service 类的后缀
private String serviceClassSuffix = "Service";
//service 的包名
private String servicePackage;
//自定义 service 的父类
private Class<?> serviceSupperClass = IService.class;
//是否生成 serviceImpl
private boolean serviceImplGenerateEnable = false;
//是否覆盖已经存在的 serviceImpl
private boolean serviceImplOverwriteEnable = false;
//serviceImpl 类的前缀
private String serviceImplClassPrefix;
//serviceImpl 类的后缀
private String serviceImplClassSuffix = "ServiceImpl";
//serviceImpl 的包名
private String serviceImplPackage;
//自定义 serviceImpl 的父类
private Class<?> serviceImplSupperClass = ServiceImpl.class;
//是否生成 controller
private boolean controllerGenerateEnable = false;
//是否覆盖已经存在的 controller
private boolean controllerOverwriteEnable = false;
//controller 类的前缀
private String controllerClassPrefix;
//controller 类的后缀
private String controllerClassSuffix = "Controller";
//controller 的包名
private String controllerPackage;
//自定义 controller 的父类
private Class<?> controllerSupperClass;
//rest 风格的 Controller
private boolean restStyleController = true;
//数据库表前缀多个前缀用英文逗号, 隔开
private String tablePrefix;
//逻辑删除的默认字段名称
private String logicDeleteColumn;
//乐观锁的字段名称
private String versionColumn;
//是否生成视图映射
private boolean generateForView = false;
//单独为某张表添加独立的配置
private Map<String, TableConfig> tableConfigMap;
//设置某个列的全局配置
private Map<String, ColumnConfig> defaultColumnConfigMap;
//生成那些表白名单
private Set<String> generateTables;
//不生成那些表黑名单
private Set<String> unGenerateTables;
//使用哪个模板引擎来生成代码
protected ITemplate templateEngine;
//其他自定义配置
private Map<String, Object> others;
public String getSourceDir() {
if (sourceDir == null || sourceDir.trim().length() == 0) {
return System.getProperty("user.dir") + "/src/main/java";
public EntityConfig getEntityConfig() {
if (entityConfig == null) {
entityConfig = new EntityConfig();
}
return sourceDir;
return entityConfig;
}
public void setSourceDir(String sourceDir) {
this.sourceDir = StringUtil.trimOrNull(sourceDir);
}
public String getBasePackage() {
return basePackage;
}
public void setBasePackage(String basePackage) {
this.basePackage = StringUtil.trimOrNull(basePackage);
}
public String getEntityPackage() {
if (StringUtil.isBlank(entityPackage)) {
entityPackage = basePackage + ".entity";
public MapperConfig getMapperConfig() {
if (mapperConfig == null) {
mapperConfig = new MapperConfig();
}
return entityPackage;
return mapperConfig;
}
public void setEntityPackage(String entityPackage) {
this.entityPackage = StringUtil.trimOrNull(entityPackage);
}
public String getEntityClassPrefix() {
if (StringUtil.isBlank(entityClassPrefix)) {
return "";
public ServiceConfig getServiceConfig() {
if (serviceConfig == null) {
serviceConfig = new ServiceConfig();
}
return entityClassPrefix;
return serviceConfig;
}
public void setEntityClassPrefix(String entityClassPrefix) {
this.entityClassPrefix = StringUtil.trimOrNull(entityClassPrefix);
}
public String getEntityClassSuffix() {
if (StringUtil.isBlank(entityClassSuffix)) {
return "";
public ServiceImplConfig getServiceImplConfig() {
if (serviceImplConfig == null) {
serviceImplConfig = new ServiceImplConfig();
}
return entityClassSuffix;
return serviceImplConfig;
}
public void setEntityClassSuffix(String entityClassSuffix) {
this.entityClassSuffix = StringUtil.trimOrNull(entityClassSuffix);
}
public Class<?> getEntitySupperClass() {
return entitySupperClass;
}
public void setEntitySupperClass(Class<?> entitySupperClass) {
this.entitySupperClass = entitySupperClass;
}
public Class<?>[] getEntityInterfaces() {
return entityInterfaces;
}
public void setEntityInterfaces(Class<?>[] entityInterfaces) {
this.entityInterfaces = entityInterfaces;
}
public boolean isEntityWithLombok() {
return entityWithLombok;
}
public void setEntityWithLombok(boolean entityWithLombok) {
this.entityWithLombok = entityWithLombok;
}
public boolean isTableDefGenerateEnable() {
return tableDefGenerateEnable;
}
public void setTableDefGenerateEnable(boolean tableDefGenerateEnable) {
this.tableDefGenerateEnable = tableDefGenerateEnable;
}
public String getTableDefPackage() {
if (StringUtil.isBlank(tableDefPackage)) {
return getEntityPackage() + ".tables";
public ControllerConfig getControllerConfig() {
if (controllerConfig == null) {
controllerConfig = new ControllerConfig();
}
return tableDefPackage;
return controllerConfig;
}
public void setTableDefPackage(String tableDefPackage) {
this.tableDefPackage = StringUtil.trimOrNull(tableDefPackage);
}
public String getTableDefClassPrefix() {
if (StringUtil.isBlank(tableDefClassPrefix)) {
return "";
public TableDefConfig getTableDefConfig() {
if (tableDefConfig == null) {
tableDefConfig = new TableDefConfig();
}
return tableDefClassPrefix;
return tableDefConfig;
}
public void setTableDefClassPrefix(String tableDefClassPrefix) {
this.tableDefClassPrefix = StringUtil.trimOrNull(tableDefClassPrefix);
public EntityConfig enableEntity() {
entityGenerateEnable = true;
return getEntityConfig();
}
public String getTableDefClassSuffix() {
return tableDefClassSuffix;
public MapperConfig enableMapper() {
mapperGenerateEnable = true;
return getMapperConfig();
}
public void setTableDefClassSuffix(String tableDefClassSuffix) {
this.tableDefClassSuffix = StringUtil.trimOrNull(tableDefClassSuffix);
public ServiceConfig enableService() {
serviceGenerateEnable = true;
return getServiceConfig();
}
public boolean isMapperGenerateEnable() {
return mapperGenerateEnable;
public ServiceImplConfig enableServiceImpl() {
serviceImplGenerateEnable = true;
return getServiceImplConfig();
}
public void setMapperGenerateEnable(boolean mapperGenerateEnable) {
this.mapperGenerateEnable = mapperGenerateEnable;
public ControllerConfig enableController() {
controllerGenerateEnable = true;
return getControllerConfig();
}
public boolean isMapperOverwriteEnable() {
return mapperOverwriteEnable;
public TableDefConfig enableTableDef() {
tableDefGenerateEnable = true;
return getTableDefConfig();
}
public void setMapperOverwriteEnable(boolean mapperOverwriteEnable) {
this.mapperOverwriteEnable = mapperOverwriteEnable;
public void disableEntity() {
entityGenerateEnable = false;
}
public String getMapperClassPrefix() {
if (StringUtil.isBlank(mapperClassPrefix)) {
return "";
public void disableMapper() {
mapperGenerateEnable = false;
}
public void disableService() {
serviceGenerateEnable = false;
}
public void disableServiceImpl() {
serviceImplGenerateEnable = false;
}
public void disableController() {
controllerGenerateEnable = false;
}
public void disableTableDef() {
tableDefGenerateEnable = false;
}
public void addCustomConfig(String key, Object value) {
if (customConfig == null) {
customConfig = new HashMap<>();
}
return mapperClassPrefix;
customConfig.put(key, value);
}
public void setMapperClassPrefix(String mapperClassPrefix) {
this.mapperClassPrefix = StringUtil.trimOrNull(mapperClassPrefix);
}
public String getMapperClassSuffix() {
return mapperClassSuffix;
}
public void setMapperClassSuffix(String mapperClassSuffix) {
this.mapperClassSuffix = StringUtil.trimOrNull(mapperClassSuffix);
}
public String getMapperPackage() {
if (StringUtil.isBlank(mapperPackage)) {
mapperPackage = basePackage + ".mapper";
}
return mapperPackage;
}
public void setMapperPackage(String mapperPackage) {
this.mapperPackage = StringUtil.trimOrNull(mapperPackage);
}
public Class<?> getMapperSupperClass() {
return mapperSupperClass;
}
public void setMapperSupperClass(Class<?> mapperSupperClass) {
this.mapperSupperClass = mapperSupperClass;
}
public boolean isServiceGenerateEnable() {
return serviceGenerateEnable;
}
public void setServiceGenerateEnable(boolean serviceGenerateEnable) {
this.serviceGenerateEnable = serviceGenerateEnable;
}
public boolean isServiceOverwriteEnable() {
return serviceOverwriteEnable;
}
public void setServiceOverwriteEnable(boolean serviceOverwriteEnable) {
this.serviceOverwriteEnable = serviceOverwriteEnable;
}
public String getServiceClassPrefix() {
if (StringUtil.isBlank(serviceClassPrefix)) {
return "";
}
return serviceClassPrefix;
}
public void setServiceClassPrefix(String serviceClassPrefix) {
this.serviceClassPrefix = StringUtil.trimOrNull(serviceClassPrefix);
}
public String getServiceClassSuffix() {
return serviceClassSuffix;
}
public void setServiceClassSuffix(String serviceClassSuffix) {
this.serviceClassSuffix = StringUtil.trimOrNull(serviceClassSuffix);
}
public String getServicePackage() {
if (StringUtil.isBlank(servicePackage)) {
servicePackage = basePackage + ".service";
}
return servicePackage;
}
public void setServicePackage(String servicePackage) {
this.servicePackage = StringUtil.trimOrNull(servicePackage);
}
public Class<?> getServiceSupperClass() {
return serviceSupperClass;
}
public void setServiceSupperClass(Class<?> serviceSupperClass) {
this.serviceSupperClass = serviceSupperClass;
}
public boolean isServiceImplGenerateEnable() {
return serviceImplGenerateEnable;
}
public void setServiceImplGenerateEnable(boolean serviceImplGenerateEnable) {
this.serviceImplGenerateEnable = serviceImplGenerateEnable;
}
public boolean isServiceImplOverwriteEnable() {
return serviceImplOverwriteEnable;
}
public void setServiceImplOverwriteEnable(boolean serviceImplOverwriteEnable) {
this.serviceImplOverwriteEnable = serviceImplOverwriteEnable;
}
public String getServiceImplClassPrefix() {
if (StringUtil.isBlank(serviceImplClassPrefix)) {
return "";
}
return serviceImplClassPrefix;
}
public void setServiceImplClassPrefix(String serviceImplClassPrefix) {
this.serviceImplClassPrefix = StringUtil.trimOrNull(serviceImplClassPrefix);
}
public String getServiceImplClassSuffix() {
return serviceImplClassSuffix;
}
public void setServiceImplClassSuffix(String serviceImplClassSuffix) {
this.serviceImplClassSuffix = StringUtil.trimOrNull(serviceImplClassSuffix);
}
public String getServiceImplPackage() {
if (StringUtil.isBlank(serviceImplPackage)) {
serviceImplPackage = basePackage + ".service.impl";
}
return serviceImplPackage;
}
public void setServiceImplPackage(String serviceImplPackage) {
this.serviceImplPackage = StringUtil.trimOrNull(serviceImplPackage);
}
public Class<?> getServiceImplSupperClass() {
return serviceImplSupperClass;
}
public void setServiceImplSupperClass(Class<?> serviceImplSupperClass) {
this.serviceImplSupperClass = serviceImplSupperClass;
}
public boolean isControllerGenerateEnable() {
return controllerGenerateEnable;
}
public void setControllerGenerateEnable(boolean controllerGenerateEnable) {
this.controllerGenerateEnable = controllerGenerateEnable;
}
public boolean isControllerOverwriteEnable() {
return controllerOverwriteEnable;
}
public void setControllerOverwriteEnable(boolean controllerOverwriteEnable) {
this.controllerOverwriteEnable = controllerOverwriteEnable;
}
public String getControllerClassPrefix() {
if (StringUtil.isBlank(controllerClassPrefix)) {
return "";
}
return controllerClassPrefix;
}
public void setControllerClassPrefix(String controllerClassPrefix) {
this.controllerClassPrefix = StringUtil.trimOrNull(controllerClassPrefix);
}
public String getControllerClassSuffix() {
return controllerClassSuffix;
}
public void setControllerClassSuffix(String controllerClassSuffix) {
this.controllerClassSuffix = StringUtil.trimOrNull(controllerClassSuffix);
}
public String getControllerPackage() {
if (StringUtil.isBlank(controllerPackage)) {
controllerPackage = basePackage + ".controller";
}
return controllerPackage;
}
public void setControllerPackage(String controllerPackage) {
this.controllerPackage = StringUtil.trimOrNull(controllerPackage);
}
public Class<?> getControllerSupperClass() {
return controllerSupperClass;
}
public void setControllerSupperClass(Class<?> controllerSupperClass) {
this.controllerSupperClass = controllerSupperClass;
}
public boolean isRestStyleController() {
return restStyleController;
}
public void setRestStyleController(boolean restStyleController) {
this.restStyleController = restStyleController;
}
public String getTablePrefix() {
return tablePrefix;
}
public void setTablePrefix(String tablePrefix) {
this.tablePrefix = StringUtil.trimOrNull(tablePrefix);
}
public String getLogicDeleteColumn() {
return logicDeleteColumn;
}
public void setLogicDeleteColumn(String logicDeleteColumn) {
this.logicDeleteColumn = StringUtil.trimOrNull(logicDeleteColumn);
}
public String getVersionColumn() {
return versionColumn;
}
public void setVersionColumn(String versionColumn) {
this.versionColumn = StringUtil.trimOrNull(versionColumn);
}
public Map<String, TableConfig> getTableConfigMap() {
return tableConfigMap;
}
public void setTableConfigMap(Map<String, TableConfig> tableConfigMap) {
this.tableConfigMap = tableConfigMap;
}
public void addTableConfig(TableConfig tableConfig) {
if (tableConfigMap == null) {
tableConfigMap = new HashMap<>();
}
tableConfigMap.put(tableConfig.getTableName(), tableConfig);
}
public TableConfig getTableConfig(String tableName) {
return tableConfigMap == null ? null : tableConfigMap.get(tableName);
}
public Map<String, ColumnConfig> getDefaultColumnConfigMap() {
return defaultColumnConfigMap;
}
public void setDefaultColumnConfigMap(Map<String, ColumnConfig> defaultColumnConfigMap) {
this.defaultColumnConfigMap = defaultColumnConfigMap;
}
public void addColumnConfig(ColumnConfig columnConfig) {
if (defaultColumnConfigMap == null) {
defaultColumnConfigMap = new HashMap<>();
}
defaultColumnConfigMap.put(columnConfig.getColumnName(), columnConfig);
}
public void addColumnConfig(String tableName, ColumnConfig columnConfig) {
TableConfig tableConfig = getTableConfig(tableName);
if (tableConfig == null) {
tableConfig = new TableConfig();
tableConfig.setTableName(tableName);
addTableConfig(tableConfig);
}
tableConfig.addColumnConfig(columnConfig);
}
public ColumnConfig getColumnConfig(String tableName, String columnName) {
ColumnConfig columnConfig = null;
TableConfig tableConfig = getTableConfig(tableName);
if (tableConfig != null) {
columnConfig = tableConfig.getColumnConfig(columnName);
}
if (columnConfig == null && defaultColumnConfigMap != null) {
columnConfig = defaultColumnConfigMap.get(columnName);
}
if (columnConfig == null) {
columnConfig = new ColumnConfig();
}
//全局配置的逻辑删除
if (columnName.equals(logicDeleteColumn) && columnConfig.getLogicDelete() == null) {
columnConfig.setLogicDelete(true);
}
//全部配置的乐观锁版本
if (columnName.equals(versionColumn) && columnConfig.getVersion() == null) {
columnConfig.setVersion(true);
}
return columnConfig;
}
public boolean isGenerateForView() {
return generateForView;
}
public void setGenerateForView(boolean generateForView) {
this.generateForView = generateForView;
}
public Set<String> getGenerateTables() {
return generateTables;
}
public void setGenerateTables(Set<String> generateTables) {
this.generateTables = generateTables;
}
public void addGenerateTable(String... tables) {
if (generateTables == null) {
generateTables = new HashSet<>();
}
for (String table : tables) {
if (table != null && table.trim().length() > 0) {
generateTables.add(table.trim());
}
}
}
public Set<String> getUnGenerateTables() {
return unGenerateTables;
}
public void setUnGenerateTables(Set<String> unGenerateTables) {
this.unGenerateTables = unGenerateTables;
}
public void addUnGenerateTable(String... tables) {
if (unGenerateTables == null) {
unGenerateTables = new HashSet<>();
}
for (String table : tables) {
if (table != null && table.trim().length() > 0) {
unGenerateTables.add(table.trim());
}
}
}
public boolean isSupportGenerate(String table) {
if (unGenerateTables != null && unGenerateTables.contains(table)) {
return false;
}
//不配置指定比表名的情况下支持所有表
if (generateTables == null || generateTables.isEmpty()) {
return true;
}
for (String generateTable : generateTables) {
if (generateTable.equals(table)) {
return true;
}
}
return false;
}
public ITemplate getTemplateEngine() {
if (templateEngine == null) {
templateEngine = new EnjoyTemplate();
}
return templateEngine;
}
public void setTemplateEngine(ITemplate templateEngine) {
this.templateEngine = templateEngine;
}
public Map<String, Object> getOthers() {
return others;
}
public void setOthers(Map<String, Object> others) {
this.others = others;
}
public void addConfig(String key, Object value) {
if (others == null) {
others = new HashMap<>();
}
others.put(key, value);
}
}

View File

@ -0,0 +1,55 @@
/**
* 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.config;
import com.mybatisflex.core.BaseMapper;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* 生成 Mapper 的配置
*
* @author 王帅
* @since 2023-05-15
*/
@Data
@Accessors(chain = true)
public class MapperConfig {
/**
* Mapper 类的前缀
*/
private String classPrefix = "";
/**
* Mapper 类的后缀
*/
private String classSuffix = "Mapper";
/**
* 自定义 Mapper 的父类
*/
private Class<?> supperClass = BaseMapper.class;
public String buildSuperClassImport() {
return supperClass.getName();
}
public String buildSuperClassName() {
return supperClass.getSimpleName();
}
}

View File

@ -0,0 +1,106 @@
package com.mybatisflex.codegen.config;
import com.mybatisflex.core.util.StringUtil;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* 生成软件包的配置
*
* @author 王帅
* @since 2023-05-15
*/
@Data
@Accessors(chain = true)
public class PackageConfig {
/**
* 代码生成目录
*/
private String sourceDir;
/**
* 根包
*/
private String basePackage = "com.mybatisflex";
/**
* Entity 所在包
*/
private String entityPackage;
/**
* Mapper 所在包
*/
private String mapperPackage;
/**
* Service 所在包
*/
private String servicePackage;
/**
* ServiceImpl 所在包
*/
private String serviceImplPackage;
/**
* Controller 所在包
*/
private String controllerPackage;
/**
* TableDef 所在包
*/
private String tableDefPackage;
public String getSourceDir() {
if (sourceDir == null || StringUtil.isBlank(sourceDir)) {
return System.getProperty("user.dir") + "/src/main/java";
}
return sourceDir;
}
public String getEntityPackage() {
if (StringUtil.isBlank(entityPackage)) {
return basePackage.concat(".entity");
}
return entityPackage;
}
public String getMapperPackage() {
if (StringUtil.isBlank(mapperPackage)) {
return basePackage.concat(".mapper");
}
return mapperPackage;
}
public String getServicePackage() {
if (StringUtil.isBlank(servicePackage)) {
return basePackage.concat(".service");
}
return servicePackage;
}
public String getServiceImplPackage() {
if (StringUtil.isBlank(serviceImplPackage)) {
return basePackage.concat(".service.impl");
}
return serviceImplPackage;
}
public String getControllerPackage() {
if (StringUtil.isBlank(controllerPackage)) {
return basePackage.concat(".controller");
}
return controllerPackage;
}
public String getTableDefPackage() {
if (StringUtil.isBlank(tableDefPackage)) {
return getEntityPackage().concat(".tables");
}
return tableDefPackage;
}
}

View File

@ -0,0 +1,55 @@
/**
* 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.config;
import com.mybatisflex.spring.service.IService;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* 生成 Service 的配置
*
* @author 王帅
* @since 2023-05-15
*/
@Data
@Accessors(chain = true)
public class ServiceConfig {
/**
* Service 类的前缀
*/
private String classPrefix = "";
/**
* Service 类的后缀
*/
private String classSuffix = "Service";
/**
* 自定义 Service 的父类
*/
private Class<?> supperClass = IService.class;
public String buildSuperClassImport() {
return supperClass.getName();
}
public String buildSuperClassName() {
return supperClass.getSimpleName();
}
}

View File

@ -0,0 +1,55 @@
/**
* 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.config;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* 生成 ServiceImpl 的配置
*
* @author 王帅
* @since 2023-05-15
*/
@Data
@Accessors(chain = true)
public class ServiceImplConfig {
/**
* ServiceImpl 类的前缀
*/
private String classPrefix = "";
/**
* ServiceImpl 类的后缀
*/
private String classSuffix = "ServiceImpl";
/**
* 自定义 ServiceImpl 的父类
*/
private Class<?> supperClass = ServiceImpl.class;
public String buildSuperClassImport() {
return supperClass.getName();
}
public String buildSuperClassName() {
return supperClass.getSimpleName();
}
}

View File

@ -0,0 +1,173 @@
package com.mybatisflex.codegen.config;
import com.mybatisflex.codegen.template.EnjoyTemplate;
import com.mybatisflex.codegen.template.ITemplate;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* 表策略配置
*
* @author 王帅
* @since 2023-05-14
*/
@Data
@Accessors(chain = true)
public class StrategyConfig {
/**
* 使用哪个模板引擎来生成代码
*/
protected ITemplate templateEngine;
/**
* 数据库表前缀多个前缀用英文逗号, 隔开
*/
private String tablePrefix;
/**
* 逻辑删除的默认字段名称
*/
private String logicDeleteColumn;
/**
* 乐观锁的字段名称
*/
private String versionColumn;
/**
* 是否生成视图映射
*/
private boolean generateForView;
/**
* 是否覆盖之前生成的文件
*/
private boolean overwriteEnable;
/**
* 单独为某张表添加独立的配置
*/
private Map<String, TableConfig> tableConfigMap;
/**
* 设置某个列的全局配置
*/
private Map<String, ColumnConfig> columnConfigMap;
/**
* 生成那些表白名单
*/
private Set<String> generateTables;
/**
* 不生成那些表黑名单
*/
private Set<String> unGenerateTables;
public void addTableConfig(TableConfig tableConfig) {
if (tableConfigMap == null) {
tableConfigMap = new HashMap<>();
}
tableConfigMap.put(tableConfig.getTableName(), tableConfig);
}
public TableConfig getTableConfig(String tableName) {
return tableConfigMap == null ? null : tableConfigMap.get(tableName);
}
public void addColumnConfig(ColumnConfig columnConfig) {
if (columnConfigMap == null) {
columnConfigMap = new HashMap<>();
}
columnConfigMap.put(columnConfig.getColumnName(), columnConfig);
}
public void addColumnConfig(String tableName, ColumnConfig columnConfig) {
TableConfig tableConfig = getTableConfig(tableName);
if (tableConfig == null) {
tableConfig = new TableConfig();
tableConfig.setTableName(tableName);
addTableConfig(tableConfig);
}
tableConfig.addColumnConfig(columnConfig);
}
public ColumnConfig getColumnConfig(String tableName, String columnName) {
ColumnConfig columnConfig = null;
TableConfig tableConfig = getTableConfig(tableName);
if (tableConfig != null) {
columnConfig = tableConfig.getColumnConfig(columnName);
}
if (columnConfig == null && columnConfigMap != null) {
columnConfig = columnConfigMap.get(columnName);
}
if (columnConfig == null) {
columnConfig = new ColumnConfig();
}
//全局配置的逻辑删除
if (columnName.equals(logicDeleteColumn) && columnConfig.getLogicDelete() == null) {
columnConfig.setLogicDelete(true);
}
//全部配置的乐观锁版本
if (columnName.equals(versionColumn) && columnConfig.getVersion() == null) {
columnConfig.setVersion(true);
}
return columnConfig;
}
public void addGenerateTable(String... tables) {
if (generateTables == null) {
generateTables = new HashSet<>();
}
for (String table : tables) {
if (table != null && table.trim().length() > 0) {
generateTables.add(table.trim());
}
}
}
public void addUnGenerateTable(String... tables) {
if (unGenerateTables == null) {
unGenerateTables = new HashSet<>();
}
for (String table : tables) {
if (table != null && table.trim().length() > 0) {
unGenerateTables.add(table.trim());
}
}
}
public boolean isSupportGenerate(String table) {
if (unGenerateTables != null && unGenerateTables.contains(table)) {
return false;
}
//不配置指定比表名的情况下支持所有表
if (generateTables == null || generateTables.isEmpty()) {
return true;
}
for (String generateTable : generateTables) {
if (generateTable.equals(table)) {
return true;
}
}
return false;
}
public ITemplate getTemplateEngine() {
if (templateEngine == null) {
templateEngine = new EnjoyTemplate();
}
return templateEngine;
}
}

View File

@ -0,0 +1,41 @@
/**
* 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.config;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* 生成 TableDef 的配置
*
* @author 王帅
* @since 2023-05-15
*/
@Data
@Accessors(chain = true)
public class TableDefConfig {
/**
* TableDef 类的前缀
*/
private String classPrefix = "";
/**
* TableDef 类的后缀
*/
private String classSuffix = "Def";
}

View File

@ -15,8 +15,7 @@
*/
package com.mybatisflex.codegen.entity;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.TableConfig;
import com.mybatisflex.codegen.config.*;
import com.mybatisflex.core.util.StringUtil;
import java.math.BigInteger;
@ -89,7 +88,7 @@ public class Table {
column.setAutoIncrement(false);
}
column.setColumnConfig(globalConfig.getColumnConfig(name, column.getName()));
column.setColumnConfig(globalConfig.getStrategyConfig().getColumnConfig(name, column.getName()));
columns.add(column);
}
@ -118,8 +117,10 @@ public class Table {
imports.addAll(column.getImportClasses());
}
EntityConfig entityConfig = globalConfig.getEntityConfig();
//开启 lombok
if (globalConfig.isEntityWithLombok()) {
if (entityConfig.isWithLombok()) {
//import lombok.AllArgsConstructor;
//import lombok.Builder;
//import lombok.Data;
@ -130,12 +131,12 @@ public class Table {
imports.add("lombok.NoArgsConstructor");
}
if (globalConfig.getEntitySupperClass() != null) {
imports.add(globalConfig.getEntitySupperClass().getName());
if (entityConfig.getSupperClass() != null) {
imports.add(entityConfig.getSupperClass().getName());
}
if (globalConfig.getEntityInterfaces() != null) {
for (Class<?> entityInterface : globalConfig.getEntityInterfaces()) {
if (entityConfig.getImplInterfaces() != null) {
for (Class<?> entityInterface : entityConfig.getImplInterfaces()) {
imports.add(entityInterface.getName());
}
}
@ -168,7 +169,7 @@ public class Table {
public String getEntityJavaFileName() {
String entityJavaFileName = name;
String tablePrefix = globalConfig.getTablePrefix();
String tablePrefix = globalConfig.getStrategyConfig().getTablePrefix();
if (tablePrefix != null) {
String[] tablePrefixes = tablePrefix.split(",");
for (String prefix : tablePrefixes) {
@ -189,9 +190,10 @@ public class Table {
*/
public String buildEntityClassName() {
String entityJavaFileName = getEntityJavaFileName();
return globalConfig.getEntityClassPrefix()
EntityConfig entityConfig = globalConfig.getEntityConfig();
return entityConfig.getClassPrefix()
+ entityJavaFileName
+ globalConfig.getEntityClassSuffix();
+ entityConfig.getClassSuffix();
}
/**
@ -201,21 +203,23 @@ public class Table {
*/
public String buildTableDefClassName() {
String tableDefJavaFileName = getEntityJavaFileName();
return globalConfig.getTableDefClassPrefix()
TableDefConfig tableDefConfig = globalConfig.getTableDefConfig();
return tableDefConfig.getClassPrefix()
+ tableDefJavaFileName
+ globalConfig.getTableDefClassSuffix();
+ tableDefConfig.getClassSuffix();
}
public String buildExtends() {
if (globalConfig.getEntitySupperClass() != null) {
return " extends " + globalConfig.getEntitySupperClass().getSimpleName();
EntityConfig entityConfig = globalConfig.getEntityConfig();
if (entityConfig.getSupperClass() != null) {
return " extends " + entityConfig.getSupperClass().getSimpleName();
} else {
return "";
}
}
public String buildImplements() {
Class<?>[] entityInterfaces = globalConfig.getEntityInterfaces();
Class<?>[] entityInterfaces = globalConfig.getEntityConfig().getImplInterfaces();
if (entityInterfaces != null && entityInterfaces.length > 0) {
return " implements " + StringUtil.join(", ", Arrays.stream(entityInterfaces)
.map(Class::getSimpleName).collect(Collectors.toList()));
@ -227,30 +231,34 @@ public class Table {
public String buildMapperClassName() {
String entityJavaFileName = getEntityJavaFileName();
return globalConfig.getMapperClassPrefix()
MapperConfig mapperConfig = globalConfig.getMapperConfig();
return mapperConfig.getClassPrefix()
+ entityJavaFileName
+ globalConfig.getMapperClassSuffix();
+ mapperConfig.getClassSuffix();
}
public String buildServiceClassName() {
String entityJavaFileName = getEntityJavaFileName();
return globalConfig.getServiceClassPrefix()
ServiceConfig serviceConfig = globalConfig.getServiceConfig();
return serviceConfig.getClassPrefix()
+ entityJavaFileName
+ globalConfig.getServiceClassSuffix();
+ serviceConfig.getClassSuffix();
}
public String buildServiceImplClassName() {
String entityJavaFileName = getEntityJavaFileName();
return globalConfig.getServiceImplClassPrefix()
ServiceImplConfig serviceImplConfig = globalConfig.getServiceImplConfig();
return serviceImplConfig.getClassPrefix()
+ entityJavaFileName
+ globalConfig.getServiceImplClassSuffix();
+ serviceImplConfig.getClassSuffix();
}
public String buildControllerClassName() {
String entityJavaFileName = getEntityJavaFileName();
return globalConfig.getControllerClassPrefix()
ControllerConfig controllerConfig = globalConfig.getControllerConfig();
return controllerConfig.getClassPrefix()
+ entityJavaFileName
+ globalConfig.getControllerClassSuffix();
+ controllerConfig.getClassSuffix();
}
/**
@ -258,7 +266,7 @@ public class Table {
*/
public String buildTableAnnotation() {
StringBuilder tableAnnotation = new StringBuilder();
if (globalConfig.isEntityWithLombok()) {
if (globalConfig.getEntityConfig().isWithLombok()) {
//@Data
//@Builder
//@NoArgsConstructor
@ -273,19 +281,19 @@ public class Table {
if (tableConfig != null) {
if (tableConfig.getSchema() != null) {
tableAnnotation.append(", schema = \"" + tableConfig.getSchema() + "\"");
tableAnnotation.append(", schema = \"").append(tableConfig.getSchema()).append("\"");
}
if (tableConfig.getCamelToUnderline() != null) {
tableAnnotation.append(", camelToUnderline = \"" + tableConfig.getCamelToUnderline() + "\"");
tableAnnotation.append(", camelToUnderline = \"").append(tableConfig.getCamelToUnderline()).append("\"");
}
if (tableConfig.getInsertListenerClass() != null) {
tableAnnotation.append(", onInsert = " + tableConfig.getInsertListenerClass().getSimpleName() + ".class");
tableAnnotation.append(", onInsert = ").append(tableConfig.getInsertListenerClass().getSimpleName()).append(".class");
}
if (tableConfig.getUpdateListenerClass() != null) {
tableAnnotation.append(", onUpdate = " + tableConfig.getUpdateListenerClass().getSimpleName() + ".class");
tableAnnotation.append(", onUpdate = ").append(tableConfig.getUpdateListenerClass().getSimpleName()).append(".class");
}
if (tableConfig.getSetListenerClass() != null) {
tableAnnotation.append(", onSet = " + tableConfig.getUpdateListenerClass().getSimpleName() + ".class");
tableAnnotation.append(", onSet = ").append(tableConfig.getUpdateListenerClass().getSimpleName()).append(".class");
}
if (Boolean.FALSE.equals(tableConfig.getMapperGenerateEnable())) {
tableAnnotation.append(", mapperGenerateEnable = false");
@ -294,35 +302,6 @@ public class Table {
return tableAnnotation.append(")").toString();
}
public String buildMapperImport() {
return globalConfig.getMapperSupperClass().getName();
}
public String buildServiceImport() {
return globalConfig.getServiceSupperClass().getName();
}
public String buildServiceImplImport() {
return globalConfig.getServiceImplSupperClass().getName();
}
public String buildMapperName() {
return globalConfig.getMapperSupperClass().getSimpleName();
}
public String buildServiceName() {
return globalConfig.getServiceSupperClass().getSimpleName();
}
public String buildServiceImplName() {
return globalConfig.getServiceImplSupperClass().getSimpleName();
}
public String buildControllerName() {
return globalConfig.getControllerSupperClass().getSimpleName();
}
@Override
public String toString() {
return "Table{" +

View File

@ -16,6 +16,8 @@
package com.mybatisflex.codegen.generator.impl;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.PackageConfig;
import com.mybatisflex.codegen.config.StrategyConfig;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.generator.IGenerator;
@ -29,7 +31,6 @@ import java.util.Map;
* @author 王帅
* @since 2023-05-14
*/
@SuppressWarnings("unused")
public class ControllerGenerator implements IGenerator {
private String templatePath = "/templates/enjoy/controller.tpl";
@ -47,21 +48,25 @@ public class ControllerGenerator implements IGenerator {
if (!globalConfig.isControllerGenerateEnable()) {
return;
}
PackageConfig packageConfig = globalConfig.getPackageConfig();
StrategyConfig strategyConfig = globalConfig.getStrategyConfig();
String controllerPackagePath = globalConfig.getControllerPackage().replace(".", "/");
File controllerJavaFile = new File(globalConfig.getSourceDir(), controllerPackagePath + "/" +
String controllerPackagePath = packageConfig.getControllerPackage().replace(".", "/");
File controllerJavaFile = new File(packageConfig.getSourceDir(), controllerPackagePath + "/" +
table.buildControllerClassName() + ".java");
if (controllerJavaFile.exists() && !globalConfig.isControllerOverwriteEnable()) {
if (controllerJavaFile.exists() && strategyConfig.isOverwriteEnable()) {
return;
}
Map<String, Object> params = new HashMap<>(2);
Map<String, Object> params = new HashMap<>(3);
params.put("table", table);
params.put("globalConfig", globalConfig);
params.put("packageConfig", packageConfig);
params.put("controllerConfig", globalConfig.getControllerConfig());
globalConfig.getTemplateEngine().generate(params, templatePath, controllerJavaFile);
strategyConfig.getTemplateEngine().generate(params, templatePath, controllerJavaFile);
}
}

View File

@ -16,6 +16,8 @@
package com.mybatisflex.codegen.generator.impl;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.PackageConfig;
import com.mybatisflex.codegen.config.StrategyConfig;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.generator.IGenerator;
@ -23,6 +25,12 @@ import java.io.File;
import java.util.HashMap;
import java.util.Map;
/**
* Entity 生成器
*
* @author Michael Yang
* @author 王帅
*/
public class EntityGenerator implements IGenerator {
private String templatePath = "/templates/enjoy/entity.tpl";
@ -37,14 +45,28 @@ public class EntityGenerator implements IGenerator {
@Override
public void generate(Table table, GlobalConfig globalConfig) {
String entityPackagePath = globalConfig.getEntityPackage().replace(".", "/");
File entityJavaFile = new File(globalConfig.getSourceDir(), entityPackagePath + "/" +
if (!globalConfig.isEntityGenerateEnable()) {
return;
}
PackageConfig packageConfig = globalConfig.getPackageConfig();
StrategyConfig strategyConfig = globalConfig.getStrategyConfig();
String entityPackagePath = packageConfig.getEntityPackage().replace(".", "/");
File entityJavaFile = new File(packageConfig.getSourceDir(), entityPackagePath + "/" +
table.buildEntityClassName() + ".java");
Map<String, Object> params = new HashMap<>();
params.put("table", table);
params.put("globalConfig", globalConfig);
globalConfig.getTemplateEngine().generate(params, templatePath, entityJavaFile);
if (entityJavaFile.exists() && !strategyConfig.isOverwriteEnable()) {
return;
}
Map<String, Object> params = new HashMap<>(3);
params.put("table", table);
params.put("packageConfig", packageConfig);
params.put("entityConfig", globalConfig.getEntityConfig());
strategyConfig.getTemplateEngine().generate(params, templatePath, entityJavaFile);
}
}

View File

@ -16,6 +16,8 @@
package com.mybatisflex.codegen.generator.impl;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.PackageConfig;
import com.mybatisflex.codegen.config.StrategyConfig;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.generator.IGenerator;
@ -23,6 +25,12 @@ import java.io.File;
import java.util.HashMap;
import java.util.Map;
/**
* Mapper 生成器
*
* @author Michael Yang
* @author 王帅
*/
public class MapperGenerator implements IGenerator {
private String templatePath = "/templates/enjoy/mapper.tpl";
@ -40,21 +48,25 @@ public class MapperGenerator implements IGenerator {
if (!globalConfig.isMapperGenerateEnable()) {
return;
}
PackageConfig packageConfig = globalConfig.getPackageConfig();
StrategyConfig strategyConfig = globalConfig.getStrategyConfig();
String mapperPackagePath = globalConfig.getMapperPackage().replace(".", "/");
File mapperJavaFile = new File(globalConfig.getSourceDir(), mapperPackagePath + "/" +
String mapperPackagePath = packageConfig.getMapperPackage().replace(".", "/");
File mapperJavaFile = new File(packageConfig.getSourceDir(), mapperPackagePath + "/" +
table.buildMapperClassName() + ".java");
if (mapperJavaFile.exists() && !globalConfig.isMapperOverwriteEnable()) {
return;//ignore
if (mapperJavaFile.exists() && !strategyConfig.isOverwriteEnable()) {
return;
}
Map<String, Object> params = new HashMap<>();
Map<String, Object> params = new HashMap<>(3);
params.put("table", table);
params.put("globalConfig", globalConfig);
params.put("packageConfig", packageConfig);
params.put("mapperConfig", globalConfig.getMapperConfig());
globalConfig.getTemplateEngine().generate(params, templatePath, mapperJavaFile);
strategyConfig.getTemplateEngine().generate(params, templatePath, mapperJavaFile);
}
}

View File

@ -16,6 +16,8 @@
package com.mybatisflex.codegen.generator.impl;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.PackageConfig;
import com.mybatisflex.codegen.config.StrategyConfig;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.generator.IGenerator;
@ -29,7 +31,6 @@ import java.util.Map;
* @author 王帅
* @since 2023-05-14
*/
@SuppressWarnings("unused")
public class ServiceGenerator implements IGenerator {
private String templatePath = "/templates/enjoy/service.tpl";
@ -47,21 +48,25 @@ public class ServiceGenerator implements IGenerator {
if (!globalConfig.isServiceGenerateEnable()) {
return;
}
PackageConfig packageConfig = globalConfig.getPackageConfig();
StrategyConfig strategyConfig = globalConfig.getStrategyConfig();
String servicePackagePath = globalConfig.getServicePackage().replace(".", "/");
File serviceJavaFile = new File(globalConfig.getSourceDir(), servicePackagePath + "/" +
String servicePackagePath = packageConfig.getServicePackage().replace(".", "/");
File serviceJavaFile = new File(packageConfig.getSourceDir(), servicePackagePath + "/" +
table.buildServiceClassName() + ".java");
if (serviceJavaFile.exists() && !globalConfig.isServiceOverwriteEnable()) {
if (serviceJavaFile.exists() && !strategyConfig.isOverwriteEnable()) {
return;
}
Map<String, Object> params = new HashMap<>(2);
Map<String, Object> params = new HashMap<>(3);
params.put("table", table);
params.put("globalConfig", globalConfig);
params.put("packageConfig", packageConfig);
params.put("serviceConfig", globalConfig.getServiceConfig());
globalConfig.getTemplateEngine().generate(params, templatePath, serviceJavaFile);
strategyConfig.getTemplateEngine().generate(params, templatePath, serviceJavaFile);
}
}

View File

@ -16,6 +16,8 @@
package com.mybatisflex.codegen.generator.impl;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.PackageConfig;
import com.mybatisflex.codegen.config.StrategyConfig;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.generator.IGenerator;
@ -29,7 +31,6 @@ import java.util.Map;
* @author 王帅
* @since 2023-05-14
*/
@SuppressWarnings("unused")
public class ServiceImplGenerator implements IGenerator {
private String templatePath = "/templates/enjoy/serviceImpl.tpl";
@ -47,21 +48,25 @@ public class ServiceImplGenerator implements IGenerator {
if (!globalConfig.isServiceImplGenerateEnable()) {
return;
}
PackageConfig packageConfig = globalConfig.getPackageConfig();
StrategyConfig strategyConfig = globalConfig.getStrategyConfig();
String serviceImplPackagePath = globalConfig.getServiceImplPackage().replace(".", "/");
File serviceImplJavaFile = new File(globalConfig.getSourceDir(), serviceImplPackagePath + "/" +
String serviceImplPackagePath = packageConfig.getServiceImplPackage().replace(".", "/");
File serviceImplJavaFile = new File(packageConfig.getSourceDir(), serviceImplPackagePath + "/" +
table.buildServiceImplClassName() + ".java");
if (serviceImplJavaFile.exists() && !globalConfig.isServiceImplOverwriteEnable()) {
if (serviceImplJavaFile.exists() && !strategyConfig.isOverwriteEnable()) {
return;
}
Map<String, Object> params = new HashMap<>(2);
Map<String, Object> params = new HashMap<>(3);
params.put("table", table);
params.put("globalConfig", globalConfig);
params.put("packageConfig", packageConfig);
params.put("serviceImplConfig", globalConfig.getServiceImplConfig());
globalConfig.getTemplateEngine().generate(params, templatePath, serviceImplJavaFile);
strategyConfig.getTemplateEngine().generate(params, templatePath, serviceImplJavaFile);
}
}

View File

@ -16,6 +16,8 @@
package com.mybatisflex.codegen.generator.impl;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.PackageConfig;
import com.mybatisflex.codegen.config.StrategyConfig;
import com.mybatisflex.codegen.entity.Table;
import com.mybatisflex.codegen.generator.IGenerator;
@ -23,6 +25,12 @@ import java.io.File;
import java.util.HashMap;
import java.util.Map;
/**
* TableDef 生成器
*
* @author Michael Yang
* @author 王帅
*/
public class TableDefGenerator implements IGenerator {
private String templatePath = "/templates/enjoy/tableDef.tpl";
@ -41,15 +49,24 @@ public class TableDefGenerator implements IGenerator {
return;
}
String tableDefPackagePath = globalConfig.getTableDefPackage().replace(".", "/");
File tableDefJavaFile = new File(globalConfig.getSourceDir(), tableDefPackagePath + "/" +
PackageConfig packageConfig = globalConfig.getPackageConfig();
StrategyConfig strategyConfig = globalConfig.getStrategyConfig();
String tableDefPackagePath = packageConfig.getTableDefPackage().replace(".", "/");
File tableDefJavaFile = new File(packageConfig.getSourceDir(), tableDefPackagePath + "/" +
table.buildTableDefClassName() + ".java");
Map<String, Object> params = new HashMap<>();
params.put("table", table);
params.put("globalConfig", globalConfig);
if (tableDefJavaFile.exists() && !strategyConfig.isOverwriteEnable()) {
return;
}
globalConfig.getTemplateEngine().generate(params, templatePath, tableDefJavaFile);
Map<String, Object> params = new HashMap<>(3);
params.put("table", table);
params.put("packageConfig", packageConfig);
params.put("tableDefConfig", globalConfig.getTableDefConfig());
strategyConfig.getTemplateEngine().generate(params, templatePath, tableDefJavaFile);
}
}

View File

@ -1,23 +1,23 @@
package #(globalConfig.controllerPackage);
package #(packageConfig.controllerPackage);
import org.springframework.web.bind.annotation.RequestMapping;
#if(globalConfig.restStyleController)
#if(controllerConfig.restStyle)
import org.springframework.web.bind.annotation.RestController;
#else
import org.springframework.stereotype.Controller;
#end
#if(globalConfig.controllerSupperClass)
import #(table.buildControllerImport())
#if(controllerConfig.supperClass)
import #(controllerConfig.buildSuperClassImport())
#end
#if(globalConfig.restStyleController)
#if(controllerConfig.restStyle)
@RestController
#else
@Controller
#end
@RequestMapping("/#(table.getEntityJavaFileName())")
#if(globalConfig.controllerSupperClass)
public class #(table.table.buildControllerClassName()) extends #(table.buildControllerName()) {
#if(controllerConfig.supperClass)
public class #(table.buildControllerClassName()) extends #(controllerConfig.buildSuperClassName()) {
}
#else

View File

@ -1,4 +1,4 @@
package #(globalConfig.entityPackage);
package #(packageConfig.entityPackage);
#for(importClass:table.buildImports())
import #(importClass);
@ -13,7 +13,7 @@ public class #(table.buildEntityClassName())#(table.buildExtends())#(table.build
#(column.buildAnnotations())private #(column.propertySimpleType) #(column.property);
#end
#if(!globalConfig.isEntityWithLombok())
#if(!entityConfig.isWithLombok())
#for(column: table.columns)
public #(column.propertySimpleType) #(column.getterMethod())() {
return #(column.property);

View File

@ -1,8 +1,8 @@
package #(globalConfig.mapperPackage);
package #(packageConfig.mapperPackage);
import #(table.buildMapperImport());
import #(globalConfig.entityPackage).#(table.buildEntityClassName());
import #(mapperConfig.buildSuperClassImport());
import #(packageConfig.entityPackage).#(table.buildEntityClassName());
public interface #(table.buildMapperClassName()) extends #(table.buildMapperName())<#(table.buildEntityClassName())> {
public interface #(table.buildMapperClassName()) extends #(mapperConfig.buildSuperClassName())<#(table.buildEntityClassName())> {
}

View File

@ -1,8 +1,8 @@
package #(globalConfig.servicePackage);
package #(packageConfig.servicePackage);
import #(table.buildServiceImport());
import #(globalConfig.entityPackage).#(table.buildEntityClassName());
import #(serviceConfig.buildSuperClassImport());
import #(packageConfig.entityPackage).#(table.buildEntityClassName());
public interface #(table.buildServiceClassName()) extends #(table.buildServiceName())<#(table.buildEntityClassName())> {
public interface #(table.buildServiceClassName()) extends #(serviceConfig.buildSuperClassName())<#(table.buildEntityClassName())> {
}

View File

@ -1,12 +1,12 @@
package #(globalConfig.serviceImplPackage);
package #(packageConfig.serviceImplPackage);
import #(table.buildServiceImplImport());
import #(globalConfig.entityPackage).#(table.buildEntityClassName());
import #(globalConfig.mapperPackage).#(table.buildMapperClassName());
import #(globalConfig.servicePackage).#(table.buildServiceClassName());
import #(serviceImplConfig.buildSuperClassImport());
import #(packageConfig.entityPackage).#(table.buildEntityClassName());
import #(packageConfig.mapperPackage).#(table.buildMapperClassName());
import #(packageConfig.servicePackage).#(table.buildServiceClassName());
import org.springframework.stereotype.Service;
@Service
public class #(table.buildServiceImplClassName()) extends #(table.buildServiceImplName())<#(table.buildMapperClassName()), #(table.buildEntityClassName())> implements #(table.buildServiceClassName()) {
public class #(table.buildServiceImplClassName()) extends #(serviceImplConfig.buildSuperClassName())<#(table.buildMapperClassName()), #(table.buildEntityClassName())> implements #(table.buildServiceClassName()) {
}

View File

@ -1,4 +1,4 @@
package #(globalConfig.entityPackage).tables;
package #(packageConfig.tableDefPackage);
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.table.TableDef;