🔥 删除冗余内容
This commit is contained in:
parent
25f46606fc
commit
464a419d9a
|
@ -39,7 +39,10 @@ public class GeneratorController {
|
||||||
@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) {
|
||||||
Map<String, List<GeneratorVo>> result = Strings.isEmpty(dto.getSql())
|
// 判断当前是使用 SQL语句 生成还是 数据库生成
|
||||||
|
String sql = dto.getSql();
|
||||||
|
|
||||||
|
Map<String, List<GeneratorVo>> result = Strings.isEmpty(sql)
|
||||||
? generatorService.generateCodeByDatabase(dto)
|
? generatorService.generateCodeByDatabase(dto)
|
||||||
: generatorService.generateCodeBySql(dto);
|
: generatorService.generateCodeBySql(dto);
|
||||||
return Result.success(result);
|
return Result.success(result);
|
||||||
|
@ -54,7 +57,10 @@ public class GeneratorController {
|
||||||
@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) {
|
||||||
return Strings.isEmpty(dto.getSql())
|
// 判断当前是使用 SQL语句 生成还是 数据库生成
|
||||||
|
String sql = dto.getSql();
|
||||||
|
|
||||||
|
return Strings.isEmpty(sql)
|
||||||
? generatorService.downloadByZipByDatabase(dto)
|
? generatorService.downloadByZipByDatabase(dto)
|
||||||
: generatorService.downloadByZipBySqL(dto);
|
: generatorService.downloadByZipBySqL(dto);
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,23 +18,23 @@ import java.util.List;
|
||||||
public class VmsArgumentDto {
|
public class VmsArgumentDto {
|
||||||
|
|
||||||
@Schema(name = "author", description = "作者名称")
|
@Schema(name = "author", description = "作者名称")
|
||||||
String author;
|
String author = "";
|
||||||
|
|
||||||
@Schema(name = "packageName", description = "包名称")
|
@Schema(name = "packageName", description = "包名称")
|
||||||
@NotBlank(message = "包名不能为空")
|
@NotBlank(message = "包名不能为空")
|
||||||
String packageName;
|
String packageName;
|
||||||
|
|
||||||
@Schema(name = "requestMapping", description = "requestMapping 名称")
|
@Schema(name = "requestMapping", description = "requestMapping 名称")
|
||||||
String requestMapping;
|
String requestMapping = "";
|
||||||
|
|
||||||
@Schema(name = "tableNames", description = "表名列表")
|
@Schema(name = "tableNames", description = "表名列表")
|
||||||
private List<String> tableNames;
|
private List<String> tableNames;
|
||||||
|
|
||||||
@Schema(name = "simpleDateFormat", description = "时间格式")
|
@Schema(name = "simpleDateFormat", description = "时间格式")
|
||||||
private String simpleDateFormat;
|
private String simpleDateFormat = "yyyy-MM-dd HH:mm:ss";
|
||||||
|
|
||||||
@Schema(name = "tablePrefixes", description = "去除表前缀")
|
@Schema(name = "tablePrefixes", description = "去除表前缀")
|
||||||
private String tablePrefixes;
|
private String tablePrefixes = "";
|
||||||
|
|
||||||
@Schema(name = "path", description = "路径")
|
@Schema(name = "path", description = "路径")
|
||||||
@NotEmpty(message = "表名称不能为空")
|
@NotEmpty(message = "表名称不能为空")
|
||||||
|
|
|
@ -1,70 +0,0 @@
|
||||||
package cn.bunny.service.helper;
|
|
||||||
|
|
||||||
import cn.bunny.core.template.VmsTBaseTemplateGenerator;
|
|
||||||
import cn.bunny.domain.dto.VmsArgumentDto;
|
|
||||||
import cn.bunny.domain.entity.ColumnMetaData;
|
|
||||||
import cn.bunny.domain.entity.TableMetaData;
|
|
||||||
import cn.bunny.domain.vo.GeneratorVo;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 代码生成服务辅助类
|
|
||||||
* 提供生成器和响应实体的构建方法
|
|
||||||
*/
|
|
||||||
public class GeneratorServiceImplHelper {
|
|
||||||
|
|
||||||
private static final String ZIP_FILE_PREFIX = "code-";
|
|
||||||
private static final String ZIP_FILE_SUFFIX = ".zip";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取生成器流
|
|
||||||
*
|
|
||||||
* @param dto 生成参数
|
|
||||||
* @param tableMeta 表元数据
|
|
||||||
* @param columns 列信息
|
|
||||||
* @return 生成器流
|
|
||||||
*/
|
|
||||||
public static Stream<GeneratorVo> getGeneratorStream(VmsArgumentDto dto, TableMetaData tableMeta, List<ColumnMetaData> columns) {
|
|
||||||
return dto.getPath().parallelStream().map(path -> {
|
|
||||||
VmsTBaseTemplateGenerator generator = new VmsTBaseTemplateGenerator(dto, path, tableMeta);
|
|
||||||
String code = generator.generateCode(tableMeta, columns).toString();
|
|
||||||
|
|
||||||
return GeneratorVo.builder()
|
|
||||||
.id(UUID.randomUUID().toString())
|
|
||||||
.code(code)
|
|
||||||
.comment(tableMeta.getComment())
|
|
||||||
.tableName(tableMeta.getTableName())
|
|
||||||
.path(VmsGeneratorPathHelper.processVmPath(dto, path, tableMeta.getTableName()))
|
|
||||||
.build();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构建ZIP下载响应实体
|
|
||||||
*
|
|
||||||
* @param zipBytes ZIP文件字节数组
|
|
||||||
* @return 响应实体
|
|
||||||
*/
|
|
||||||
public static ResponseEntity<byte[]> getResponseEntity(byte[] zipBytes) {
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
|
||||||
headers.add("Content-Disposition", "attachment; filename=" + generateZipFilename());
|
|
||||||
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
|
|
||||||
headers.add("Pragma", "no-cache");
|
|
||||||
headers.add("Expires", "0");
|
|
||||||
|
|
||||||
return new ResponseEntity<>(zipBytes, headers, HttpStatus.OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成ZIP文件名
|
|
||||||
*/
|
|
||||||
private static String generateZipFilename() {
|
|
||||||
return ZIP_FILE_PREFIX + UUID.randomUUID().toString().split("-")[0] + ZIP_FILE_SUFFIX;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,23 +1,24 @@
|
||||||
package cn.bunny.service.impl;
|
package cn.bunny.service.impl;
|
||||||
|
|
||||||
import cn.bunny.core.ZipFileService;
|
|
||||||
import cn.bunny.core.provider.IMetadataProvider;
|
import cn.bunny.core.provider.IMetadataProvider;
|
||||||
|
import cn.bunny.core.template.VmsTBaseTemplateGenerator;
|
||||||
import cn.bunny.domain.dto.VmsArgumentDto;
|
import cn.bunny.domain.dto.VmsArgumentDto;
|
||||||
import cn.bunny.domain.entity.ColumnMetaData;
|
import cn.bunny.domain.entity.ColumnMetaData;
|
||||||
import cn.bunny.domain.entity.TableMetaData;
|
import cn.bunny.domain.entity.TableMetaData;
|
||||||
import cn.bunny.domain.vo.GeneratorVo;
|
import cn.bunny.domain.vo.GeneratorVo;
|
||||||
import cn.bunny.service.GeneratorService;
|
import cn.bunny.service.GeneratorService;
|
||||||
import cn.bunny.service.helper.GeneratorServiceImplHelper;
|
import cn.bunny.service.helper.VmsGeneratorPathHelper;
|
||||||
|
import cn.bunny.utils.ZipFileUtil;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 代码生成服务实现类
|
* 代码生成服务实现类
|
||||||
|
@ -29,7 +30,6 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||||
|
|
||||||
private final IMetadataProvider databaseMetadataProvider;
|
private final IMetadataProvider databaseMetadataProvider;
|
||||||
private final IMetadataProvider sqlMetadataProvider;
|
private final IMetadataProvider sqlMetadataProvider;
|
||||||
private final ZipFileService zipFileService;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 代码生成方法---数据库生成
|
* 代码生成方法---数据库生成
|
||||||
|
@ -43,7 +43,7 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||||
.flatMap(tableName -> {
|
.flatMap(tableName -> {
|
||||||
TableMetaData tableMeta = databaseMetadataProvider.getTableMetadata(tableName);
|
TableMetaData tableMeta = databaseMetadataProvider.getTableMetadata(tableName);
|
||||||
List<ColumnMetaData> columns = databaseMetadataProvider.getColumnInfoList(tableName);
|
List<ColumnMetaData> columns = databaseMetadataProvider.getColumnInfoList(tableName);
|
||||||
return GeneratorServiceImplHelper.getGeneratorStream(dto, tableMeta, columns);
|
return getGeneratorStream(dto, tableMeta, columns);
|
||||||
})
|
})
|
||||||
.collect(Collectors.groupingBy(GeneratorVo::getTableName));
|
.collect(Collectors.groupingBy(GeneratorVo::getTableName));
|
||||||
}
|
}
|
||||||
|
@ -60,7 +60,7 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||||
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 = GeneratorServiceImplHelper.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<>();
|
||||||
map.put(tableMeta.getTableName(), generatorVoList);
|
map.put(tableMeta.getTableName(), generatorVoList);
|
||||||
|
@ -87,12 +87,47 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||||
*/
|
*/
|
||||||
private ResponseEntity<byte[]> downloadByZip(VmsArgumentDto dto,
|
private ResponseEntity<byte[]> downloadByZip(VmsArgumentDto dto,
|
||||||
Function<VmsArgumentDto, Map<String, List<GeneratorVo>>> generator) {
|
Function<VmsArgumentDto, Map<String, List<GeneratorVo>>> generator) {
|
||||||
|
// 调用生成函数
|
||||||
List<GeneratorVo> generatorVoList = generator.apply(dto)
|
List<GeneratorVo> generatorVoList = generator.apply(dto)
|
||||||
.values().stream()
|
.values().stream()
|
||||||
.flatMap(Collection::stream)
|
.flatMap(Collection::stream)
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
byte[] zipBytes = zipFileService.createZipFile(generatorVoList);
|
// 创建Zip文件
|
||||||
return GeneratorServiceImplHelper.getResponseEntity(zipBytes);
|
byte[] zipBytes = ZipFileUtil.createZipFile(generatorVoList);
|
||||||
|
|
||||||
|
// 设置返回给前端的文件名
|
||||||
|
String zipFilename = "code-" + UUID.randomUUID().toString().split("-")[0] + ".zip";
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.add("Content-Disposition", "attachment; filename=" + zipFilename);
|
||||||
|
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||||
|
headers.add("Pragma", "no-cache");
|
||||||
|
headers.add("Expires", "0");
|
||||||
|
|
||||||
|
return new ResponseEntity<>(zipBytes, headers, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取生成器流
|
||||||
|
*
|
||||||
|
* @param dto 生成参数
|
||||||
|
* @param tableMeta 表元数据
|
||||||
|
* @param columns 列信息
|
||||||
|
* @return 生成器流
|
||||||
|
*/
|
||||||
|
public Stream<GeneratorVo> getGeneratorStream(VmsArgumentDto dto, TableMetaData tableMeta, List<ColumnMetaData> columns) {
|
||||||
|
return dto.getPath().parallelStream().map(path -> {
|
||||||
|
VmsTBaseTemplateGenerator generator = new VmsTBaseTemplateGenerator(dto, path, tableMeta);
|
||||||
|
String code = generator.generateCode(tableMeta, columns).toString();
|
||||||
|
|
||||||
|
return GeneratorVo.builder()
|
||||||
|
.id(UUID.randomUUID().toString())
|
||||||
|
.code(code)
|
||||||
|
.comment(tableMeta.getComment())
|
||||||
|
.tableName(tableMeta.getTableName())
|
||||||
|
.path(VmsGeneratorPathHelper.processVmPath(dto, path, tableMeta.getTableName()))
|
||||||
|
.build();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package cn.bunny.core;
|
package cn.bunny.utils;
|
||||||
|
|
||||||
import cn.bunny.domain.vo.GeneratorVo;
|
import cn.bunny.domain.vo.GeneratorVo;
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -16,9 +14,7 @@ import java.util.zip.ZipOutputStream;
|
||||||
* <p>
|
* <p>
|
||||||
* 提供将生成的代码模板打包为ZIP文件并支持下载的功能
|
* 提供将生成的代码模板打包为ZIP文件并支持下载的功能
|
||||||
*/
|
*/
|
||||||
@Service
|
public class ZipFileUtil {
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class ZipFileService {
|
|
||||||
|
|
||||||
private static final String FILE_EXTENSION = ".vm";
|
private static final String FILE_EXTENSION = ".vm";
|
||||||
private static final String UTF_8 = StandardCharsets.UTF_8.name();
|
private static final String UTF_8 = StandardCharsets.UTF_8.name();
|
||||||
|
@ -30,7 +26,7 @@ public class ZipFileService {
|
||||||
* @return ZIP文件字节数组
|
* @return ZIP文件字节数组
|
||||||
* @throws RuntimeException 打包失败时抛出
|
* @throws RuntimeException 打包失败时抛出
|
||||||
*/
|
*/
|
||||||
public byte[] createZipFile(List<GeneratorVo> generatorVoList) {
|
public static byte[] createZipFile(List<GeneratorVo> generatorVoList) {
|
||||||
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||||
ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream, StandardCharsets.UTF_8)) {
|
ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream, StandardCharsets.UTF_8)) {
|
||||||
|
|
||||||
|
@ -48,7 +44,7 @@ public class ZipFileService {
|
||||||
* @param generatorVo 代码生成结果对象,包含文件路径和内容
|
* @param generatorVo 代码生成结果对象,包含文件路径和内容
|
||||||
* @throws RuntimeException 当文件添加失败时抛出,包含失败文件路径信息
|
* @throws RuntimeException 当文件添加失败时抛出,包含失败文件路径信息
|
||||||
*/
|
*/
|
||||||
private void addToZip(ZipOutputStream zipOutputStream, GeneratorVo generatorVo) {
|
private static void addToZip(ZipOutputStream zipOutputStream, GeneratorVo generatorVo) {
|
||||||
try {
|
try {
|
||||||
String entryPath = generatorVo.getPath().replace(FILE_EXTENSION, "");
|
String entryPath = generatorVo.getPath().replace(FILE_EXTENSION, "");
|
||||||
zipOutputStream.putNextEntry(new ZipEntry(entryPath));
|
zipOutputStream.putNextEntry(new ZipEntry(entryPath));
|
|
@ -14,4 +14,7 @@ spring:
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
url: jdbc:mysql://${bunny.master.host}:${bunny.master.port}/${bunny.master.database}?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
|
url: jdbc:mysql://${bunny.master.host}:${bunny.master.port}/${bunny.master.database}?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
|
||||||
username: ${bunny.master.username}
|
username: ${bunny.master.username}
|
||||||
password: ${bunny.master.password}
|
password: ${bunny.master.password}
|
||||||
|
hikari:
|
||||||
|
maximum-pool-size: 20
|
||||||
|
connection-timeout: 30000
|
Loading…
Reference in New Issue