feat: 优化代码
This commit is contained in:
parent
2325754ede
commit
3d3540e3e9
|
@ -7,16 +7,23 @@ import io.swagger.v3.oas.models.info.Info;
|
|||
import io.swagger.v3.oas.models.info.License;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springdoc.core.models.GroupedOpenApi;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class Knife4jConfig {
|
||||
|
||||
@Value("${server.port}")
|
||||
private String port;
|
||||
|
||||
@Bean
|
||||
public OpenAPI openAPI() {
|
||||
String url = "http://localhost:" + port;
|
||||
|
||||
// 作者等信息
|
||||
Contact contact = new Contact().name("Bunny").email("1319900154@qq.com").url("http://localhost:9999");
|
||||
Contact contact = new Contact().name("Bunny").email("1319900154@qq.com").url(url);
|
||||
// 使用协议
|
||||
License license = new License().name("MIT").url("https://mit-license.org");
|
||||
// 相关信息
|
||||
|
@ -24,7 +31,7 @@ public class Knife4jConfig {
|
|||
.contact(contact).license(license)
|
||||
.description("Bunny代码生成器")
|
||||
.summary("Bunny的代码生成器")
|
||||
.termsOfService("http://localhost:9999")
|
||||
.termsOfService(url)
|
||||
.version("v1.0.0");
|
||||
|
||||
return new OpenAPI().info(info).externalDocs(new ExternalDocumentation());
|
||||
|
|
|
@ -6,6 +6,8 @@ import cn.bunny.dao.vo.TableInfoVo;
|
|||
import cn.bunny.service.TableService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
@ -18,24 +20,31 @@ import java.util.stream.Collectors;
|
|||
@RequestMapping("/api/table")
|
||||
public class TableController {
|
||||
|
||||
private final TableService tableService;
|
||||
@Value("${bunny.master.database}")
|
||||
private String currentDatabase;
|
||||
|
||||
public TableController(TableService tableService) {
|
||||
this.tableService = tableService;
|
||||
@Resource
|
||||
private TableService tableService;
|
||||
|
||||
@Operation(summary = "当前配置的数据库", description = "当前配置的数据库")
|
||||
@GetMapping("currentDatabaseName")
|
||||
public Result<String> getCurrentDatabaseName() {
|
||||
return Result.success(currentDatabase);
|
||||
}
|
||||
|
||||
@Operation(summary = "数据库所有的表", description = "获取[当前/所有]数据库表")
|
||||
@GetMapping("getDbTables")
|
||||
public Result<List<TableInfoVo>> getDbTables(String dbName) {
|
||||
List<TableInfoVo> list = tableService.getDbTables(dbName);
|
||||
@GetMapping("databaseTableList")
|
||||
public Result<List<TableInfoVo>> databaseTableList(String dbName) {
|
||||
List<TableInfoVo> list = tableService.databaseTableList(dbName);
|
||||
return Result.success(list);
|
||||
}
|
||||
|
||||
@Operation(summary = "所有的数据库", description = "所有的数据库")
|
||||
@GetMapping("getDbList")
|
||||
public Result<List<TableInfoVo>> getDbList() {
|
||||
List<TableInfoVo> allDb = tableService.getDbTables(null);
|
||||
@Operation(summary = "所有的数据库名称", description = "当前数据库所有的数据库名称")
|
||||
@GetMapping("databaseList")
|
||||
public Result<List<TableInfoVo>> databaseList() {
|
||||
List<TableInfoVo> allDb = tableService.databaseTableList(null);
|
||||
|
||||
// 将当前数据库表分组,以数据库名称为key
|
||||
List<TableInfoVo> list = allDb.stream()
|
||||
.collect(Collectors.groupingBy(TableInfoVo::getTableCat))
|
||||
.values().stream()
|
||||
|
@ -48,17 +57,17 @@ public class TableController {
|
|||
return Result.success(list);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取表属性", description = "获取表属性")
|
||||
@GetMapping("getTableMetaData")
|
||||
public Result<TableInfoVo> getTableMetaData(String tableName) {
|
||||
TableInfoVo tableMetaData = tableService.getTableMetaData(tableName);
|
||||
@Operation(summary = "表属性", description = "获取当前查询表属性")
|
||||
@GetMapping("tableMetaData")
|
||||
public Result<TableInfoVo> tableMetaData(String tableName) {
|
||||
TableInfoVo tableMetaData = tableService.tableMetaData(tableName);
|
||||
return Result.success(tableMetaData);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取列属性", description = "获取列属性")
|
||||
@GetMapping("getColumnInfo")
|
||||
public Result<List<ColumnMetaData>> getColumnInfo(String tableName) {
|
||||
List<ColumnMetaData> columnInfo = tableService.getColumnInfo(tableName);
|
||||
@Operation(summary = "表的列属性", description = "获取当前查询表中列属性")
|
||||
@GetMapping("tableColumnInfo")
|
||||
public Result<List<ColumnMetaData>> tableColumnInfo(String tableName) {
|
||||
List<ColumnMetaData> columnInfo = tableService.tableColumnInfo(tableName);
|
||||
return Result.success(columnInfo);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import cn.bunny.dao.vo.VmsPathVo;
|
|||
import cn.bunny.service.VmsService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
@ -19,16 +20,13 @@ import java.util.Map;
|
|||
@RequestMapping("/api/vms")
|
||||
public class VmsController {
|
||||
|
||||
private final VmsService vmsService;
|
||||
|
||||
public VmsController(VmsService vmsService) {
|
||||
this.vmsService = vmsService;
|
||||
}
|
||||
@Resource
|
||||
private VmsService vmsService;
|
||||
|
||||
@Operation(summary = "获取vms文件路径", description = "获取所有vms下的文件路径")
|
||||
@GetMapping("getVmsPathList")
|
||||
public Result<Map<String, List<VmsPathVo>>> getVmsPathList() {
|
||||
Map<String, List<VmsPathVo>> list = vmsService.getVmsPathList();
|
||||
@GetMapping("vmsResourcePathList")
|
||||
public Result<Map<String, List<VmsPathVo>>> vmsResourcePathList() {
|
||||
Map<String, List<VmsPathVo>> list = vmsService.vmsResourcePathList();
|
||||
return Result.success(list);
|
||||
}
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||
@Getter
|
||||
@ToString
|
||||
@Slf4j
|
||||
public class AuthCustomerException extends RuntimeException {
|
||||
public class GeneratorCodeException extends RuntimeException {
|
||||
// 状态码
|
||||
Integer code;
|
||||
|
||||
|
@ -21,18 +21,18 @@ public class AuthCustomerException extends RuntimeException {
|
|||
ResultCodeEnum resultCodeEnum;
|
||||
|
||||
|
||||
public AuthCustomerException(Integer code, String message) {
|
||||
public GeneratorCodeException(Integer code, String message) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public AuthCustomerException(String message) {
|
||||
public GeneratorCodeException(String message) {
|
||||
super(message);
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public AuthCustomerException(ResultCodeEnum codeEnum) {
|
||||
public GeneratorCodeException(ResultCodeEnum codeEnum) {
|
||||
super(codeEnum.getMessage());
|
||||
this.code = codeEnum.getCode();
|
||||
this.message = codeEnum.getMessage();
|
|
@ -24,9 +24,9 @@ import java.util.stream.Collectors;
|
|||
@Slf4j
|
||||
public class GlobalExceptionHandler {
|
||||
// 自定义异常信息
|
||||
@ExceptionHandler(AuthCustomerException.class)
|
||||
@ExceptionHandler(GeneratorCodeException.class)
|
||||
@ResponseBody
|
||||
public Result<Object> exceptionHandler(AuthCustomerException exception) {
|
||||
public Result<Object> exceptionHandler(GeneratorCodeException exception) {
|
||||
Integer code = exception.getCode() != null ? exception.getCode() : 500;
|
||||
return Result.error(null, code, exception.getMessage());
|
||||
}
|
||||
|
@ -50,24 +50,24 @@ public class GlobalExceptionHandler {
|
|||
String dataTooLongError = "Data too long for column (.*?) at row 1";
|
||||
Matcher dataTooLongErrorMatcher = Pattern.compile(dataTooLongError).matcher(message);
|
||||
if (dataTooLongErrorMatcher.find()) {
|
||||
return Result.error(null, 500, dataTooLongErrorMatcher.group(1) + " 字段数据过大" );
|
||||
return Result.error(null, 500, dataTooLongErrorMatcher.group(1) + " 字段数据过大");
|
||||
}
|
||||
|
||||
// 主键冲突
|
||||
String primaryKeyError = "Duplicate entry '(.*?)' for key .*";
|
||||
Matcher primaryKeyErrorMatcher = Pattern.compile(primaryKeyError).matcher(message);
|
||||
if (primaryKeyErrorMatcher.find()) {
|
||||
return Result.error(null, 500, "[" + primaryKeyErrorMatcher.group(1) + "]已存在" );
|
||||
return Result.error(null, 500, "[" + primaryKeyErrorMatcher.group(1) + "]已存在");
|
||||
}
|
||||
|
||||
// corn表达式错误
|
||||
String cronExpression = "CronExpression '(.*?)' is invalid";
|
||||
Matcher cronExpressionMatcher = Pattern.compile(cronExpression).matcher(message);
|
||||
if (cronExpressionMatcher.find()) {
|
||||
return Result.error(null, 500, "表达式 " + cronExpressionMatcher.group(1) + " 不合法" );
|
||||
return Result.error(null, 500, "表达式 " + cronExpressionMatcher.group(1) + " 不合法");
|
||||
}
|
||||
|
||||
log.error("GlobalExceptionHandler===>运行时异常信息:{}" , message);
|
||||
log.error("GlobalExceptionHandler===>运行时异常信息:{}", message);
|
||||
return Result.error(null, 500, message);
|
||||
}
|
||||
|
||||
|
@ -76,7 +76,7 @@ public class GlobalExceptionHandler {
|
|||
public Result<String> handleValidationExceptions(MethodArgumentNotValidException ex) {
|
||||
String errorMessage = ex.getBindingResult().getFieldErrors().stream()
|
||||
.map(DefaultMessageSourceResolvable::getDefaultMessage)
|
||||
.collect(Collectors.joining(", " ));
|
||||
.collect(Collectors.joining(", "));
|
||||
return Result.error(null, 201, errorMessage);
|
||||
}
|
||||
|
||||
|
@ -84,7 +84,7 @@ public class GlobalExceptionHandler {
|
|||
@ExceptionHandler(ArithmeticException.class)
|
||||
@ResponseBody
|
||||
public Result<Object> error(ArithmeticException exception) {
|
||||
log.error("GlobalExceptionHandler===>特定异常信息:{}" , exception.getMessage());
|
||||
log.error("GlobalExceptionHandler===>特定异常信息:{}", exception.getMessage());
|
||||
|
||||
return Result.error(null, 500, exception.getMessage());
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ public class GlobalExceptionHandler {
|
|||
@ExceptionHandler(AccessDeniedException.class)
|
||||
@ResponseBody
|
||||
public Result<String> error(AccessDeniedException exception) throws AccessDeniedException {
|
||||
log.error("GlobalExceptionHandler===>spring security异常:{}" , exception.getMessage());
|
||||
log.error("GlobalExceptionHandler===>spring security异常:{}", exception.getMessage());
|
||||
|
||||
return Result.error(ResultCodeEnum.SERVICE_ERROR);
|
||||
}
|
||||
|
@ -102,10 +102,10 @@ public class GlobalExceptionHandler {
|
|||
@ExceptionHandler(SQLIntegrityConstraintViolationException.class)
|
||||
@ResponseBody
|
||||
public Result<String> exceptionHandler(SQLIntegrityConstraintViolationException exception) {
|
||||
log.error("GlobalExceptionHandler===>处理SQL异常:{}" , exception.getMessage());
|
||||
log.error("GlobalExceptionHandler===>处理SQL异常:{}", exception.getMessage());
|
||||
|
||||
String message = exception.getMessage();
|
||||
if (message.contains("Duplicate entry" )) {
|
||||
if (message.contains("Duplicate entry")) {
|
||||
// 错误信息
|
||||
return Result.error(ResultCodeEnum.USER_IS_EMPTY);
|
||||
} else {
|
||||
|
|
|
@ -13,14 +13,14 @@ public interface TableService {
|
|||
* @param tableName 表名称
|
||||
* @return 表属性
|
||||
*/
|
||||
TableInfoVo getTableMetaData(String tableName);
|
||||
TableInfoVo tableMetaData(String tableName);
|
||||
|
||||
/**
|
||||
* 获取所有数据库
|
||||
*
|
||||
* @return 所有表信息
|
||||
*/
|
||||
List<TableInfoVo> getDbTables(String tableName);
|
||||
List<TableInfoVo> databaseTableList(String tableName);
|
||||
|
||||
/**
|
||||
* 获取列属性
|
||||
|
@ -28,5 +28,5 @@ public interface TableService {
|
|||
* @param tableName 表名称
|
||||
* @return 当前表所有的列内容
|
||||
*/
|
||||
List<ColumnMetaData> getColumnInfo(String tableName);
|
||||
List<ColumnMetaData> tableColumnInfo(String tableName);
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ public interface VmsService {
|
|||
*
|
||||
* @return vms下的文件路径
|
||||
*/
|
||||
Map<String, List<VmsPathVo>> getVmsPathList();
|
||||
Map<String, List<VmsPathVo>> vmsResourcePathList();
|
||||
|
||||
/**
|
||||
* 打包成zip下载
|
||||
|
|
|
@ -5,6 +5,7 @@ import cn.bunny.dao.entity.TableMetaData;
|
|||
import cn.bunny.dao.vo.TableInfoVo;
|
||||
import cn.bunny.service.TableService;
|
||||
import cn.bunny.utils.DbInfoUtil;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
@ -14,11 +15,8 @@ import java.util.List;
|
|||
@Service
|
||||
public class TableServiceImpl implements TableService {
|
||||
|
||||
private final DbInfoUtil dbInfoUtil;
|
||||
|
||||
public TableServiceImpl(DbInfoUtil dbInfoUtil) {
|
||||
this.dbInfoUtil = dbInfoUtil;
|
||||
}
|
||||
@Resource
|
||||
private DbInfoUtil dbInfoUtil;
|
||||
|
||||
/**
|
||||
* 获取表属性
|
||||
|
@ -28,7 +26,7 @@ public class TableServiceImpl implements TableService {
|
|||
*/
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public TableInfoVo getTableMetaData(String tableName) {
|
||||
public TableInfoVo tableMetaData(String tableName) {
|
||||
TableInfoVo tableInfoVo = new TableInfoVo();
|
||||
|
||||
TableMetaData tableMetaData = dbInfoUtil.tableInfo(tableName);
|
||||
|
@ -44,7 +42,7 @@ public class TableServiceImpl implements TableService {
|
|||
*/
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public List<TableInfoVo> getDbTables(String dbName) {
|
||||
public List<TableInfoVo> databaseTableList(String dbName) {
|
||||
// 当前数据库数据库所有的表
|
||||
List<TableMetaData> allTableInfo = dbInfoUtil.getDbTableList(dbName);
|
||||
|
||||
|
@ -64,7 +62,7 @@ public class TableServiceImpl implements TableService {
|
|||
*/
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public List<ColumnMetaData> getColumnInfo(String tableName) {
|
||||
public List<ColumnMetaData> tableColumnInfo(String tableName) {
|
||||
return dbInfoUtil.columnInfo(tableName);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,8 +10,8 @@ import cn.bunny.service.VmsService;
|
|||
import cn.bunny.utils.ResourceFileUtil;
|
||||
import cn.bunny.utils.VmsUtil;
|
||||
import cn.hutool.crypto.digest.MD5;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.SneakyThrows;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
@ -29,13 +29,10 @@ import java.util.zip.ZipEntry;
|
|||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
@Service
|
||||
|
||||
public class VmsServiceImpl implements VmsService {
|
||||
private final TableService tableService;
|
||||
|
||||
public VmsServiceImpl(TableService tableService) {
|
||||
this.tableService = tableService;
|
||||
}
|
||||
@Resource
|
||||
private TableService tableService;
|
||||
|
||||
/**
|
||||
* 生成服务端代码
|
||||
|
@ -48,33 +45,12 @@ public class VmsServiceImpl implements VmsService {
|
|||
String tableName = dto.getTableName();
|
||||
|
||||
return dto.getPath().stream().map(path -> {
|
||||
StringWriter writer = new StringWriter();
|
||||
|
||||
// 表格属性名 和 列信息
|
||||
TableInfoVo tableMetaData = tableService.getTableMetaData(tableName);
|
||||
List<ColumnMetaData> columnInfoList = tableService.getColumnInfo(tableName).stream().distinct().toList();
|
||||
List<String> list = columnInfoList.stream().map(ColumnMetaData::getColumnName).toList();
|
||||
|
||||
// 添加要生成的属性
|
||||
VelocityContext context = new VelocityContext();
|
||||
|
||||
// 当前的表名
|
||||
context.put("tableName" , tableMetaData.getTableName());
|
||||
|
||||
// 表字段的注释内容
|
||||
context.put("comment" , dto.getComment());
|
||||
|
||||
// 设置包名称
|
||||
context.put("package" , dto.getPackageName());
|
||||
|
||||
// 当前表的列信息
|
||||
context.put("columnInfoList" , columnInfoList);
|
||||
|
||||
// 数据库sql列
|
||||
context.put("baseColumnList" , String.join("," , list));
|
||||
TableInfoVo tableMetaData = tableService.tableMetaData(tableName);
|
||||
List<ColumnMetaData> columnInfoList = tableService.tableColumnInfo(tableName).stream().distinct().toList();
|
||||
|
||||
// 生成模板
|
||||
VmsUtil.commonVms(writer, context, "vms/" + path, dto);
|
||||
StringWriter writer = VmsUtil.buildGeneratorCodeTemplate(dto, path, tableMetaData, columnInfoList);
|
||||
|
||||
// 处理 vm 文件名
|
||||
path = VmsUtil.handleVmFilename(path, dto.getClassName());
|
||||
|
@ -95,13 +71,13 @@ public class VmsServiceImpl implements VmsService {
|
|||
*/
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public Map<String, List<VmsPathVo>> getVmsPathList() {
|
||||
public Map<String, List<VmsPathVo>> vmsResourcePathList() {
|
||||
// 读取当前项目中所有的 vm 模板发给前端
|
||||
List<String> vmsRelativeFiles = ResourceFileUtil.getRelativeFiles("vms" );
|
||||
List<String> vmsRelativeFiles = ResourceFileUtil.getRelativeFiles("vms");
|
||||
|
||||
return vmsRelativeFiles.stream().map(vmFile -> {
|
||||
String[] filepathList = vmFile.split("/" );
|
||||
String filename = filepathList[filepathList.length - 1].replace(".vm" , "" );
|
||||
String[] filepathList = vmFile.split("/");
|
||||
String filename = filepathList[filepathList.length - 1].replace(".vm", "");
|
||||
|
||||
return VmsPathVo.builder().name(vmFile).label(filename).type(filepathList[0]).build();
|
||||
})
|
||||
|
@ -125,7 +101,7 @@ public class VmsServiceImpl implements VmsService {
|
|||
// 2. 遍历并创建
|
||||
generatorVoList.forEach(generatorVo -> {
|
||||
// zip中的路径
|
||||
String path = generatorVo.getPath().replace(".vm" , "" );
|
||||
String path = generatorVo.getPath().replace(".vm", "");
|
||||
|
||||
// zip中的文件
|
||||
String code = generatorVo.getCode();
|
||||
|
@ -148,14 +124,14 @@ public class VmsServiceImpl implements VmsService {
|
|||
|
||||
// 2.1 文件不重名
|
||||
long currentTimeMillis = System.currentTimeMillis();
|
||||
String digestHex = MD5.create().digestHex(currentTimeMillis + "" );
|
||||
String digestHex = MD5.create().digestHex(currentTimeMillis + "");
|
||||
|
||||
// 3. 准备响应
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add("Content-Disposition" , "attachment; filename=" + "vms-" + digestHex + ".zip" );
|
||||
headers.add("Cache-Control" , "no-cache, no-store, must-revalidate" );
|
||||
headers.add("Pragma" , "no-cache" );
|
||||
headers.add("Expires" , "0" );
|
||||
headers.add("Content-Disposition", "attachment; filename=" + "vms-" + digestHex + ".zip");
|
||||
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
headers.add("Pragma", "no-cache");
|
||||
headers.add("Expires", "0");
|
||||
|
||||
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
|
||||
return new ResponseEntity<>(byteArrayInputStream.readAllBytes(), headers, HttpStatus.OK);
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package cn.bunny.utils;
|
||||
|
||||
import cn.bunny.dao.dto.VmsArgumentDto;
|
||||
import cn.bunny.dao.entity.ColumnMetaData;
|
||||
import cn.bunny.dao.vo.TableInfoVo;
|
||||
import com.google.common.base.CaseFormat;
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
|
@ -9,70 +11,77 @@ import org.apache.velocity.app.Velocity;
|
|||
import java.io.StringWriter;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public class VmsUtil {
|
||||
|
||||
private static final Map<String, String> TYPE_MAPPINGS = Map.of(
|
||||
"controller" , "Controller" ,
|
||||
"service" , "Service" ,
|
||||
"serviceImpl" , "ServiceImpl" ,
|
||||
"mapper" , "Mapper" ,
|
||||
"resourceMapper" , "Mapper"
|
||||
"controller", "Controller",
|
||||
"service", "Service",
|
||||
"serviceImpl", "ServiceImpl",
|
||||
"mapper", "Mapper",
|
||||
"resourceMapper", "Mapper"
|
||||
);
|
||||
|
||||
/**
|
||||
* 生成模板
|
||||
*
|
||||
* @param writer 写入字符串内容
|
||||
* @param context Velocity上下文
|
||||
* @param templateName 模板名称
|
||||
* @param dto 类名称可以自定义,格式为 xxx_xxx
|
||||
* @param dto 类名称可以自定义,格式为 xxx_xxx
|
||||
* @param path 当前路径
|
||||
* @param tableMetaData 表属性
|
||||
* @param columnInfoList 列属性数组
|
||||
* @return StringWriter
|
||||
*/
|
||||
public static void commonVms(StringWriter writer, VelocityContext context, String templateName, VmsArgumentDto dto) {
|
||||
public static StringWriter buildGeneratorCodeTemplate(VmsArgumentDto dto, String path, TableInfoVo tableMetaData, List<ColumnMetaData> columnInfoList) {
|
||||
StringWriter writer = new StringWriter();
|
||||
String date = new SimpleDateFormat(dto.getSimpleDateFormat()).format(new Date());
|
||||
List<String> list = columnInfoList.stream().map(ColumnMetaData::getColumnName).toList();
|
||||
|
||||
// 添加要生成的属性
|
||||
VelocityContext context = new VelocityContext();
|
||||
|
||||
// 类名称如果是小驼峰,需要 [手写] 为 [下划线] 之后由 [代码 -> 小驼峰/大驼峰]
|
||||
String className = dto.getClassName();
|
||||
|
||||
// 去除表开头前缀
|
||||
String tablePrefixes = dto.getTablePrefixes();
|
||||
|
||||
// 当前捉着
|
||||
String author = dto.getAuthor();
|
||||
|
||||
// vm 不能直接写 `{` 需要转换下
|
||||
context.put("leftBrace", "{");
|
||||
// 当前日期
|
||||
context.put("date", date);
|
||||
// 作者名字
|
||||
context.put("author", dto.getAuthor());
|
||||
// 每个 Controller 上的请求前缀
|
||||
String requestMapping = dto.getRequestMapping();
|
||||
context.put("requestMapping", dto.getRequestMapping());
|
||||
// 当前的表名
|
||||
context.put("tableName", tableMetaData.getTableName());
|
||||
// 表字段的注释内容
|
||||
context.put("comment", dto.getComment());
|
||||
// 设置包名称
|
||||
context.put("package", dto.getPackageName());
|
||||
// 当前表的列信息
|
||||
context.put("columnInfoList", columnInfoList);
|
||||
// 数据库sql列
|
||||
context.put("baseColumnList", String.join(",", list));
|
||||
|
||||
// 将 表前缀 转成数组
|
||||
AtomicReference<String> replaceTableName = new AtomicReference<>(className);
|
||||
for (String prefix : tablePrefixes.split("[,,]" )) {
|
||||
replaceTableName.set(className.replace(prefix, "" ));
|
||||
String replaceTableName = "";
|
||||
for (String prefix : tablePrefixes.split("[,,]")) {
|
||||
replaceTableName = className.replace(prefix, "");
|
||||
}
|
||||
|
||||
String date = new SimpleDateFormat(dto.getSimpleDateFormat()).format(new Date());
|
||||
// vm 不能直接写 `{` 需要转换下
|
||||
context.put("leftBrace" , "{" );
|
||||
|
||||
// 当前日期
|
||||
context.put("date" , date);
|
||||
|
||||
// 作者名字
|
||||
context.put("author" , author);
|
||||
|
||||
// 每个 Controller 上的请求前缀
|
||||
context.put("requestMapping" , requestMapping);
|
||||
|
||||
// 将类名称转成小驼峰
|
||||
String toCamelCase = ConvertUtil.convertToCamelCase(replaceTableName.get());
|
||||
context.put("classLowercaseName" , toCamelCase);
|
||||
|
||||
String toCamelCase = ConvertUtil.convertToCamelCase(replaceTableName);
|
||||
context.put("classLowercaseName", toCamelCase);
|
||||
// 将类名称转成大驼峰
|
||||
String convertToCamelCase = ConvertUtil.convertToCamelCase(replaceTableName.get(), true);
|
||||
context.put("classUppercaseName" , convertToCamelCase);
|
||||
|
||||
String convertToCamelCase = ConvertUtil.convertToCamelCase(replaceTableName, true);
|
||||
context.put("classUppercaseName", convertToCamelCase);
|
||||
// Velocity 生成模板
|
||||
Template servicePathTemplate = Velocity.getTemplate(templateName, "UTF-8" );
|
||||
Template servicePathTemplate = Velocity.getTemplate("vms/" + path, "UTF-8");
|
||||
servicePathTemplate.merge(context, writer);
|
||||
|
||||
return writer;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -82,7 +91,7 @@ public class VmsUtil {
|
|||
* @param className 类名
|
||||
*/
|
||||
public static String handleVmFilename(String path, String className) {
|
||||
String[] splitPaths = path.split("/" );
|
||||
String[] splitPaths = path.split("/");
|
||||
int splitPathsSize = splitPaths.length - 1;
|
||||
|
||||
// 大驼峰名称
|
||||
|
@ -92,9 +101,9 @@ public class VmsUtil {
|
|||
|
||||
// 当前文件名
|
||||
String filename = splitPaths[splitPathsSize];
|
||||
filename = filename.replace(".vm" , "" );
|
||||
filename = filename.replace(".vm", "");
|
||||
|
||||
String[] split = filename.split("\\." );
|
||||
String[] split = filename.split("\\.");
|
||||
// 文件名称
|
||||
String name = split[0];
|
||||
// 文件扩展名
|
||||
|
@ -106,15 +115,15 @@ public class VmsUtil {
|
|||
// 判断是否是 Java 或者 xml 文件
|
||||
String typeMappingsFilename = TYPE_MAPPINGS.get(name);
|
||||
typeMappingsFilename = typeMappingsFilename == null ? "" : typeMappingsFilename;
|
||||
if (filename.contains("java" ) || filename.contains("xml" )) {
|
||||
if (filename.contains("java") || filename.contains("xml")) {
|
||||
filename = CamelCase + typeMappingsFilename + "." + extension;
|
||||
}
|
||||
|
||||
if (filename.contains("vue" ) && !filename.contains("index" )) {
|
||||
if (filename.contains("vue") && !filename.contains("index")) {
|
||||
filename = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, camelCase) + "-" + name + "." + extension;
|
||||
}
|
||||
|
||||
splitPaths[splitPathsSize] = filename;
|
||||
return String.join("/" , splitPaths);
|
||||
return String.join("/", splitPaths);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,6 @@ spring:
|
|||
name: generator-code
|
||||
thymeleaf:
|
||||
check-template-location: false
|
||||
prefix: classpath:/static/
|
||||
|
||||
datasource:
|
||||
type: com.zaxxer.hikari.HikariDataSource
|
||||
|
|
|
@ -15,7 +15,7 @@ class VmsServiceImplTest {
|
|||
|
||||
|
||||
@Test
|
||||
void getVmsPathList() throws IOException, URISyntaxException {
|
||||
void vmsResourcePathList() throws IOException, URISyntaxException {
|
||||
List<String> vmsFiles = ResourceFileUtil.getAbsoluteFiles("vms");
|
||||
System.out.println(vmsFiles);
|
||||
|
||||
|
|
Loading…
Reference in New Issue