增加生成后端服务代码。

This commit is contained in:
asp_ly 2024-12-28 20:04:06 +08:00
parent cfeafb4b36
commit 693bd8d833
5 changed files with 25 additions and 6 deletions

View File

@ -17,6 +17,11 @@ public class CodeGenerator {
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
try { try {
// 0. 选择API类型
System.out.println("请选择API类型1: 三方API, 2: 二方API");
String apiType = scanner.nextLine();
boolean isThirdPartyApi = "1".equals(apiType);
// 1. 输入模块信息 // 1. 输入模块信息
System.out.println("请输入模块名称(中文,如:用户):"); System.out.println("请输入模块名称(中文,如:用户):");
String moduleName = scanner.nextLine(); String moduleName = scanner.nextLine();
@ -59,6 +64,7 @@ public class CodeGenerator {
config.setOutputPath(outputPath); config.setOutputPath(outputPath);
config.setTableName(tableName); config.setTableName(tableName);
config.setUrlPath(urlPath); config.setUrlPath(urlPath);
config.setThirdPartyApi(isThirdPartyApi);
// 5. 生成代码 // 5. 生成代码
CodeGeneratorUtils.generateCode(createTableSql, config); CodeGeneratorUtils.generateCode(createTableSql, config);
@ -78,7 +84,8 @@ public class CodeGenerator {
* 通过API方式生成代码 * 通过API方式生成代码
*/ */
public static void generate(String moduleName, String className, String basePackage, public static void generate(String moduleName, String className, String basePackage,
String outputPath, String tableName, String urlPath, String createTableSql) { String outputPath, String tableName, String urlPath, String createTableSql,
boolean isThirdPartyApi) {
try { try {
// 1. 验证输入 // 1. 验证输入
if (StringUtils.isAnyBlank(moduleName, className, basePackage, outputPath, tableName, urlPath, createTableSql)) { if (StringUtils.isAnyBlank(moduleName, className, basePackage, outputPath, tableName, urlPath, createTableSql)) {
@ -93,6 +100,7 @@ public class CodeGenerator {
config.setOutputPath(outputPath); config.setOutputPath(outputPath);
config.setTableName(tableName); config.setTableName(tableName);
config.setUrlPath(urlPath); config.setUrlPath(urlPath);
config.setThirdPartyApi(isThirdPartyApi);
// 3. 生成代码 // 3. 生成代码
CodeGeneratorUtils.generateCode(createTableSql, config); CodeGeneratorUtils.generateCode(createTableSql, config);

View File

@ -55,6 +55,8 @@ public class CodeGeneratorUtils {
private String urlPath; // URL路径 private String urlPath; // URL路径
private List<FieldInfo> fields; // 字段信息 private List<FieldInfo> fields; // 字段信息
private boolean isThirdPartyApi; // 是否为三方APItrue: 三方API, false: 二方API
} }
private static final Configuration configuration; private static final Configuration configuration;
@ -83,9 +85,17 @@ public class CodeGeneratorUtils {
generateFile("repository.ftl", config, "/repository/I" + config.getClassName() + "Repository.java"); generateFile("repository.ftl", config, "/repository/I" + config.getClassName() + "Repository.java");
generateFile("service.ftl", config, "/service/I" + config.getClassName() + "Service.java"); generateFile("service.ftl", config, "/service/I" + config.getClassName() + "Service.java");
generateFile("serviceImpl.ftl", config, "/service/impl/" + config.getClassName() + "ServiceImpl.java"); generateFile("serviceImpl.ftl", config, "/service/impl/" + config.getClassName() + "ServiceImpl.java");
generateFile("controller.ftl", config, "/controller/" + config.getClassName() + "ApiController.java");
generateFile("converter.ftl", config, "/converter/" + config.getClassName() + "Converter.java"); generateFile("converter.ftl", config, "/converter/" + config.getClassName() + "Converter.java");
// 根据API类型生成不同的Controller
if (config.isThirdPartyApi()) {
// 三方API生成在api包下类名为XxxApiController
generateFile("controller.ftl", config, "/api/" + config.getClassName() + "ApiController.java");
} else {
// 二方API生成在controller包下类名为XxxController
generateFile("controller.ftl", config, "/controller/" + config.getClassName() + "Controller.java");
}
log.info("代码生成完成,输出路径: {}", config.getOutputPath()); log.info("代码生成完成,输出路径: {}", config.getOutputPath());
} catch (Exception e) { } catch (Exception e) {
log.error("代码生成失败", e); log.error("代码生成失败", e);
@ -204,6 +214,7 @@ public class CodeGeneratorUtils {
dataModel.put("tableName", config.getTableName()); dataModel.put("tableName", config.getTableName());
dataModel.put("fields", config.getFields()); dataModel.put("fields", config.getFields());
dataModel.put("urlPath", config.getUrlPath()); dataModel.put("urlPath", config.getUrlPath());
dataModel.put("isThirdPartyApi", config.isThirdPartyApi());
// 生成文件 // 生成文件
try (Writer writer = new FileWriter(outputFile)) { try (Writer writer = new FileWriter(outputFile)) {

View File

@ -1,4 +1,4 @@
package ${basePackage}.controller; package ${basePackage}.<#if isThirdPartyApi!false>api<#else>controller</#if>;
import com.qqchen.deploy.backend.framework.controller.BaseController; import com.qqchen.deploy.backend.framework.controller.BaseController;
import ${basePackage}.entity.${className}; import ${basePackage}.entity.${className};
@ -18,7 +18,7 @@ import java.util.List;
@RestController @RestController
@RequestMapping("/api/v1/${urlPath}") @RequestMapping("/api/v1/${urlPath}")
@Tag(name = "${moduleName}管理", description = "${moduleName}管理相关接口") @Tag(name = "${moduleName}管理", description = "${moduleName}管理相关接口")
public class ${className}ApiController extends BaseController<${className}, ${className}DTO, Long, ${className}Query> { public class ${className}<#if isThirdPartyApi!false>Api</#if>Controller extends BaseController<${className}, ${className}DTO, Long, ${className}Query> {
@Override @Override
protected void exportData(HttpServletResponse response, List<${className}DTO> data) { protected void exportData(HttpServletResponse response, List<${className}DTO> data) {

View File

@ -8,5 +8,5 @@ import ${basePackage}.query.${className}Query;
/** /**
* ${moduleName} Service接口 * ${moduleName} Service接口
*/ */
public interface I${className}Service extends IBaseService<${className}, ${className}DTO, Long, ${className}Query> { public interface I${className}Service extends IBaseService<${className}, ${className}DTO, ${className}Query, Long> {
} }

View File

@ -13,6 +13,6 @@ import org.springframework.stereotype.Service;
*/ */
@Slf4j @Slf4j
@Service @Service
public class ${className}ServiceImpl extends BaseServiceImpl<${className}, ${className}DTO, Long, ${className}Query> public class ${className}ServiceImpl extends BaseServiceImpl<${className}, ${className}DTO, ${className}Query, Long>
implements I${className}Service { implements I${className}Service {
} }