diff --git a/src/main/java/cn/iocoder/dashboard/framework/codegen/config/CodegenConfiguration.java b/src/main/java/cn/iocoder/dashboard/framework/codegen/config/CodegenConfiguration.java new file mode 100644 index 000000000..7e357bc86 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/codegen/config/CodegenConfiguration.java @@ -0,0 +1,9 @@ +package cn.iocoder.dashboard.framework.codegen.config; + +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableConfigurationProperties(CodegenProperties.class) +public class CodegenConfiguration { +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/codegen/config/CodegenProperties.java b/src/main/java/cn/iocoder/dashboard/framework/codegen/config/CodegenProperties.java new file mode 100644 index 000000000..8b77bbfb8 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/codegen/config/CodegenProperties.java @@ -0,0 +1,28 @@ +package cn.iocoder.dashboard.framework.codegen.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.validation.annotation.Validated; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; +import java.util.Collection; + +@ConfigurationProperties(prefix = "yudao.codegen") +@Validated +@Data +public class CodegenProperties { + + /** + * 生成的 Java 代码的基础包 + */ + @NotNull(message = "Java 代码的基础包不能为空") + private String basePackage; + + /** + * 数据库名数组 + */ + @NotEmpty(message = "数据库不能为空") + private Collection dbSchemas; + +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/codegen/package-info.java b/src/main/java/cn/iocoder/dashboard/framework/codegen/package-info.java new file mode 100644 index 000000000..29f1464fe --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/codegen/package-info.java @@ -0,0 +1,4 @@ +/** + * 代码生成器 + */ +package cn.iocoder.dashboard.framework.codegen; diff --git a/src/main/java/cn/iocoder/dashboard/framework/datasource/DataSourceConfiguration.java b/src/main/java/cn/iocoder/dashboard/framework/datasource/config/DataSourceConfiguration.java similarity index 83% rename from src/main/java/cn/iocoder/dashboard/framework/datasource/DataSourceConfiguration.java rename to src/main/java/cn/iocoder/dashboard/framework/datasource/config/DataSourceConfiguration.java index 8c7f2a4c4..44654f328 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/datasource/DataSourceConfiguration.java +++ b/src/main/java/cn/iocoder/dashboard/framework/datasource/config/DataSourceConfiguration.java @@ -1,4 +1,4 @@ -package cn.iocoder.dashboard.framework.datasource; +package cn.iocoder.dashboard.framework.datasource.config; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.annotation.EnableTransactionManagement; diff --git a/src/main/java/cn/iocoder/dashboard/framework/mybatis/config/MybatisConfiguration.java b/src/main/java/cn/iocoder/dashboard/framework/mybatis/config/MybatisConfiguration.java index b2038cd36..a955c402e 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/mybatis/config/MybatisConfiguration.java +++ b/src/main/java/cn/iocoder/dashboard/framework/mybatis/config/MybatisConfiguration.java @@ -13,7 +13,7 @@ import org.springframework.context.annotation.Configuration; * @author 芋道源码 */ @Configuration -@MapperScan(value = "cn.iocoder.dashboard", annotationClass = Mapper.class) +@MapperScan(value = "${yudao.info.base-package}", annotationClass = Mapper.class) public class MybatisConfiguration { @Bean diff --git a/src/main/java/cn/iocoder/dashboard/framework/mybatis/core/query/QueryWrapperX.java b/src/main/java/cn/iocoder/dashboard/framework/mybatis/core/query/QueryWrapperX.java index 6e5e00c8f..d44c86a8f 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/mybatis/core/query/QueryWrapperX.java +++ b/src/main/java/cn/iocoder/dashboard/framework/mybatis/core/query/QueryWrapperX.java @@ -104,4 +104,10 @@ public class QueryWrapperX extends QueryWrapper { return this; } + @Override + public QueryWrapperX in(String column, Collection coll) { + super.in(column, coll); + return this; + } + } diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/controller/codegen/ToolCodegenController.java b/src/main/java/cn/iocoder/dashboard/modules/tool/controller/codegen/ToolCodegenController.java index fb87c6116..f642dd41d 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/tool/controller/codegen/ToolCodegenController.java +++ b/src/main/java/cn/iocoder/dashboard/modules/tool/controller/codegen/ToolCodegenController.java @@ -9,13 +9,16 @@ import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.ToolCodegenPrevie import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.ToolCodegenUpdateReqVO; import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.table.ToolCodegenTablePageReqVO; import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.table.ToolCodegenTableRespVO; +import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.table.ToolSchemaTableRespVO; import cn.iocoder.dashboard.modules.tool.convert.codegen.ToolCodegenConvert; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColumnDO; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO; +import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaTableDO; import cn.iocoder.dashboard.modules.tool.service.codegen.ToolCodegenService; import cn.iocoder.dashboard.util.servlet.ServletUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -40,6 +43,23 @@ public class ToolCodegenController { @Resource private ToolCodegenService codegenService; + @ApiOperation(value = "获得数据库自带的表定义列表", notes = "会过滤掉已经导入 Codegen 的表") + @GetMapping("/db/table/list") + @ApiImplicitParams({ + @ApiImplicitParam(name = "tableName", required = true, example = "yudao", dataTypeClass = String.class), + @ApiImplicitParam(name = "tableComment", required = true, example = "芋道", dataTypeClass = Long.class) + }) +// @PreAuthorize("@ss.hasPermi('tool:gen:list')") TODO 权限 + public CommonResult> getSchemaTableList( + @RequestParam(value = "tableName", required = false) String tableName, + @RequestParam(value = "tableComment", required = false) String tableComment) { + // 获得数据库自带的表定义列表 + List schemaTables = codegenService.getSchemaTableList(tableName, tableComment); + // 移除在 Codegen 中,已经存在的 + + return null; + } + @ApiOperation("获得表定义分页") @GetMapping("/table/page") // TODO 权限 @PreAuthorize("@ss.hasPermi('tool:gen:list')") diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/controller/codegen/vo/table/ToolSchemaTableRespVO.java b/src/main/java/cn/iocoder/dashboard/modules/tool/controller/codegen/vo/table/ToolSchemaTableRespVO.java new file mode 100644 index 000000000..4b724a1f3 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/tool/controller/codegen/vo/table/ToolSchemaTableRespVO.java @@ -0,0 +1,25 @@ +package cn.iocoder.dashboard.modules.tool.controller.codegen.vo.table; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.util.Date; + +@ApiModel("数据字典的表定义 Response VO") +@Data +public class ToolSchemaTableRespVO { + + @ApiModelProperty(value = "数据库", required = true, example = "yudao") + private String tableSchema; + + @ApiModelProperty(value = "表名称", required = true, example = "yuanma") + private String tableName; + + @ApiModelProperty(value = "表描述", required = true, example = "芋道源码") + private String tableComment; + + @ApiModelProperty(value = "创建时间", required = true) + private Date createTime; + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/convert/codegen/ToolCodegenConvert.java b/src/main/java/cn/iocoder/dashboard/modules/tool/convert/codegen/ToolCodegenConvert.java index eb8ad9b73..175076eff 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/tool/convert/codegen/ToolCodegenConvert.java +++ b/src/main/java/cn/iocoder/dashboard/modules/tool/convert/codegen/ToolCodegenConvert.java @@ -8,8 +8,8 @@ import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.column.ToolCodege import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.table.ToolCodegenTableRespVO; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColumnDO; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO; -import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolInformationSchemaColumnDO; -import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolInformationSchemaTableDO; +import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaColumnDO; +import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaTableDO; import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; @@ -24,11 +24,11 @@ public interface ToolCodegenConvert { // ========== ToolInformationSchemaTableDO 和 ToolInformationSchemaColumnDO 相关 ========== - ToolCodegenTableDO convert(ToolInformationSchemaTableDO bean); + ToolCodegenTableDO convert(ToolSchemaTableDO bean); - List convertList(List list); + List convertList(List list); - ToolCodegenTableRespVO convert(ToolInformationSchemaColumnDO bean); + ToolCodegenTableRespVO convert(ToolSchemaColumnDO bean); // ========== ToolCodegenTableDO 相关 ========== diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/dal/dataobject/codegen/ToolInformationSchemaColumnDO.java b/src/main/java/cn/iocoder/dashboard/modules/tool/dal/dataobject/codegen/ToolSchemaColumnDO.java similarity index 96% rename from src/main/java/cn/iocoder/dashboard/modules/tool/dal/dataobject/codegen/ToolInformationSchemaColumnDO.java rename to src/main/java/cn/iocoder/dashboard/modules/tool/dal/dataobject/codegen/ToolSchemaColumnDO.java index 93be410a2..d20a9c86b 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/tool/dal/dataobject/codegen/ToolInformationSchemaColumnDO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/tool/dal/dataobject/codegen/ToolSchemaColumnDO.java @@ -13,7 +13,7 @@ import lombok.Data; @TableName(value = "information_schema.columns", autoResultMap = true) @Data @Builder -public class ToolInformationSchemaColumnDO { +public class ToolSchemaColumnDO { /** * 表名称 diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/dal/dataobject/codegen/ToolInformationSchemaTableDO.java b/src/main/java/cn/iocoder/dashboard/modules/tool/dal/dataobject/codegen/ToolSchemaTableDO.java similarity index 93% rename from src/main/java/cn/iocoder/dashboard/modules/tool/dal/dataobject/codegen/ToolInformationSchemaTableDO.java rename to src/main/java/cn/iocoder/dashboard/modules/tool/dal/dataobject/codegen/ToolSchemaTableDO.java index 61b6050d1..e16d91396 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/tool/dal/dataobject/codegen/ToolInformationSchemaTableDO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/tool/dal/dataobject/codegen/ToolSchemaTableDO.java @@ -14,7 +14,7 @@ import java.util.Date; @TableName(value = "information_schema.tables", autoResultMap = true) @Data @Builder -public class ToolInformationSchemaTableDO { +public class ToolSchemaTableDO { /** * 数据库 diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaTableMapper.java b/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaTableMapper.java deleted file mode 100644 index 64a6a3531..000000000 --- a/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaTableMapper.java +++ /dev/null @@ -1,21 +0,0 @@ -package cn.iocoder.dashboard.modules.tool.dal.mysql.coegen; - -import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX; -import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolInformationSchemaTableDO; -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import org.apache.ibatis.annotations.Mapper; - -import java.util.List; - -@Mapper -public interface ToolInformationSchemaTableMapper extends BaseMapperX { - - default List selectListByTableSchema(String tableSchema) { - return selectList(new QueryWrapper().eq("table_schema", tableSchema)); - } - - default ToolInformationSchemaTableDO selectByTableName(String tableName) { - return selectOne(new QueryWrapper().eq("table_name", tableName)); - } - -} diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaColumnMapper.java b/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolSchemaColumnMapper.java similarity index 54% rename from src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaColumnMapper.java rename to src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolSchemaColumnMapper.java index 8fa299544..a891e75db 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaColumnMapper.java +++ b/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolSchemaColumnMapper.java @@ -1,17 +1,17 @@ package cn.iocoder.dashboard.modules.tool.dal.mysql.coegen; import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX; -import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolInformationSchemaColumnDO; +import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaColumnDO; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper -public interface ToolInformationSchemaColumnMapper extends BaseMapperX { +public interface ToolSchemaColumnMapper extends BaseMapperX { - default List selectListByTableName(String tableName) { - return selectList(new QueryWrapper().eq("table_name", tableName) + default List selectListByTableName(String tableName) { + return selectList(new QueryWrapper().eq("table_name", tableName) .orderByAsc("ordinal_position")); } diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolSchemaTableMapper.java b/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolSchemaTableMapper.java new file mode 100644 index 000000000..26de951f8 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolSchemaTableMapper.java @@ -0,0 +1,29 @@ +package cn.iocoder.dashboard.modules.tool.dal.mysql.coegen; + +import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX; +import cn.iocoder.dashboard.framework.mybatis.core.query.QueryWrapperX; +import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaTableDO; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import org.apache.ibatis.annotations.Mapper; + +import java.util.Collection; +import java.util.List; + +@Mapper +public interface ToolSchemaTableMapper extends BaseMapperX { + + default List selectList(Collection tableSchemas, String tableName, String tableComment) { + return selectList(new QueryWrapperX().in("table_schema", tableSchemas) + .eqIfPresent("table_name", tableName) + .eqIfPresent("table_comment", tableComment)); + } + + default List selectListByTableSchema(String tableSchema) { + return selectList(new QueryWrapper().eq("table_schema", tableSchema)); + } + + default ToolSchemaTableDO selectByTableName(String tableName) { + return selectOne(new QueryWrapper().eq("table_name", tableName)); + } + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/ToolCodegenService.java b/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/ToolCodegenService.java index eb3a401e3..4ab72c889 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/ToolCodegenService.java +++ b/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/ToolCodegenService.java @@ -5,6 +5,7 @@ import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.ToolCodegenUpdate import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.table.ToolCodegenTablePageReqVO; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColumnDO; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO; +import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaTableDO; import java.util.List; import java.util.Map; @@ -63,4 +64,13 @@ public interface ToolCodegenService { */ Map generationCodes(Long tableId); + /** + * 获得数据库自带的表定义列表 + * + * @param tableName 表名称 + * @param tableComment 表描述 + * @return 表定义列表 + */ + List getSchemaTableList(String tableName, String tableComment); + } diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenBuilder.java b/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenBuilder.java index 813dcfac0..9d76d72a4 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenBuilder.java +++ b/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenBuilder.java @@ -6,8 +6,8 @@ import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; import cn.iocoder.dashboard.modules.tool.convert.codegen.ToolCodegenConvert; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColumnDO; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO; -import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolInformationSchemaColumnDO; -import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolInformationSchemaTableDO; +import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaColumnDO; +import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaTableDO; import cn.iocoder.dashboard.modules.tool.enums.codegen.ToolCodegenColumnHtmlTypeEnum; import cn.iocoder.dashboard.modules.tool.enums.codegen.ToolCodegenColumnListConditionEnum; import cn.iocoder.dashboard.modules.tool.enums.codegen.ToolCodegenTemplateTypeEnum; @@ -21,8 +21,8 @@ import static cn.hutool.core.text.CharSequenceUtil.*; /** * 代码生成器的 Builder,负责: - * 1. 将数据库的表 {@link ToolInformationSchemaTableDO} 定义,构建成 {@link ToolCodegenTableDO} - * 2. 将数据库的列 {@link ToolInformationSchemaColumnDO} 构定义,建成 {@link ToolCodegenColumnDO} + * 1. 将数据库的表 {@link ToolSchemaTableDO} 定义,构建成 {@link ToolCodegenTableDO} + * 2. 将数据库的列 {@link ToolSchemaColumnDO} 构定义,建成 {@link ToolCodegenColumnDO} */ @Component public class ToolCodegenBuilder { @@ -112,7 +112,7 @@ public class ToolCodegenBuilder { LIST_OPERATION_RESULT_EXCLUDE_COLUMN.remove("createTime"); // 创建时间,还是需要返回的 } - public ToolCodegenTableDO buildTable(ToolInformationSchemaTableDO schemaTable) { + public ToolCodegenTableDO buildTable(ToolSchemaTableDO schemaTable) { ToolCodegenTableDO table = ToolCodegenConvert.INSTANCE.convert(schemaTable); initTableDefault(table); return table; @@ -135,7 +135,7 @@ public class ToolCodegenBuilder { table.setTemplateType(ToolCodegenTemplateTypeEnum.CRUD.getType()); } - public List buildColumns(List schemaColumns) { + public List buildColumns(List schemaColumns) { List columns = ToolCodegenConvert.INSTANCE.convertList(schemaColumns); columns.forEach(this::initColumnDefault); return columns; diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java b/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java index cfe79c207..185001451 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java +++ b/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java @@ -9,6 +9,7 @@ import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil; import cn.iocoder.dashboard.common.pojo.CommonResult; import cn.iocoder.dashboard.common.pojo.PageParam; import cn.iocoder.dashboard.common.pojo.PageResult; +import cn.iocoder.dashboard.framework.codegen.config.CodegenProperties; import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.dashboard.framework.mybatis.core.query.QueryWrapperX; @@ -19,6 +20,7 @@ import cn.iocoder.dashboard.util.date.DateUtils; import com.google.common.collect.Maps; import org.springframework.stereotype.Component; +import javax.annotation.PostConstruct; import javax.annotation.Resource; import java.util.HashMap; import java.util.LinkedHashMap; @@ -45,18 +47,30 @@ public class ToolCodegenEngine { */ private static final Map TEMPLATES = MapUtil.builder(new LinkedHashMap<>()) // 有序 // Java - .put("codegen/java/controller/controller.vm", "java/${basePackage}/${table.moduleName}/controller/${table.businessName}/${table.className}Controller.java") - .put("codegen/java/controller/vo/baseVO.vm", "java/${basePackage}/${table.moduleName}/controller/${table.businessName}/vo/${table.className}BaseVO.java") - .put("codegen/java/controller/vo/createReqVO.vm", "java/${basePackage}/${table.moduleName}/controller/${table.businessName}/vo/${table.className}CreateReqVO.java") - .put("codegen/java/controller/vo/pageReqVO.vm", "java/${basePackage}/${table.moduleName}/controller/${table.businessName}/vo/${table.className}PageReqVO.java") - .put("codegen/java/controller/vo/respVO.vm", "java/${basePackage}/${table.moduleName}/controller/${table.businessName}/vo/${table.className}RespVO.java") - .put("codegen/java/controller/vo/updateReqVO.vm", "java/${basePackage}/${table.moduleName}/controller/${table.businessName}/vo/${table.className}UpdateReqVO.java") - .put("codegen/java/convert/convert.vm", "java/${basePackage}/${table.moduleName}/convert/${table.businessName}/${table.className}Convert.java") - .put("codegen/java/dal/do.vm", "java/${basePackage}/${table.moduleName}/dal/dataobject/${table.businessName}/${table.className}DO.java") - .put("codegen/java/dal/mapper.vm", "java/${basePackage}/${table.moduleName}/dal/mysql/${table.businessName}/${table.className}Mapper.java") - .put("codegen/java/enums/errorcode.vm", "java/${basePackage}/${table.moduleName}/enums/${simpleModuleName_upperFirst}ErrorCodeConstants.java") - .put("codegen/java/service/service.vm", "java/${basePackage}/${table.moduleName}/service/${table.businessName}/${table.className}Service.java") - .put("codegen/java/service/serviceImpl.vm", "java/${basePackage}/${table.moduleName}/service/${table.businessName}/impl/${table.className}ServiceImpl.java") + .put(javaTemplatePath("controller/controller"), + javaFilePath("controller/${table.businessName}/${table.className}Controller")) + .put(javaTemplatePath("controller/vo/baseVO"), + javaFilePath("controller/${table.businessName}/vo/${table.className}BaseVO")) + .put(javaTemplatePath("controller/vo/createReqVO"), + javaFilePath("controller/${table.businessName}/vo/${table.className}CreateReqVO")) + .put(javaTemplatePath("controller/vo/pageReqVO"), + javaFilePath("controller/${table.businessName}/vo/${table.className}PageReqVO")) + .put(javaTemplatePath("controller/vo/respVO"), + javaFilePath("controller/${table.businessName}/vo/${table.className}RespVO")) + .put(javaTemplatePath("controller/vo/updateReqVO"), + javaFilePath("controller/${table.businessName}/vo/${table.className}UpdateReqVO")) + .put(javaTemplatePath("convert/convert"), + javaFilePath("convert/${table.businessName}/${table.className}Convert")) + .put(javaTemplatePath("dal/do"), + javaFilePath("dal/dataobject/${table.businessName}/${table.className}DO")) + .put(javaTemplatePath("dal/mapper"), + javaFilePath("dal/mysql/${table.businessName}/${table.className}Mapper")) + .put(javaTemplatePath("enums/errorcode"), + javaFilePath("enums/${simpleModuleName_upperFirst}ErrorCodeConstants")) + .put(javaTemplatePath("service/service"), + javaFilePath("service/${table.businessName}/${table.className}Service")) + .put(javaTemplatePath("service/serviceImpl"), + javaFilePath("service/${table.businessName}/impl/${table.className}ServiceImpl")) // Vue // SQL .build(); @@ -64,6 +78,9 @@ public class ToolCodegenEngine { @Resource private ToolCodegenBuilder codegenBuilder; + @Resource + private CodegenProperties codegenProperties; + /** * 模板引擎,由 hutool 实现 */ @@ -78,13 +95,12 @@ public class ToolCodegenEngine { TemplateConfig config = new TemplateConfig(); config.setResourceMode(TemplateConfig.ResourceMode.CLASSPATH); this.templateEngine = TemplateUtil.createEngine(config); - // 初始化基础 bindingMap - initGlobalBindingMap(); } + @PostConstruct private void initGlobalBindingMap() { // 全局配置 - globalBindingMap.put("basePackage", "cn.iocoder.dashboard.modules"); // TODO 基础包, 抽成参数 + globalBindingMap.put("basePackage", codegenProperties.getBasePackage()); // 全局 Java Bean globalBindingMap.put("CommonResultClassName", CommonResult.class.getName()); globalBindingMap.put("PageResultClassName", PageResult.class.getName()); @@ -137,4 +153,12 @@ public class ToolCodegenEngine { return filePath; } + private static String javaTemplatePath(String path) { + return "codegen/java/" + path + ".vm"; + } + + private static String javaFilePath(String path) { + return "java/${basePackage}/${table.moduleName}/" + path + ".java"; + } + } diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenServiceImpl.java index 17f052189..dec893d84 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenServiceImpl.java @@ -2,17 +2,18 @@ package cn.iocoder.dashboard.modules.tool.service.codegen.impl; import cn.hutool.core.collection.CollUtil; import cn.iocoder.dashboard.common.pojo.PageResult; +import cn.iocoder.dashboard.framework.codegen.config.CodegenProperties; import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.ToolCodegenUpdateReqVO; import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.table.ToolCodegenTablePageReqVO; import cn.iocoder.dashboard.modules.tool.convert.codegen.ToolCodegenConvert; -import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolCodegenColumnMapper; -import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolCodegenTableMapper; -import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolInformationSchemaColumnMapper; -import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolInformationSchemaTableMapper; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColumnDO; import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO; -import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolInformationSchemaColumnDO; -import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolInformationSchemaTableDO; +import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaColumnDO; +import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaTableDO; +import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolCodegenColumnMapper; +import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolCodegenTableMapper; +import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolSchemaColumnMapper; +import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolSchemaTableMapper; import cn.iocoder.dashboard.modules.tool.service.codegen.ToolCodegenService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -30,9 +31,9 @@ import java.util.Map; public class ToolCodegenServiceImpl implements ToolCodegenService { @Resource - private ToolInformationSchemaTableMapper informationSchemaTableMapper; + private ToolSchemaTableMapper schemaTableMapper; @Resource - private ToolInformationSchemaColumnMapper informationSchemaColumnMapper; + private ToolSchemaColumnMapper schemaColumnMapper; @Resource private ToolCodegenTableMapper codegenTableMapper; @Resource @@ -43,15 +44,18 @@ public class ToolCodegenServiceImpl implements ToolCodegenService { @Resource private ToolCodegenEngine codegenEngine; + @Resource + private CodegenProperties codegenProperties; + @Override @Transactional public Long createCodegen(String tableName) { // 从数据库中,获得数据库表结构 - ToolInformationSchemaTableDO schemaTable = informationSchemaTableMapper.selectByTableName(tableName); + ToolSchemaTableDO schemaTable = schemaTableMapper.selectByTableName(tableName); if (schemaTable == null) { throw new RuntimeException(""); // TODO } - List schemaColumns = informationSchemaColumnMapper.selectListByTableName(tableName); + List schemaColumns = schemaColumnMapper.selectListByTableName(tableName); if (CollUtil.isEmpty(schemaColumns)) { throw new RuntimeException(""); // TODO } @@ -119,4 +123,9 @@ public class ToolCodegenServiceImpl implements ToolCodegenService { return codegenEngine.execute(table, columns); } + @Override + public List getSchemaTableList(String tableName, String tableComment) { + return schemaTableMapper.selectList(codegenProperties.getDbSchemas(), tableName, tableComment); + } + } diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 2499cf662..f4e255c69 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -44,10 +44,12 @@ spring: # 芋道配置项,设置当前项目所有自定义的配置 yudao: - version: 1.0.0 + info: + version: 1.0.0 + base-package: cn.iocoder.dashboard web: api-prefix: /api - controller-package: cn.iocoder.dashboard + controller-package: ${yudao.info.base-package} security: token-header: Authorization token-secret: abcdefghijklmnopqrstuvwxyz @@ -66,6 +68,9 @@ yudao: height: 60 file: base-path: http://127.0.0.1:1024/api/file/get/ + codegen: + base-package: ${yudao.info.base-package}.modules + db-schemas: ruoyi-vue-pro # Apollo 配置中心 apollo: @@ -89,7 +94,7 @@ mybatis-plus: logic-delete-value: 1 # 逻辑已删除值(默认为 1) logic-not-delete-value: 0 # 逻辑未删除值(默认为 0) mapper-locations: classpath*:mapper/*.xml - type-aliases-package: cn.iocoder.dashboard.modules.*.dal.mysql.dataobject + type-aliases-package: ${yudao.info.base-package}.modules.*.dal.dataobject # Actuator 监控端点的配置项 management: diff --git a/src/main/resources/banner.txt b/src/main/resources/banner.txt index d4e3b433b..39a441d7d 100644 --- a/src/main/resources/banner.txt +++ b/src/main/resources/banner.txt @@ -1,25 +1,17 @@ 芋道源码 http://www.iocoder.cn -Application Version: ${yudao.version} +Application Version: ${yudao.info.version} Spring Boot Version: ${spring-boot.version} -//////////////////////////////////////////////////////////////////// -// _ooOoo_ // -// o8888888o // -// 88" . "88 // -// (| ^_^ |) // -// O\ = /O // -// ____/`---'\____ // -// .' \\| |// `. // -// / \\||| : |||// \ // -// / _||||| -:- |||||- \ // -// | | \\\ - /// | | // -// | \_| ''\---/'' | | // -// \ .-\__ `-` ___/-. / // -// ___`. .' /--.--\ `. . ___ // -// ."" '< `.___\_<|>_/___.' >'"". // -// | | : `- \`.;`\ _ /`;.`/ - ` : | | // -// \ \ `-. \_ __\ /__ _/ .-` / / // -// ========`-.____`-.___\_____/___.-`____.-'======== // -// `=---=' // -// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // -// 佛祖保佑 永不宕机 永无BUG // -//////////////////////////////////////////////////////////////////// + +.__ __. ______ .______ __ __ _______ +| \ | | / __ \ | _ \ | | | | / _____| +| \| | | | | | | |_) | | | | | | | __ +| . ` | | | | | | _ < | | | | | | |_ | +| |\ | | `--' | | |_) | | `--' | | |__| | +|__| \__| \______/ |______/ \______/ \______| + +███╗ ██╗ ██████╗ ██████╗ ██╗ ██╗ ██████╗ +████╗ ██║██╔═══██╗ ██╔══██╗██║ ██║██╔════╝ +██╔██╗ ██║██║ ██║ ██████╔╝██║ ██║██║ ███╗ +██║╚██╗██║██║ ██║ ██╔══██╗██║ ██║██║ ██║ +██║ ╚████║╚██████╔╝ ██████╔╝╚██████╔╝╚██████╔╝ +╚═╝ ╚═══╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚═════╝ diff --git a/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaColumnMapperTest.java b/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaColumnMapperTest.java index adad7d177..d1569ca86 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaColumnMapperTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaColumnMapperTest.java @@ -1,7 +1,7 @@ package cn.iocoder.dashboard.modules.tool.dal.mysql.coegen; import cn.iocoder.dashboard.TestApplication; -import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolInformationSchemaColumnDO; +import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaColumnDO; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @@ -15,11 +15,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue; public class ToolInformationSchemaColumnMapperTest { @Resource - private ToolInformationSchemaColumnMapper toolInformationSchemaColumnMapper; + private ToolSchemaColumnMapper toolInformationSchemaColumnMapper; @Test public void testSelectListByTableName() { - List columns = toolInformationSchemaColumnMapper + List columns = toolInformationSchemaColumnMapper .selectListByTableName("inf_config"); assertTrue(columns.size() > 0); } diff --git a/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaTableMapperTest.java b/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaTableMapperTest.java index 962a86234..a22635f52 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaTableMapperTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/tool/dal/mysql/coegen/ToolInformationSchemaTableMapperTest.java @@ -1,7 +1,7 @@ package cn.iocoder.dashboard.modules.tool.dal.mysql.coegen; import cn.iocoder.dashboard.TestApplication; -import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolInformationSchemaTableDO; +import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaTableDO; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; @@ -14,11 +14,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue; class ToolInformationSchemaTableMapperTest { @Resource - private ToolInformationSchemaTableMapper toolInformationSchemaTableMapper; + private ToolSchemaTableMapper toolInformationSchemaTableMapper; @Test public void tstSelectListByTableSchema() { - List tables = toolInformationSchemaTableMapper + List tables = toolInformationSchemaTableMapper .selectListByTableSchema("ruoyi-vue-pro"); assertTrue(tables.size() > 0); }