mirror of
https://gitee.com/youlaitech/youlai-mall.git
synced 2025-01-04 01:52:21 +08:00
refactor: 完善代码生成接口和多数据源切换
This commit is contained in:
parent
daec9f8542
commit
688ed8d159
@ -85,6 +85,11 @@ public class CodegenProperties {
|
|||||||
*/
|
*/
|
||||||
private String author;
|
private String author;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认包名(e.g. com.youlai.mall)
|
||||||
|
*/
|
||||||
|
private String packageName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认模块名(e.g. system)
|
* 默认模块名(e.g. system)
|
||||||
*/
|
*/
|
||||||
|
@ -65,15 +65,16 @@ public class CodegenController {
|
|||||||
@Operation(summary = "获取代码生成配置")
|
@Operation(summary = "获取代码生成配置")
|
||||||
@GetMapping("/{tableName}/config")
|
@GetMapping("/{tableName}/config")
|
||||||
public Result<GenConfigForm> getGenConfigFormData(
|
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);
|
return Result.success(formData);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(summary = "保存代码生成配置")
|
@Operation(summary = "保存代码生成配置")
|
||||||
@PostMapping("/config")
|
@PostMapping("/config")
|
||||||
public Result<?> saveGenConfig(
|
public Result<Void> saveGenConfig(
|
||||||
@RequestBody GenConfigForm formData
|
@RequestBody GenConfigForm formData
|
||||||
) {
|
) {
|
||||||
genConfigService.saveGenConfig(formData);
|
genConfigService.saveGenConfig(formData);
|
||||||
@ -82,7 +83,7 @@ public class CodegenController {
|
|||||||
|
|
||||||
@Operation(summary = "删除代码生成配置")
|
@Operation(summary = "删除代码生成配置")
|
||||||
@DeleteMapping("/{tableName}/config")
|
@DeleteMapping("/{tableName}/config")
|
||||||
public Result<?> deleteGenConfig(
|
public Result<Void> deleteGenConfig(
|
||||||
@Parameter(description = "表名", example = "sys_user") @PathVariable String tableName
|
@Parameter(description = "表名", example = "sys_user") @PathVariable String tableName
|
||||||
) {
|
) {
|
||||||
genConfigService.deleteGenConfig(tableName);
|
genConfigService.deleteGenConfig(tableName);
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.youlai.codegen.mapper;
|
package com.youlai.codegen.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
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);
|
Page<TablePageVO> getTablePage(Page<TablePageVO> page, TablePageQuery queryParams);
|
||||||
|
|
||||||
|
|
||||||
List<ColumnMetaData> getTableColumns(String tableName);
|
List<ColumnMetaData> getTableColumns(String tableName);
|
||||||
|
|
||||||
|
|
||||||
TableMetaData getTableMetadata(String tableName);
|
TableMetaData getTableMetadata(String tableName);
|
||||||
}
|
}
|
||||||
|
@ -16,9 +16,10 @@ public interface GenConfigService extends IService<GenConfig> {
|
|||||||
* 获取代码生成配置
|
* 获取代码生成配置
|
||||||
*
|
*
|
||||||
* @param tableName 表名
|
* @param tableName 表名
|
||||||
|
* @param datasourceKey 数据源Key
|
||||||
* @return
|
* @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.collection.CollectionUtil;
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.util.StrUtil;
|
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.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.youlai.common.core.exception.BusinessException;
|
import com.youlai.common.core.exception.BusinessException;
|
||||||
@ -55,20 +56,17 @@ public class GenConfigServiceImpl extends ServiceImpl<GenConfigMapper, GenConfig
|
|||||||
* @return 代码生成配置
|
* @return 代码生成配置
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public GenConfigForm getGenConfigFormData(String tableName) {
|
public GenConfigForm getGenConfigFormData(String tableName, String datasourceKey) {
|
||||||
// 查询表生成配置
|
// 查询表生成配置
|
||||||
GenConfig genConfig = this.getOne(
|
GenConfig genConfig = this.getOne(new LambdaQueryWrapper<>(GenConfig.class)
|
||||||
new LambdaQueryWrapper<>(GenConfig.class)
|
|
||||||
.eq(GenConfig::getTableName, tableName)
|
.eq(GenConfig::getTableName, tableName)
|
||||||
.last("LIMIT 1")
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// 是否有代码生成配置
|
// 是否有代码生成配置
|
||||||
boolean hasGenConfig = genConfig != null;
|
boolean hasGenConfig = genConfig != null;
|
||||||
|
|
||||||
// 如果没有代码生成配置,则根据表的元数据生成默认配置
|
// 如果没有代码生成配置,则根据表的元数据生成默认配置
|
||||||
if (genConfig == null) {
|
if (genConfig == null) {
|
||||||
TableMetaData tableMetadata = databaseMapper.getTableMetadata(tableName);
|
TableMetaData tableMetadata = this.getTableMetadata(tableName, datasourceKey);
|
||||||
Assert.isTrue(tableMetadata != null, "未找到表元数据");
|
Assert.isTrue(tableMetadata != null, "未找到表元数据");
|
||||||
|
|
||||||
genConfig = new GenConfig();
|
genConfig = new GenConfig();
|
||||||
@ -82,24 +80,21 @@ public class GenConfigServiceImpl extends ServiceImpl<GenConfigMapper, GenConfig
|
|||||||
String entityName = StrUtil.toCamelCase(StrUtil.removePrefix(tableName, tableName.split("_")[0]));
|
String entityName = StrUtil.toCamelCase(StrUtil.removePrefix(tableName, tableName.split("_")[0]));
|
||||||
genConfig.setEntityName(entityName);
|
genConfig.setEntityName(entityName);
|
||||||
|
|
||||||
genConfig.setPackageName("com.youlai");
|
// 默认配置
|
||||||
// 默认模块名
|
CodegenProperties.DefaultConfig defaultConfig = codegenProperties.getDefaultConfig();
|
||||||
genConfig.setModuleName(codegenProperties.getDefaultConfig().getModuleName());
|
genConfig.setPackageName(defaultConfig.getPackageName());
|
||||||
genConfig.setAuthor(codegenProperties.getDefaultConfig().getAuthor());
|
genConfig.setModuleName(defaultConfig.getModuleName());
|
||||||
|
genConfig.setAuthor(defaultConfig.getAuthor());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据表的列 + 已经存在的字段生成配置 得到 组合后的字段生成配置
|
// 根据表的列 + 已经存在的字段生成配置 得到 组合后的字段生成配置
|
||||||
List<GenFieldConfig> genFieldConfigs = new ArrayList<>();
|
List<GenFieldConfig> genFieldConfigs = new ArrayList<>();
|
||||||
|
|
||||||
// 获取表的列
|
// 获取表的列
|
||||||
List<ColumnMetaData> tableColumns = databaseMapper.getTableColumns(tableName);
|
List<ColumnMetaData> tableColumns = this.getTableColumns(tableName, datasourceKey);
|
||||||
if (CollectionUtil.isNotEmpty(tableColumns)) {
|
if (CollectionUtil.isNotEmpty(tableColumns)) {
|
||||||
// 查询字段生成配置
|
// 查询字段生成配置
|
||||||
List<GenFieldConfig> fieldConfigList = genFieldConfigService.list(
|
List<GenFieldConfig> fieldConfigList = this.getGenFieldConfigs(tableName);
|
||||||
new LambdaQueryWrapper<GenFieldConfig>()
|
|
||||||
.eq(GenFieldConfig::getConfigId, genConfig.getId())
|
|
||||||
.orderByAsc(GenFieldConfig::getFieldSort)
|
|
||||||
);
|
|
||||||
Integer maxSort = fieldConfigList.stream()
|
Integer maxSort = fieldConfigList.stream()
|
||||||
.map(GenFieldConfig::getFieldSort)
|
.map(GenFieldConfig::getFieldSort)
|
||||||
.filter(Objects::nonNull)
|
.filter(Objects::nonNull)
|
||||||
@ -129,8 +124,10 @@ public class GenConfigServiceImpl extends ServiceImpl<GenConfigMapper, GenConfig
|
|||||||
genFieldConfigs.add(fieldConfig);
|
genFieldConfigs.add(fieldConfig);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//对genFieldConfigs按照fieldSort排序
|
// 对genFieldConfigs按照fieldSort排序
|
||||||
genFieldConfigs = genFieldConfigs.stream().sorted(Comparator.comparing(GenFieldConfig::getFieldSort)).toList();
|
genFieldConfigs = genFieldConfigs.stream()
|
||||||
|
.sorted(Comparator.comparing(GenFieldConfig::getFieldSort))
|
||||||
|
.toList();
|
||||||
GenConfigForm genConfigForm = codegenConverter.toGenConfigForm(genConfig, genFieldConfigs);
|
GenConfigForm genConfigForm = codegenConverter.toGenConfigForm(genConfig, genFieldConfigs);
|
||||||
|
|
||||||
genConfigForm.setFrontendAppName(codegenProperties.getFrontendAppName());
|
genConfigForm.setFrontendAppName(codegenProperties.getFrontendAppName());
|
||||||
@ -138,6 +135,29 @@ public class GenConfigServiceImpl extends ServiceImpl<GenConfigMapper, GenConfig
|
|||||||
return genConfigForm;
|
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:
|
defaultConfig:
|
||||||
author: youlaitech
|
author: youlaitech
|
||||||
|
packageName: com.youlai.mall
|
||||||
moduleName: system
|
moduleName: system
|
||||||
# 排除数据表
|
# 排除数据表
|
||||||
excludeTables:
|
excludeTables:
|
||||||
|
Loading…
Reference in New Issue
Block a user