fix: 代码生成测试问题修复

This commit is contained in:
Ray.Hao 2024-09-12 20:54:57 +08:00
parent 688ed8d159
commit b5930bfac5
11 changed files with 103 additions and 78 deletions

View File

@ -66,9 +66,9 @@ public class CodegenController {
@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 @Parameter(description = "数据源Key", example = "youlai_system") @RequestParam String dsKey
) { ) {
GenConfigForm formData = genConfigService.getGenConfigFormData(tableName,datasourceKey); GenConfigForm formData = genConfigService.getGenConfigFormData(tableName,dsKey);
return Result.success(formData); return Result.success(formData);
} }

View File

@ -25,9 +25,11 @@ public interface DatabaseMapper extends BaseMapper {
Page<TablePageVO> getTablePage(Page<TablePageVO> page, TablePageQuery queryParams); Page<TablePageVO> getTablePage(Page<TablePageVO> page, TablePageQuery queryParams);
@DS("#datasourceKey")
TableMetaData getTableMetadata(String tableName,String datasourceKey);
List<ColumnMetaData> getTableColumns(String tableName); @DS("#datasourceKey")
List<ColumnMetaData> getTableColumns(String tableName,String datasourceKey);
TableMetaData getTableMetadata(String tableName);
} }

View File

@ -1,10 +1,11 @@
package com.youlai.codegen.model.entity; package com.youlai.codegen.model.entity;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.*;
import com.youlai.common.base.BaseEntity;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.time.LocalDateTime;
/** /**
* 代码生成基础配置 * 代码生成基础配置
* *
@ -14,7 +15,13 @@ import lombok.Setter;
@TableName(value = "gen_config") @TableName(value = "gen_config")
@Getter @Getter
@Setter @Setter
public class GenConfig extends BaseEntity { public class GenConfig {
/**
* 主键ID
*/
@TableId(type = IdType.AUTO)
private Long id;
/** /**
* 表名 * 表名
@ -50,4 +57,17 @@ public class GenConfig extends BaseEntity {
* 作者 * 作者
*/ */
private String author; private String author;
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
/**
* 更新时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
} }

View File

@ -1,14 +1,14 @@
package com.youlai.codegen.model.entity; package com.youlai.codegen.model.entity;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.youlai.common.base.BaseEntity;
import com.youlai.codegen.enums.FormTypeEnum; import com.youlai.codegen.enums.FormTypeEnum;
import com.youlai.codegen.enums.QueryTypeEnum; import com.youlai.codegen.enums.QueryTypeEnum;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.time.LocalDateTime;
/** /**
* 字段生成配置实体 * 字段生成配置实体
* *
@ -18,7 +18,13 @@ import lombok.Setter;
@TableName(value = "gen_field_config") @TableName(value = "gen_field_config")
@Getter @Getter
@Setter @Setter
public class GenFieldConfig extends BaseEntity { public class GenFieldConfig {
/**
* 主键ID
*/
@TableId(type = IdType.AUTO)
private Long id;
/** /**
@ -102,4 +108,17 @@ public class GenFieldConfig extends BaseEntity {
* 字典类型 * 字典类型
*/ */
private String dictType; private String dictType;
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
/**
* 更新时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
} }

View File

@ -19,11 +19,11 @@ import java.util.List;
@Setter @Setter
public class TablePageQuery extends BasePageQuery { public class TablePageQuery extends BasePageQuery {
@Schema(description="关键字(表名)") @Schema(description="关键字(表名)",example = "sys_user")
private String keywords; private String keywords;
@Schema(description="数据源Key") @Schema(description="数据源Key",example = "master")
private String datasourceKey; private String dsKey;
/** /**
* 排除的表名 * 排除的表名

View File

@ -133,9 +133,9 @@ public class CodegenServiceImpl implements CodegenService {
previewVO.setFileName(fileName); previewVO.setFileName(fileName);
/* 2. 生成文件路径 */ /* 2. 生成文件路径 */
// 包名com.youlai.boot // 包名com.youlai.mall
String packageName = genConfig.getPackageName(); String packageName = genConfig.getPackageName();
// 模块名system // 模块名mall-order
String moduleName = genConfig.getModuleName(); String moduleName = genConfig.getModuleName();
// 子包名controller // 子包名controller
String subpackageName = templateConfig.getSubpackageName(); String subpackageName = templateConfig.getSubpackageName();

View File

@ -32,7 +32,7 @@ public class DatabaseServiceImpl implements DatabaseService {
* @return 分页结果 * @return 分页结果
*/ */
@Override @Override
@DS("#queryParams.datasourceKey") @DS("#queryParams.dsKey")
public Page<TablePageVO> getTablePage(TablePageQuery queryParams) { public Page<TablePageVO> getTablePage(TablePageQuery queryParams) {
// 设置排除的表 // 设置排除的表
List<String> excludeTables = codegenProperties.getExcludeTables(); List<String> excludeTables = codegenProperties.getExcludeTables();

View File

@ -4,6 +4,7 @@ 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.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
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;
@ -66,9 +67,8 @@ public class GenConfigServiceImpl extends ServiceImpl<GenConfigMapper, GenConfig
boolean hasGenConfig = genConfig != null; boolean hasGenConfig = genConfig != null;
// 如果没有代码生成配置则根据表的元数据生成默认配置 // 如果没有代码生成配置则根据表的元数据生成默认配置
if (genConfig == null) { if (genConfig == null) {
TableMetaData tableMetadata = this.getTableMetadata(tableName, datasourceKey); TableMetaData tableMetadata = databaseMapper.getTableMetadata(tableName, datasourceKey);
Assert.isTrue(tableMetadata != null, "未找到表元数据"); Assert.isTrue(tableMetadata != null, "未找到表元数据");
genConfig = new GenConfig(); genConfig = new GenConfig();
genConfig.setTableName(tableName); genConfig.setTableName(tableName);
@ -91,10 +91,16 @@ public class GenConfigServiceImpl extends ServiceImpl<GenConfigMapper, GenConfig
List<GenFieldConfig> genFieldConfigs = new ArrayList<>(); List<GenFieldConfig> genFieldConfigs = new ArrayList<>();
// 获取表的列 // 获取表的列
List<ColumnMetaData> tableColumns = this.getTableColumns(tableName, datasourceKey); List<ColumnMetaData> tableColumns = databaseMapper.getTableColumns(tableName, datasourceKey);
if (CollectionUtil.isNotEmpty(tableColumns)) { if (CollectionUtil.isNotEmpty(tableColumns)) {
try {
DynamicDataSourceContextHolder.push("master");
// 查询字段生成配置 // 查询字段生成配置
List<GenFieldConfig> fieldConfigList = this.getGenFieldConfigs(tableName); List<GenFieldConfig> fieldConfigList = genFieldConfigService.list(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)
@ -123,6 +129,9 @@ public class GenConfigServiceImpl extends ServiceImpl<GenConfigMapper, GenConfig
} }
genFieldConfigs.add(fieldConfig); genFieldConfigs.add(fieldConfig);
} }
} finally {
DynamicDataSourceContextHolder.poll();
}
} }
// 对genFieldConfigs按照fieldSort排序 // 对genFieldConfigs按照fieldSort排序
genFieldConfigs = genFieldConfigs.stream() genFieldConfigs = genFieldConfigs.stream()
@ -135,30 +144,6 @@ 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);
}
/** /**
* 创建默认字段配置 * 创建默认字段配置
* *

View File

@ -27,7 +27,7 @@ spring:
master: master:
type: com.alibaba.druid.pool.DruidDataSource type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/youlai_generator?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&allowMultiQueries=true url: jdbc:mysql://localhost:3306/youlai_codegen?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&allowMultiQueries=true
username: root username: root
password: 123456 password: 123456
youlai_system: # 系统库 youlai_system: # 系统库

View File

@ -1,6 +1,3 @@
server:
port: 8810
spring: spring:
application: application:
name: youlai-codegen name: youlai-codegen

View File

@ -24,8 +24,10 @@ public class RestTemplateConfig {
@Bean @Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() { public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(5000);//单位 ms // 读取超时时间 5s
factory.setConnectTimeout(5000);//单位 ms factory.setReadTimeout(5000);
// 连接超时时间 5s
factory.setConnectTimeout(5000);
return factory; return factory;
} }