🔨 更新生成字段
This commit is contained in:
parent
1ce2a7e210
commit
cadedad259
|
@ -18,6 +18,8 @@ bunny-web.site.key
|
|||
bunny-web.site_bundle.crt
|
||||
bunny-web.site_bundle.pem
|
||||
|
||||
application-prod.yml
|
||||
|
||||
yarn.lock
|
||||
npm-debug.log*
|
||||
.pnpm-error.log*
|
||||
|
|
|
@ -30,12 +30,6 @@ public class GeneratorController {
|
|||
|
||||
private final GeneratorService generatorService;
|
||||
|
||||
/**
|
||||
* 生成代码
|
||||
*
|
||||
* @param dto 生成参数DTO
|
||||
* @return 生成的代码结果,按表名分组
|
||||
*/
|
||||
@Operation(summary = "生成代码", description = "根据SQL或数据库表生成代码")
|
||||
@PostMapping
|
||||
public Result<Map<String, List<GeneratorVo>>> generator(@Valid @RequestBody VmsArgumentDto dto) {
|
||||
|
@ -48,18 +42,12 @@ public class GeneratorController {
|
|||
return Result.success(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 打包代码为ZIP下载
|
||||
*
|
||||
* @param dto 生成参数DTO
|
||||
* @return ZIP文件响应实体
|
||||
*/
|
||||
@Operation(summary = "打包下载", description = "将生成的代码打包为ZIP文件下载")
|
||||
@PostMapping("downloadByZip")
|
||||
public ResponseEntity<byte[]> downloadByZip(@Valid @RequestBody VmsArgumentDto dto) {
|
||||
// 判断当前是使用 SQL语句 生成还是 数据库生成
|
||||
String sql = dto.getSql();
|
||||
|
||||
|
||||
return Strings.isEmpty(sql)
|
||||
? generatorService.downloadByZipByDatabase(dto)
|
||||
: generatorService.downloadByZipBySqL(dto);
|
||||
|
|
|
@ -30,6 +30,16 @@ public class VmsTBaseTemplateGenerator extends AbstractTemplateGenerator {
|
|||
this.dto = dto;
|
||||
this.path = path;
|
||||
this.tableMetaData = tableMetaData;
|
||||
|
||||
// 处理表名称,替换前缀
|
||||
String tableName = tableMetaData.getTableName();
|
||||
String[] prefixes = dto.getTablePrefixes().split("[,,]");
|
||||
for (String prefix : prefixes) {
|
||||
if (tableName.startsWith(prefix)) {
|
||||
String handlerTableName = tableName.replace(prefix, "");
|
||||
tableMetaData.setCleanTableName(handlerTableName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -40,7 +50,7 @@ public class VmsTBaseTemplateGenerator extends AbstractTemplateGenerator {
|
|||
@Override
|
||||
public void addContext(VelocityContext context) {
|
||||
// 当前的表名
|
||||
String tableName = tableMetaData.getTableName();
|
||||
String handlerTableName = tableMetaData.getCleanTableName();
|
||||
// 表的注释内容
|
||||
String comment = tableMetaData.getComment();
|
||||
|
||||
|
@ -61,11 +71,11 @@ public class VmsTBaseTemplateGenerator extends AbstractTemplateGenerator {
|
|||
context.put("package", dto.getPackageName());
|
||||
|
||||
// 将类名称转成小驼峰
|
||||
String lowerCamelCase = MysqlTypeConvertUtil.convertToCamelCase(tableName, false);
|
||||
String lowerCamelCase = MysqlTypeConvertUtil.convertToCamelCase(handlerTableName, false);
|
||||
context.put("classLowercaseName", lowerCamelCase);
|
||||
|
||||
// 将类名称转成大驼峰
|
||||
String upperCameCase = MysqlTypeConvertUtil.convertToCamelCase(tableName, true);
|
||||
String upperCameCase = MysqlTypeConvertUtil.convertToCamelCase(handlerTableName, true);
|
||||
context.put("classUppercaseName", upperCameCase);
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ import lombok.Builder;
|
|||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
|
@ -13,11 +14,14 @@ import java.util.List;
|
|||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Schema(name = "TableMetaData", description = "表信息数据")
|
||||
public class TableMetaData {
|
||||
public class TableMetaData implements Serializable {
|
||||
|
||||
@Schema(name = "tableName", description = "表名")
|
||||
private String tableName;
|
||||
|
||||
@Schema(name = "handlerTableName", description = "处理后的表名称")
|
||||
private String cleanTableName;
|
||||
|
||||
@Schema(name = "comment", description = "注释内容")
|
||||
private String comment;
|
||||
|
||||
|
@ -33,4 +37,5 @@ public class TableMetaData {
|
|||
@Schema(name = "columns", description = "列名称")
|
||||
private List<ColumnMetaData> columns = List.of();
|
||||
|
||||
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
package cn.bunny.service.helper;
|
||||
|
||||
import cn.bunny.domain.dto.VmsArgumentDto;
|
||||
import cn.bunny.utils.MysqlTypeConvertUtil;
|
||||
import com.google.common.base.CaseFormat;
|
||||
|
||||
|
@ -23,36 +22,25 @@ public class VmsGeneratorPathHelper {
|
|||
/**
|
||||
* 处理模板文件路径和命名
|
||||
*
|
||||
* @param dto 生成参数
|
||||
* @param path 原始模板路径
|
||||
* @param tableName 数据库表名
|
||||
* @return 处理后的文件路径
|
||||
*/
|
||||
public static String processVmPath(VmsArgumentDto dto, String path, String tableName) {
|
||||
String className = removeTablePrefixes(dto, tableName);
|
||||
public static String processVmPath(String path, String tableName) {
|
||||
String lowerCamelCase = MysqlTypeConvertUtil.convertToCamelCase(tableName, false);
|
||||
String[] pathParts = path.replace("$className", lowerCamelCase).split("/");
|
||||
|
||||
// 处理文件名
|
||||
pathParts[pathParts.length - 1] = processFilename(
|
||||
pathParts[pathParts.length - 1],
|
||||
className
|
||||
);
|
||||
if (lowerCamelCase != null) {
|
||||
String[] pathParts = path.replace("$className", lowerCamelCase).split("/");
|
||||
// 处理文件名
|
||||
pathParts[pathParts.length - 1] = processFilename(
|
||||
pathParts[pathParts.length - 1],
|
||||
lowerCamelCase
|
||||
);
|
||||
|
||||
return String.join("/", pathParts);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除表前缀
|
||||
*/
|
||||
private static String removeTablePrefixes(VmsArgumentDto dto, String tableName) {
|
||||
String[] prefixes = dto.getTablePrefixes().split("[,,]");
|
||||
for (String prefix : prefixes) {
|
||||
if (tableName.startsWith(prefix)) {
|
||||
return tableName.substring(prefix.length());
|
||||
}
|
||||
return String.join("/", pathParts);
|
||||
}
|
||||
return tableName;
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -56,10 +56,12 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||
*/
|
||||
@Override
|
||||
public Map<String, List<GeneratorVo>> generateCodeBySql(VmsArgumentDto dto) {
|
||||
// 根据Sql语句进行分析表的属性和表列字段
|
||||
String sql = dto.getSql();
|
||||
TableMetaData tableMeta = sqlMetadataProvider.getTableMetadata(sql);
|
||||
List<ColumnMetaData> columns = sqlMetadataProvider.getColumnInfoList(sql);
|
||||
|
||||
// 生成代码
|
||||
List<GeneratorVo> generatorVoList = getGeneratorStream(dto, tableMeta, columns).toList();
|
||||
|
||||
Map<String, List<GeneratorVo>> map = new HashMap<>();
|
||||
|
@ -68,11 +70,23 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据数据库进行生成
|
||||
*
|
||||
* @param dto 生成参数
|
||||
* @return 生成的ZIP文件
|
||||
*/
|
||||
@Override
|
||||
public ResponseEntity<byte[]> downloadByZipByDatabase(VmsArgumentDto dto) {
|
||||
return downloadByZip(dto, this::generateCodeByDatabase);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据Sql语句及逆行生成
|
||||
*
|
||||
* @param dto 生成参数
|
||||
* @return 生成的ZIP文件
|
||||
*/
|
||||
@Override
|
||||
public ResponseEntity<byte[]> downloadByZipBySqL(VmsArgumentDto dto) {
|
||||
return downloadByZip(dto, this::generateCodeBySql);
|
||||
|
@ -117,16 +131,22 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||
* @return 生成器流
|
||||
*/
|
||||
public Stream<GeneratorVo> getGeneratorStream(VmsArgumentDto dto, TableMetaData tableMeta, List<ColumnMetaData> columns) {
|
||||
// 因为这里使用到了并行流,对 tableMeta 操作正常是会拿到修改后的值,但是在并行流会有线程安全问题,所以直接要手动实现深拷贝
|
||||
|
||||
return dto.getPath().parallelStream().map(path -> {
|
||||
// 创建生成模板
|
||||
VmsTBaseTemplateGenerator generator = new VmsTBaseTemplateGenerator(dto, path, tableMeta);
|
||||
|
||||
// 生成好的模板
|
||||
String code = generator.generateCode(tableMeta, columns).toString();
|
||||
String processVmPath = VmsGeneratorPathHelper.processVmPath(path, tableMeta.getCleanTableName());
|
||||
|
||||
return GeneratorVo.builder()
|
||||
.id(UUID.randomUUID().toString())
|
||||
.code(code)
|
||||
.comment(tableMeta.getComment())
|
||||
.tableName(tableMeta.getTableName())
|
||||
.path(VmsGeneratorPathHelper.processVmPath(dto, path, tableMeta.getTableName()))
|
||||
.path(processVmPath)
|
||||
.build();
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue