parent
dee3cf64cb
commit
7031a51cd4
@ -16,14 +16,13 @@
|
|||||||
|
|
||||||
package top.charles7c.continew.admin.generator.config.properties;
|
package top.charles7c.continew.admin.generator.config.properties;
|
||||||
|
|
||||||
import java.util.Map;
|
import cn.hutool.core.map.MapUtil;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import cn.hutool.core.map.MapUtil;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 代码生成器配置属性
|
* 代码生成器配置属性
|
||||||
@ -39,7 +38,12 @@ public class GeneratorProperties {
|
|||||||
/**
|
/**
|
||||||
* 排除数据表(哪些数据表不展示在代码生成中)
|
* 排除数据表(哪些数据表不展示在代码生成中)
|
||||||
*/
|
*/
|
||||||
private String[] excludeTables;
|
private String[] excludeTables = new String[0];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 类型映射
|
||||||
|
*/
|
||||||
|
private Map<String, Map<String, List<String>>> typeMappings = MapUtil.newHashMap();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 模板配置
|
* 模板配置
|
||||||
|
@ -19,8 +19,6 @@ package top.charles7c.continew.admin.generator.model.entity;
|
|||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.db.meta.Column;
|
import cn.hutool.db.meta.Column;
|
||||||
import cn.hutool.setting.dialect.Props;
|
|
||||||
import cn.hutool.setting.dialect.PropsUtil;
|
|
||||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||||
import com.baomidou.mybatisplus.annotation.TableField;
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
import com.baomidou.mybatisplus.annotation.TableName;
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
@ -169,10 +167,4 @@ public class FieldConfigDO implements Serializable {
|
|||||||
this.columnName = columnName;
|
this.columnName = columnName;
|
||||||
this.fieldName = StrUtil.toCamelCase(this.columnName);
|
this.fieldName = StrUtil.toCamelCase(this.columnName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setColumnType(String columnType) {
|
|
||||||
this.columnType = columnType;
|
|
||||||
Props generatorProp = PropsUtil.get("generator");
|
|
||||||
this.fieldType = generatorProp.getStr(columnType);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -46,6 +46,7 @@ import top.charles7c.continew.admin.generator.service.GeneratorService;
|
|||||||
import top.charles7c.continew.starter.core.constant.StringConstants;
|
import top.charles7c.continew.starter.core.constant.StringConstants;
|
||||||
import top.charles7c.continew.starter.core.exception.BusinessException;
|
import top.charles7c.continew.starter.core.exception.BusinessException;
|
||||||
import top.charles7c.continew.starter.core.util.TemplateUtils;
|
import top.charles7c.continew.starter.core.util.TemplateUtils;
|
||||||
|
import top.charles7c.continew.starter.data.core.enums.DatabaseType;
|
||||||
import top.charles7c.continew.starter.data.core.util.MetaUtils;
|
import top.charles7c.continew.starter.data.core.util.MetaUtils;
|
||||||
import top.charles7c.continew.starter.data.core.util.Table;
|
import top.charles7c.continew.starter.data.core.util.Table;
|
||||||
import top.charles7c.continew.starter.core.util.validate.CheckUtils;
|
import top.charles7c.continew.starter.core.util.validate.CheckUtils;
|
||||||
@ -136,6 +137,12 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||||||
List<FieldConfigDO> latestFieldConfigList = new ArrayList<>();
|
List<FieldConfigDO> latestFieldConfigList = new ArrayList<>();
|
||||||
// 获取最新数据表列信息
|
// 获取最新数据表列信息
|
||||||
Collection<Column> columnList = MetaUtils.getColumns(dataSource, tableName);
|
Collection<Column> columnList = MetaUtils.getColumns(dataSource, tableName);
|
||||||
|
// 获取数据库对应的类型映射配置
|
||||||
|
DatabaseType databaseType = MetaUtils.getDatabaseType(dataSource);
|
||||||
|
Map<String, List<String>> typeMappingMap = generatorProperties.getTypeMappings()
|
||||||
|
.get(databaseType.getDatabase());
|
||||||
|
CheckUtils.throwIfEmpty(typeMappingMap, "请先配置对应数据库的类型映射");
|
||||||
|
Set<Map.Entry<String, List<String>>> typeMappingEntrySet = typeMappingMap.entrySet();
|
||||||
// 新增或更新字段配置
|
// 新增或更新字段配置
|
||||||
Map<String, FieldConfigDO> fieldConfigMap = fieldConfigList.stream()
|
Map<String, FieldConfigDO> fieldConfigMap = fieldConfigList.stream()
|
||||||
.collect(Collectors.toMap(FieldConfigDO::getColumnName, Function.identity(), (key1, key2) -> key2));
|
.collect(Collectors.toMap(FieldConfigDO::getColumnName, Function.identity(), (key1, key2) -> key2));
|
||||||
@ -143,13 +150,19 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||||||
for (Column column : columnList) {
|
for (Column column : columnList) {
|
||||||
FieldConfigDO fieldConfig = Optional.ofNullable(fieldConfigMap.get(column.getName()))
|
FieldConfigDO fieldConfig = Optional.ofNullable(fieldConfigMap.get(column.getName()))
|
||||||
.orElseGet(() -> new FieldConfigDO(column));
|
.orElseGet(() -> new FieldConfigDO(column));
|
||||||
fieldConfig.setFieldSort(i++);
|
|
||||||
// 更新已有字段配置
|
// 更新已有字段配置
|
||||||
if (null != fieldConfig.getCreateTime()) {
|
if (null != fieldConfig.getCreateTime()) {
|
||||||
String columnType = StrUtil.splitToArray(column.getTypeName(), StringConstants.SPACE)[0].toLowerCase();
|
String columnType = StrUtil.splitToArray(column.getTypeName(), StringConstants.SPACE)[0].toLowerCase();
|
||||||
fieldConfig.setColumnType(columnType);
|
fieldConfig.setColumnType(columnType);
|
||||||
fieldConfig.setColumnSize(Convert.toStr(column.getSize()));
|
fieldConfig.setColumnSize(Convert.toStr(column.getSize()));
|
||||||
}
|
}
|
||||||
|
String fieldType = typeMappingEntrySet.stream()
|
||||||
|
.filter(entry -> entry.getValue().contains(fieldConfig.getColumnType()))
|
||||||
|
.map(Map.Entry::getKey)
|
||||||
|
.findFirst()
|
||||||
|
.orElse(null);
|
||||||
|
fieldConfig.setFieldType(fieldType);
|
||||||
|
fieldConfig.setFieldSort(i++);
|
||||||
latestFieldConfigList.add(fieldConfig);
|
latestFieldConfigList.add(fieldConfig);
|
||||||
}
|
}
|
||||||
return latestFieldConfigList;
|
return latestFieldConfigList;
|
||||||
@ -232,13 +245,13 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||||||
// 渲染前端代码
|
// 渲染前端代码
|
||||||
// api 代码
|
// api 代码
|
||||||
genConfigMap.put("fieldConfigs", fieldConfigList);
|
genConfigMap.put("fieldConfigs", fieldConfigList);
|
||||||
String apiContent = TemplateUtils.render("generator/api.ftl", genConfigMap);
|
String apiContent = TemplateUtils.render("api.ftl", genConfigMap);
|
||||||
GeneratePreviewResp apiCodePreview = new GeneratePreviewResp();
|
GeneratePreviewResp apiCodePreview = new GeneratePreviewResp();
|
||||||
apiCodePreview.setFileName(classNamePrefix.toLowerCase() + ".ts");
|
apiCodePreview.setFileName(classNamePrefix.toLowerCase() + ".ts");
|
||||||
apiCodePreview.setContent(apiContent);
|
apiCodePreview.setContent(apiContent);
|
||||||
generatePreviewList.add(apiCodePreview);
|
generatePreviewList.add(apiCodePreview);
|
||||||
// view 代码
|
// view 代码
|
||||||
String viewContent = TemplateUtils.render("generator/index.ftl", genConfigMap);
|
String viewContent = TemplateUtils.render("index.ftl", genConfigMap);
|
||||||
GeneratePreviewResp viewCodePreview = new GeneratePreviewResp();
|
GeneratePreviewResp viewCodePreview = new GeneratePreviewResp();
|
||||||
viewCodePreview.setFileName("index.vue");
|
viewCodePreview.setFileName("index.vue");
|
||||||
viewCodePreview.setContent(viewContent);
|
viewCodePreview.setContent(viewContent);
|
||||||
@ -260,9 +273,9 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||||||
// 1.确定后端代码基础路径
|
// 1.确定后端代码基础路径
|
||||||
// 例如:D:/continew-admin
|
// 例如:D:/continew-admin
|
||||||
String projectPath = SystemUtil.getUserInfo().getCurrentDir();
|
String projectPath = SystemUtil.getUserInfo().getCurrentDir();
|
||||||
// 例如:D:/continew-admin/continew-admin-tool
|
// 例如:D:/continew-admin/continew-admin-system
|
||||||
File backendModuleFile = new File(projectPath, genConfig.getModuleName());
|
File backendModuleFile = new File(projectPath, genConfig.getModuleName());
|
||||||
// 例如:D:/continew-admin/continew-admin-tool/src/main/java/top/charles7c/continew/admin/tool
|
// 例如:D:/continew-admin/continew-admin-system/src/main/java/top/charles7c/continew/admin/system
|
||||||
List<String> backendModuleChildPathList = CollUtil.newArrayList("src", "main", "java");
|
List<String> backendModuleChildPathList = CollUtil.newArrayList("src", "main", "java");
|
||||||
backendModuleChildPathList.addAll(StrUtil.split(packageName, StringConstants.DOT));
|
backendModuleChildPathList.addAll(StrUtil.split(packageName, StringConstants.DOT));
|
||||||
File backendParentFile = FileUtil.file(backendModuleFile, backendModuleChildPathList
|
File backendParentFile = FileUtil.file(backendModuleFile, backendModuleChildPathList
|
||||||
@ -273,7 +286,7 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||||||
.toList();
|
.toList();
|
||||||
Map<String, TemplateConfig> templateConfigMap = generatorProperties.getTemplateConfigs();
|
Map<String, TemplateConfig> templateConfigMap = generatorProperties.getTemplateConfigs();
|
||||||
for (GeneratePreviewResp codePreview : backendCodePreviewList) {
|
for (GeneratePreviewResp codePreview : backendCodePreviewList) {
|
||||||
// 例如:D:/continew-admin/continew-admin-tool/src/main/java/top/charles7c/continew/admin/tool/service/impl/XxxServiceImpl.java
|
// 例如:D:/continew-admin/continew-admin-system/src/main/java/top/charles7c/continew/admin/system/service/impl/XxxServiceImpl.java
|
||||||
TemplateConfig templateConfig = templateConfigMap.get(codePreview.getFileName()
|
TemplateConfig templateConfig = templateConfigMap.get(codePreview.getFileName()
|
||||||
.replace(classNamePrefix, StringConstants.EMPTY)
|
.replace(classNamePrefix, StringConstants.EMPTY)
|
||||||
.replace(FileNameUtil.EXT_JAVA, StringConstants.EMPTY));
|
.replace(FileNameUtil.EXT_JAVA, StringConstants.EMPTY));
|
||||||
@ -310,7 +323,7 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||||||
FileUtil.writeUtf8String(apiCodePreview.getContent(), apiFile);
|
FileUtil.writeUtf8String(apiCodePreview.getContent(), apiFile);
|
||||||
// 2.生成 view 代码
|
// 2.生成 view 代码
|
||||||
GeneratePreviewResp viewCodePreview = frontendCodePreviewList.get(1);
|
GeneratePreviewResp viewCodePreview = frontendCodePreviewList.get(1);
|
||||||
// 例如:D:/continew-admin-ui/src/views/tool/xxx/index.vue
|
// 例如:D:/continew-admin-ui/src/views/system/xxx/index.vue
|
||||||
File indexFile = FileUtil.file(frontendPath, apiModuleName, StrUtil
|
File indexFile = FileUtil.file(frontendPath, apiModuleName, StrUtil
|
||||||
.lowerFirst(classNamePrefix), "index.vue");
|
.lowerFirst(classNamePrefix), "index.vue");
|
||||||
if (indexFile.exists() && Boolean.FALSE.equals(isOverride)) {
|
if (indexFile.exists() && Boolean.FALSE.equals(isOverride)) {
|
||||||
|
@ -1,26 +0,0 @@
|
|||||||
tinyint=Integer
|
|
||||||
smallint=Integer
|
|
||||||
mediumint=Integer
|
|
||||||
int=Integer
|
|
||||||
integer=Integer
|
|
||||||
|
|
||||||
bigint=Long
|
|
||||||
|
|
||||||
float=Float
|
|
||||||
|
|
||||||
double=Double
|
|
||||||
|
|
||||||
decimal=BigDecimal
|
|
||||||
|
|
||||||
bit=Boolean
|
|
||||||
|
|
||||||
char=String
|
|
||||||
varchar=String
|
|
||||||
tinytext=String
|
|
||||||
text=String
|
|
||||||
mediumtext=String
|
|
||||||
longtext=String
|
|
||||||
|
|
||||||
date=LocalDate
|
|
||||||
datetime=LocalDateTime
|
|
||||||
timestamp=LocalDateTime
|
|
@ -6,11 +6,42 @@ generator:
|
|||||||
- DATABASECHANGELOGLOCK
|
- DATABASECHANGELOGLOCK
|
||||||
- gen_config
|
- gen_config
|
||||||
- gen_field_config
|
- gen_field_config
|
||||||
# 模板配置
|
## 类型映射
|
||||||
|
typeMappings:
|
||||||
|
MySQL:
|
||||||
|
Integer:
|
||||||
|
- int
|
||||||
|
- tinyint
|
||||||
|
- smallint
|
||||||
|
- mediumint
|
||||||
|
- integer
|
||||||
|
String:
|
||||||
|
- varchar
|
||||||
|
- char
|
||||||
|
- text
|
||||||
|
- mediumtext
|
||||||
|
- longtext
|
||||||
|
- tinytext
|
||||||
|
LocalDate:
|
||||||
|
- date
|
||||||
|
LocalDateTime:
|
||||||
|
- datetime
|
||||||
|
- timestamp
|
||||||
|
Long:
|
||||||
|
- bigint
|
||||||
|
Float:
|
||||||
|
- float
|
||||||
|
Double:
|
||||||
|
- double
|
||||||
|
BigDecimal:
|
||||||
|
- decimal
|
||||||
|
Boolean:
|
||||||
|
- bit
|
||||||
|
## 模板配置
|
||||||
templateConfigs:
|
templateConfigs:
|
||||||
DO:
|
DO:
|
||||||
# 模板路径
|
# 模板路径
|
||||||
templatePath: generator/Entity.ftl
|
templatePath: Entity.ftl
|
||||||
# 包名称
|
# 包名称
|
||||||
packageName: model.entity
|
packageName: model.entity
|
||||||
# 排除字段
|
# 排除字段
|
||||||
@ -21,20 +52,20 @@ generator:
|
|||||||
- updateUser
|
- updateUser
|
||||||
- updateTime
|
- updateTime
|
||||||
Query:
|
Query:
|
||||||
templatePath: generator/Query.ftl
|
templatePath: Query.ftl
|
||||||
packageName: model.query
|
packageName: model.query
|
||||||
Req:
|
Req:
|
||||||
templatePath: generator/Req.ftl
|
templatePath: Req.ftl
|
||||||
packageName: model.req
|
packageName: model.req
|
||||||
Resp:
|
Resp:
|
||||||
templatePath: generator/Resp.ftl
|
templatePath: Resp.ftl
|
||||||
packageName: model.resp
|
packageName: model.resp
|
||||||
excludeFields:
|
excludeFields:
|
||||||
- id
|
- id
|
||||||
- createUser
|
- createUser
|
||||||
- createTime
|
- createTime
|
||||||
DetailResp:
|
DetailResp:
|
||||||
templatePath: generator/DetailResp.ftl
|
templatePath: DetailResp.ftl
|
||||||
packageName: model.resp
|
packageName: model.resp
|
||||||
excludeFields:
|
excludeFields:
|
||||||
- id
|
- id
|
||||||
@ -43,14 +74,14 @@ generator:
|
|||||||
- updateUser
|
- updateUser
|
||||||
- updateTime
|
- updateTime
|
||||||
Mapper:
|
Mapper:
|
||||||
templatePath: generator/Mapper.ftl
|
templatePath: Mapper.ftl
|
||||||
packageName: mapper
|
packageName: mapper
|
||||||
Service:
|
Service:
|
||||||
templatePath: generator/Service.ftl
|
templatePath: Service.ftl
|
||||||
packageName: service
|
packageName: service
|
||||||
ServiceImpl:
|
ServiceImpl:
|
||||||
templatePath: generator/ServiceImpl.ftl
|
templatePath: ServiceImpl.ftl
|
||||||
packageName: service.impl
|
packageName: service.impl
|
||||||
Controller:
|
Controller:
|
||||||
templatePath: generator/Controller.ftl
|
templatePath: Controller.ftl
|
||||||
packageName: controller
|
packageName: controller
|
@ -214,6 +214,8 @@ spring:
|
|||||||
profiles:
|
profiles:
|
||||||
# 启用的环境
|
# 启用的环境
|
||||||
active: dev
|
active: dev
|
||||||
|
include:
|
||||||
|
- generator
|
||||||
main:
|
main:
|
||||||
# 允许定义重名的 bean 对象覆盖原有的 bean
|
# 允许定义重名的 bean 对象覆盖原有的 bean
|
||||||
allow-bean-definition-overriding: true
|
allow-bean-definition-overriding: true
|
||||||
|
Loading…
Reference in New Issue
Block a user