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