mirror of
https://gitee.com/youlaitech/youlai-mall.git
synced 2024-12-22 20:54:26 +08:00
refactor: 完善代码生成接口和多数据源切换
This commit is contained in:
parent
daec9f8542
commit
688ed8d159
@ -85,6 +85,11 @@ public class CodegenProperties {
|
||||
*/
|
||||
private String author;
|
||||
|
||||
/**
|
||||
* 默认包名(e.g. com.youlai.mall)
|
||||
*/
|
||||
private String packageName;
|
||||
|
||||
/**
|
||||
* 默认模块名(e.g. system)
|
||||
*/
|
||||
|
@ -65,15 +65,16 @@ public class CodegenController {
|
||||
@Operation(summary = "获取代码生成配置")
|
||||
@GetMapping("/{tableName}/config")
|
||||
public Result<GenConfigForm> 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<Void> saveGenConfig(
|
||||
@RequestBody GenConfigForm formData
|
||||
) {
|
||||
genConfigService.saveGenConfig(formData);
|
||||
@ -82,7 +83,7 @@ public class CodegenController {
|
||||
|
||||
@Operation(summary = "删除代码生成配置")
|
||||
@DeleteMapping("/{tableName}/config")
|
||||
public Result<?> deleteGenConfig(
|
||||
public Result<Void> deleteGenConfig(
|
||||
@Parameter(description = "表名", example = "sys_user") @PathVariable String tableName
|
||||
) {
|
||||
genConfigService.deleteGenConfig(tableName);
|
||||
|
@ -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<TablePageVO> getTablePage(Page<TablePageVO> page, TablePageQuery queryParams);
|
||||
|
||||
|
||||
List<ColumnMetaData> getTableColumns(String tableName);
|
||||
|
||||
|
||||
TableMetaData getTableMetadata(String tableName);
|
||||
}
|
||||
|
@ -16,9 +16,10 @@ public interface GenConfigService extends IService<GenConfig> {
|
||||
* 获取代码生成配置
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param datasourceKey 数据源Key
|
||||
* @return
|
||||
*/
|
||||
GenConfigForm getGenConfigFormData(String tableName);
|
||||
GenConfigForm getGenConfigFormData(String tableName,String datasourceKey);
|
||||
|
||||
/**
|
||||
* 保存代码生成配置
|
||||
|
@ -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<GenConfigMapper, GenConfig
|
||||
* @return 代码生成配置
|
||||
*/
|
||||
@Override
|
||||
public GenConfigForm getGenConfigFormData(String tableName) {
|
||||
public GenConfigForm getGenConfigFormData(String tableName, String datasourceKey) {
|
||||
// 查询表生成配置
|
||||
GenConfig genConfig = this.getOne(
|
||||
new LambdaQueryWrapper<>(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<GenConfigMapper, GenConfig
|
||||
String entityName = StrUtil.toCamelCase(StrUtil.removePrefix(tableName, tableName.split("_")[0]));
|
||||
genConfig.setEntityName(entityName);
|
||||
|
||||
genConfig.setPackageName("com.youlai");
|
||||
// 默认模块名
|
||||
genConfig.setModuleName(codegenProperties.getDefaultConfig().getModuleName());
|
||||
genConfig.setAuthor(codegenProperties.getDefaultConfig().getAuthor());
|
||||
// 默认配置
|
||||
CodegenProperties.DefaultConfig defaultConfig = codegenProperties.getDefaultConfig();
|
||||
genConfig.setPackageName(defaultConfig.getPackageName());
|
||||
genConfig.setModuleName(defaultConfig.getModuleName());
|
||||
genConfig.setAuthor(defaultConfig.getAuthor());
|
||||
}
|
||||
|
||||
// 根据表的列 + 已经存在的字段生成配置 得到 组合后的字段生成配置
|
||||
List<GenFieldConfig> genFieldConfigs = new ArrayList<>();
|
||||
|
||||
// 获取表的列
|
||||
List<ColumnMetaData> tableColumns = databaseMapper.getTableColumns(tableName);
|
||||
List<ColumnMetaData> tableColumns = this.getTableColumns(tableName, datasourceKey);
|
||||
if (CollectionUtil.isNotEmpty(tableColumns)) {
|
||||
// 查询字段生成配置
|
||||
List<GenFieldConfig> fieldConfigList = genFieldConfigService.list(
|
||||
new LambdaQueryWrapper<GenFieldConfig>()
|
||||
.eq(GenFieldConfig::getConfigId, genConfig.getId())
|
||||
.orderByAsc(GenFieldConfig::getFieldSort)
|
||||
);
|
||||
List<GenFieldConfig> fieldConfigList = this.getGenFieldConfigs(tableName);
|
||||
Integer maxSort = fieldConfigList.stream()
|
||||
.map(GenFieldConfig::getFieldSort)
|
||||
.filter(Objects::nonNull)
|
||||
@ -129,8 +124,10 @@ public class GenConfigServiceImpl extends ServiceImpl<GenConfigMapper, GenConfig
|
||||
genFieldConfigs.add(fieldConfig);
|
||||
}
|
||||
}
|
||||
//对genFieldConfigs按照fieldSort排序
|
||||
genFieldConfigs = genFieldConfigs.stream().sorted(Comparator.comparing(GenFieldConfig::getFieldSort)).toList();
|
||||
// 对genFieldConfigs按照fieldSort排序
|
||||
genFieldConfigs = genFieldConfigs.stream()
|
||||
.sorted(Comparator.comparing(GenFieldConfig::getFieldSort))
|
||||
.toList();
|
||||
GenConfigForm genConfigForm = codegenConverter.toGenConfigForm(genConfig, genFieldConfigs);
|
||||
|
||||
genConfigForm.setFrontendAppName(codegenProperties.getFrontendAppName());
|
||||
@ -138,6 +135,29 @@ public class GenConfigServiceImpl extends ServiceImpl<GenConfigMapper, GenConfig
|
||||
return genConfigForm;
|
||||
}
|
||||
|
||||
@DS("master")
|
||||
public List<GenFieldConfig> 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<GenFieldConfig>()
|
||||
.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<ColumnMetaData> getTableColumns(String tableName, String datasourceKey) {
|
||||
return databaseMapper.getTableColumns(tableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建默认字段配置
|
||||
|
@ -18,6 +18,7 @@ codegen:
|
||||
# 默认配置
|
||||
defaultConfig:
|
||||
author: youlaitech
|
||||
packageName: com.youlai.mall
|
||||
moduleName: system
|
||||
# 排除数据表
|
||||
excludeTables:
|
||||
|
Loading…
Reference in New Issue
Block a user