update 优化 代码生成模块

This commit is contained in:
疯狂的狮子Li
2026-03-24 10:49:16 +08:00
parent fe8d15c6a8
commit 77fc3d37d8
6 changed files with 19 additions and 57 deletions

View File

@@ -1,45 +0,0 @@
package org.dromara.generator.config;
import cn.hutool.extra.template.TemplateConfig;
import org.dromara.common.core.factory.YmlPropertySourceFactory;
import org.dromara.generator.config.properties.GenProperties;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
/**
* 读取代码生成相关配置
*
* @author ruoyi
*/
@AutoConfiguration
@EnableConfigurationProperties(GenProperties.class)
@PropertySource(value = "classpath:generator.yml", factory = YmlPropertySourceFactory.class)
public class GenConfig {
private static GenProperties genProperties;
public GenConfig(GenProperties genProperties) {
GenConfig.genProperties = genProperties;
}
public static String getAuthor() {
return genProperties.getAuthor();
}
public static String getPackageName() {
return genProperties.getPackageName();
}
public static boolean getAutoRemovePre() {
return genProperties.isAutoRemovePre();
}
public static String getTablePrefix() {
return genProperties.getTablePrefix();
}
public static TemplateConfig getTemplateConfig() {
return genProperties.getTemplateConfig();
}
}

View File

@@ -2,7 +2,10 @@ package org.dromara.generator.config.properties;
import cn.hutool.extra.template.TemplateConfig;
import lombok.Data;
import org.dromara.common.core.factory.YmlPropertySourceFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
@@ -12,7 +15,9 @@ import java.nio.charset.StandardCharsets;
* @author 秋辞未寒
*/
@Data
@Component
@ConfigurationProperties(prefix = "gen")
@PropertySource(value = "classpath:generator.yml", factory = YmlPropertySourceFactory.class)
public class GenProperties {
/**

View File

@@ -3,8 +3,9 @@ package org.dromara.generator.util;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.RegExUtils;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.generator.config.GenConfig;
import org.dromara.generator.config.properties.GenProperties;
import org.dromara.generator.constant.GenConstants;
import org.dromara.generator.domain.GenTable;
import org.dromara.generator.domain.GenTableColumn;
@@ -19,6 +20,8 @@ import java.util.Arrays;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class GenUtils {
private final static GenProperties PROPERTIES = SpringUtils.getBean(GenProperties.class);
/**
* 初始化表信息
*
@@ -26,11 +29,11 @@ public class GenUtils {
*/
public static void initTable(GenTable genTable) {
genTable.setClassName(convertClassName(genTable.getTableName()));
genTable.setPackageName(GenConfig.getPackageName());
genTable.setModuleName(getModuleName(GenConfig.getPackageName()));
genTable.setPackageName(PROPERTIES.getPackageName());
genTable.setModuleName(getModuleName(PROPERTIES.getPackageName()));
genTable.setBusinessName(getBusinessName(genTable.getTableName()));
genTable.setFunctionName(replaceText(genTable.getTableComment()));
genTable.setFunctionAuthor(GenConfig.getAuthor());
genTable.setFunctionAuthor(PROPERTIES.getAuthor());
genTable.setCreateTime(null);
genTable.setUpdateTime(null);
}
@@ -157,8 +160,8 @@ public class GenUtils {
* @return 类名
*/
public static String convertClassName(String tableName) {
boolean autoRemovePre = GenConfig.getAutoRemovePre();
String tablePrefix = GenConfig.getTablePrefix();
boolean autoRemovePre = PROPERTIES.isAutoRemovePre();
String tablePrefix = PROPERTIES.getTablePrefix();
if (autoRemovePre && StringUtils.isNotEmpty(tablePrefix)) {
String[] searchList = StringUtils.split(tablePrefix, StringUtils.SEPARATOR);
tableName = replaceFirst(tableName, searchList);

View File

@@ -9,11 +9,12 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.common.mybatis.enums.DataBaseType;
import org.dromara.common.mybatis.helper.DataBaseHelper;
import org.dromara.generator.config.GenConfig;
import org.dromara.generator.config.properties.GenProperties;
import org.dromara.generator.constant.GenConstants;
import org.dromara.generator.domain.GenTable;
import org.dromara.generator.domain.GenTableColumn;
@@ -52,7 +53,8 @@ public class TemplateEngineUtils {
static {
// 模板引擎初始化
TEMPLATE_ENGINE = TemplateUtil.createEngine(GenConfig.getTemplateConfig());
GenProperties properties = SpringUtils.getBean(GenProperties.class);
TEMPLATE_ENGINE = TemplateUtil.createEngine(properties.getTemplateConfig());
TEMPLATE_MAPPER = PathNamedTemplate.form(TEMPLATE_ENGINE, GenConstants.TEMPLATE_PATHS);
}