From 688ed8d15923ebf87c7ab3f4416741ce7c2b6371 Mon Sep 17 00:00:00 2001 From: "Ray.Hao" <1490493387@qq.com> Date: Wed, 11 Sep 2024 23:48:41 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=AE=8C=E5=96=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=94=9F=E6=88=90=E6=8E=A5=E5=8F=A3=E5=92=8C=E5=A4=9A?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=BA=90=E5=88=87=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/config/CodegenProperties.java | 5 ++ .../codegen/controller/CodegenController.java | 9 +-- .../youlai/codegen/mapper/DatabaseMapper.java | 3 + .../codegen/service/GenConfigService.java | 3 +- .../service/impl/GenConfigServiceImpl.java | 58 +++++++++++++------ .../src/main/resources/bootstrap.yml | 1 + 6 files changed, 55 insertions(+), 24 deletions(-) diff --git a/youlai-codegen/src/main/java/com/youlai/codegen/config/CodegenProperties.java b/youlai-codegen/src/main/java/com/youlai/codegen/config/CodegenProperties.java index d81bcb3fb..7bbdef655 100644 --- a/youlai-codegen/src/main/java/com/youlai/codegen/config/CodegenProperties.java +++ b/youlai-codegen/src/main/java/com/youlai/codegen/config/CodegenProperties.java @@ -85,6 +85,11 @@ public class CodegenProperties { */ private String author; + /** + * 默认包名(e.g. com.youlai.mall) + */ + private String packageName; + /** * 默认模块名(e.g. system) */ diff --git a/youlai-codegen/src/main/java/com/youlai/codegen/controller/CodegenController.java b/youlai-codegen/src/main/java/com/youlai/codegen/controller/CodegenController.java index a765af6a6..ffbba1ef2 100644 --- a/youlai-codegen/src/main/java/com/youlai/codegen/controller/CodegenController.java +++ b/youlai-codegen/src/main/java/com/youlai/codegen/controller/CodegenController.java @@ -65,15 +65,16 @@ public class CodegenController { @Operation(summary = "获取代码生成配置") @GetMapping("/{tableName}/config") public Result getGenConfigFormData( - @Parameter(description = "表名", example = "sys_user") @PathVariable String tableName + @Parameter(description = "表名", example = "sys_user") @PathVariable String tableName, + @Parameter(description = "数据源", example = "youlai_system") @RequestParam String datasourceKey ) { - GenConfigForm formData = genConfigService.getGenConfigFormData(tableName); + GenConfigForm formData = genConfigService.getGenConfigFormData(tableName,datasourceKey); return Result.success(formData); } @Operation(summary = "保存代码生成配置") @PostMapping("/config") - public Result saveGenConfig( + public Result saveGenConfig( @RequestBody GenConfigForm formData ) { genConfigService.saveGenConfig(formData); @@ -82,7 +83,7 @@ public class CodegenController { @Operation(summary = "删除代码生成配置") @DeleteMapping("/{tableName}/config") - public Result deleteGenConfig( + public Result deleteGenConfig( @Parameter(description = "表名", example = "sys_user") @PathVariable String tableName ) { genConfigService.deleteGenConfig(tableName); diff --git a/youlai-codegen/src/main/java/com/youlai/codegen/mapper/DatabaseMapper.java b/youlai-codegen/src/main/java/com/youlai/codegen/mapper/DatabaseMapper.java index 8fb390fc1..fe010f08e 100644 --- a/youlai-codegen/src/main/java/com/youlai/codegen/mapper/DatabaseMapper.java +++ b/youlai-codegen/src/main/java/com/youlai/codegen/mapper/DatabaseMapper.java @@ -1,5 +1,6 @@ package com.youlai.codegen.mapper; +import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @@ -24,7 +25,9 @@ public interface DatabaseMapper extends BaseMapper { Page getTablePage(Page page, TablePageQuery queryParams); + List getTableColumns(String tableName); + TableMetaData getTableMetadata(String tableName); } diff --git a/youlai-codegen/src/main/java/com/youlai/codegen/service/GenConfigService.java b/youlai-codegen/src/main/java/com/youlai/codegen/service/GenConfigService.java index 846c6c013..5675fa9c1 100644 --- a/youlai-codegen/src/main/java/com/youlai/codegen/service/GenConfigService.java +++ b/youlai-codegen/src/main/java/com/youlai/codegen/service/GenConfigService.java @@ -16,9 +16,10 @@ public interface GenConfigService extends IService { * 获取代码生成配置 * * @param tableName 表名 + * @param datasourceKey 数据源Key * @return */ - GenConfigForm getGenConfigFormData(String tableName); + GenConfigForm getGenConfigFormData(String tableName,String datasourceKey); /** * 保存代码生成配置 diff --git a/youlai-codegen/src/main/java/com/youlai/codegen/service/impl/GenConfigServiceImpl.java b/youlai-codegen/src/main/java/com/youlai/codegen/service/impl/GenConfigServiceImpl.java index 88a7ef5f2..2b2300400 100644 --- a/youlai-codegen/src/main/java/com/youlai/codegen/service/impl/GenConfigServiceImpl.java +++ b/youlai-codegen/src/main/java/com/youlai/codegen/service/impl/GenConfigServiceImpl.java @@ -3,6 +3,7 @@ package com.youlai.codegen.service.impl; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.lang.Assert; import cn.hutool.core.util.StrUtil; +import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.youlai.common.core.exception.BusinessException; @@ -55,20 +56,17 @@ public class GenConfigServiceImpl extends ServiceImpl(GenConfig.class) - .eq(GenConfig::getTableName, tableName) - .last("LIMIT 1") + GenConfig genConfig = this.getOne(new LambdaQueryWrapper<>(GenConfig.class) + .eq(GenConfig::getTableName, tableName) ); // 是否有代码生成配置 boolean hasGenConfig = genConfig != null; - // 如果没有代码生成配置,则根据表的元数据生成默认配置 if (genConfig == null) { - TableMetaData tableMetadata = databaseMapper.getTableMetadata(tableName); + TableMetaData tableMetadata = this.getTableMetadata(tableName, datasourceKey); Assert.isTrue(tableMetadata != null, "未找到表元数据"); genConfig = new GenConfig(); @@ -82,24 +80,21 @@ public class GenConfigServiceImpl extends ServiceImpl genFieldConfigs = new ArrayList<>(); // 获取表的列 - List tableColumns = databaseMapper.getTableColumns(tableName); + List tableColumns = this.getTableColumns(tableName, datasourceKey); if (CollectionUtil.isNotEmpty(tableColumns)) { // 查询字段生成配置 - List fieldConfigList = genFieldConfigService.list( - new LambdaQueryWrapper() - .eq(GenFieldConfig::getConfigId, genConfig.getId()) - .orderByAsc(GenFieldConfig::getFieldSort) - ); + List fieldConfigList = this.getGenFieldConfigs(tableName); Integer maxSort = fieldConfigList.stream() .map(GenFieldConfig::getFieldSort) .filter(Objects::nonNull) @@ -129,8 +124,10 @@ public class GenConfigServiceImpl extends ServiceImpl getGenFieldConfigs(String tableName) { + GenConfig genConfig = this.getOne(new LambdaQueryWrapper<>(GenConfig.class) + .eq(GenConfig::getTableName, tableName) + ); + if (genConfig == null) { + return null; + } + return genFieldConfigService.list(new LambdaQueryWrapper() + .eq(GenFieldConfig::getConfigId, genConfig.getId()) + .orderByAsc(GenFieldConfig::getFieldSort) + ); + } + + @DS("#datasourceKey") + public TableMetaData getTableMetadata(String tableName, String datasourceKey) { + return databaseMapper.getTableMetadata(tableName); + } + + @DS("#datasourceKey") + public List getTableColumns(String tableName, String datasourceKey) { + return databaseMapper.getTableColumns(tableName); + } /** * 创建默认字段配置 diff --git a/youlai-codegen/src/main/resources/bootstrap.yml b/youlai-codegen/src/main/resources/bootstrap.yml index 704100b7b..62e6fd543 100644 --- a/youlai-codegen/src/main/resources/bootstrap.yml +++ b/youlai-codegen/src/main/resources/bootstrap.yml @@ -18,6 +18,7 @@ codegen: # 默认配置 defaultConfig: author: youlaitech + packageName: com.youlai.mall moduleName: system # 排除数据表 excludeTables: