|
|
@@ -32,59 +32,60 @@ import static com.railway.generator.util.StringPlusUtils.underlineToCamelCase;
|
|
|
@Slf4j
|
|
|
public class IndexController {
|
|
|
|
|
|
- @Autowired
|
|
|
- private FreemarkerTool freemarkerTool;
|
|
|
+ @Autowired
|
|
|
+ private FreemarkerTool freemarkerTool;
|
|
|
|
|
|
- @RequestMapping("/")
|
|
|
- public String index() {
|
|
|
- return "index";
|
|
|
- }
|
|
|
-
|
|
|
- @RequestMapping("/genCode")
|
|
|
- @ResponseBody
|
|
|
- public ReturnT<Map<String, String>> codeGenerate(String tableSql,
|
|
|
- //2019-2-10 liutf 修改为@RequestParam参数校验
|
|
|
- @RequestParam(required = false, defaultValue = "author") String authorName,
|
|
|
- @RequestParam(required = false, defaultValue = "com.railway.business.catenary") String packageName,
|
|
|
- @RequestParam(required = false, defaultValue = "") String className,
|
|
|
- @RequestParam(required = false, defaultValue = "id") String primaryKey,
|
|
|
- @RequestParam(required = false, defaultValue = "/Users/wuhonghao/IdeaProjects/railway") String projectPath,
|
|
|
- @RequestParam(required = false, defaultValue = "ApiReturnUtil") String returnUtil
|
|
|
- ) {
|
|
|
+ @RequestMapping("/")
|
|
|
+ public String index() {
|
|
|
+ return "index";
|
|
|
+ }
|
|
|
|
|
|
+ @RequestMapping("/genCode")
|
|
|
+ @ResponseBody
|
|
|
+ public ReturnT<Map<String, String>> codeGenerate(String tableSql,
|
|
|
+ //2019-2-10 liutf 修改为@RequestParam参数校验
|
|
|
+ @RequestParam(required = false, defaultValue = "author") String authorName,
|
|
|
+ @RequestParam(required = false, defaultValue = "com.railway.business.catenary") String packageName,
|
|
|
+ @RequestParam(required = false, defaultValue = "") String className,
|
|
|
+ @RequestParam(required = false, defaultValue = "id") String primaryKey,
|
|
|
+ @RequestParam(required = false, defaultValue = "D:/railway/") String projectPath,
|
|
|
+ @RequestParam(required = false, defaultValue = "ApiReturnUtil") String returnUtil
|
|
|
+ ) {
|
|
|
|
|
|
- try {
|
|
|
+ try {
|
|
|
|
|
|
- if (StringUtils.isBlank(tableSql)) {
|
|
|
- return new ReturnT<>(ReturnT.FAIL_CODE, "表结构信息不可为空");
|
|
|
- }
|
|
|
+ if (StringUtils.isBlank(tableSql)) {
|
|
|
+ return new ReturnT<>(ReturnT.FAIL_CODE, "表结构信息不可为空");
|
|
|
+ }
|
|
|
|
|
|
- // parse table
|
|
|
- ClassInfo classInfo = CodeGeneratorTool.processTableIntoClassInfo(tableSql);
|
|
|
+ // parse table
|
|
|
+ ClassInfo classInfo = CodeGeneratorTool.processTableIntoClassInfo(tableSql);
|
|
|
|
|
|
- // code genarete
|
|
|
- Map<String, Object> params = new HashMap<String, Object>();
|
|
|
- if (StringUtils.isNotEmpty(className)) {
|
|
|
- classInfo.setClassName(className);
|
|
|
- }
|
|
|
- String primaryKeyCame = underlineToCamelCase(primaryKey);
|
|
|
- String restPath = packageName.substring(packageName.lastIndexOf(".") + 1) + camelCaseToUnderline(className).replace("_", "/");
|
|
|
- params.put("classInfo", classInfo);
|
|
|
- params.put("authorName", authorName);
|
|
|
- params.put("packageName", packageName);
|
|
|
- params.put("returnUtil", returnUtil);
|
|
|
- params.put("primaryKey", primaryKey);
|
|
|
- params.put("primaryKeyCame", primaryKeyCame);
|
|
|
- params.put("restPath", restPath);
|
|
|
- List<FieldInfo> list = new ArrayList();
|
|
|
- for (FieldInfo fieldInfo : classInfo.getFieldList()) {
|
|
|
- if (!fieldInfo.getFieldName().equals(primaryKeyCame)) {
|
|
|
- list.add(fieldInfo);
|
|
|
- }
|
|
|
- }
|
|
|
- classInfo.setFieldList(list);
|
|
|
- // result
|
|
|
- Map<String, String> result = new HashMap<String, String>();
|
|
|
+ // code genarete
|
|
|
+ Map<String, Object> params = new HashMap<String, Object>();
|
|
|
+ if (StringUtils.isNotEmpty(className)) {
|
|
|
+ classInfo.setClassName(className);
|
|
|
+ }
|
|
|
+ String primaryKeyCame = underlineToCamelCase(primaryKey);
|
|
|
+ String restPath =
|
|
|
+ packageName.substring(packageName.lastIndexOf(".") + 1) + camelCaseToUnderline(
|
|
|
+ className).replace("_", "/");
|
|
|
+ params.put("classInfo", classInfo);
|
|
|
+ params.put("authorName", authorName);
|
|
|
+ params.put("packageName", packageName);
|
|
|
+ params.put("returnUtil", returnUtil);
|
|
|
+ params.put("primaryKey", primaryKey);
|
|
|
+ params.put("primaryKeyCame", primaryKeyCame);
|
|
|
+ params.put("restPath", restPath);
|
|
|
+ List<FieldInfo> list = new ArrayList();
|
|
|
+ for (FieldInfo fieldInfo : classInfo.getFieldList()) {
|
|
|
+ if (!fieldInfo.getFieldName().equals(primaryKeyCame)) {
|
|
|
+ list.add(fieldInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ classInfo.setFieldList(list);
|
|
|
+ // result
|
|
|
+ Map<String, String> result = new HashMap<String, String>();
|
|
|
//
|
|
|
// //UI
|
|
|
// result.put("swagger-ui", freemarkerTool.processString("code-generator/ui/swagger-ui.ftl", params));
|
|
|
@@ -112,20 +113,39 @@ public class IndexController {
|
|
|
// //mybatis plus
|
|
|
// result.put("pluscontroller", freemarkerTool.processString("code-generator/mybatis-plus/pluscontroller.ftl", params));
|
|
|
// result.put("plusmapper", freemarkerTool.processString("code-generator/mybatis-plus/plusmapper.ftl", params));
|
|
|
- String controllerPath = projectPath + File.separator + "railway-admin/src/main/java/com/railway/web/controller/business/catenary" + File.separator + classInfo.getClassName() + "Controller.java";
|
|
|
- String servicePath = projectPath + File.separator + "railway-business/src/main/java/com/railway/business/catenary" + File.separator + "service" + File.separator + "I"+classInfo.getClassName() + "Service.java";
|
|
|
- String serviceImplPath = projectPath + File.separator + "railway-business/src/main/java/com/railway/business//catenary" + File.separator + "service" + File.separator + "impl" + File.separator + classInfo.getClassName() + "ServiceImpl.java";
|
|
|
- String daoPath = projectPath + File.separator + "railway-business/src/main/java/com/railway/business/catenary" + File.separator + "mapper" + File.separator + classInfo.getClassName() + "Mapper.java";
|
|
|
- String entityPath = projectPath + File.separator + "railway-business/src/main/java/com/railway/business/catenary" + File.separator + "domain" + File.separator + classInfo.getClassName() + ".java";
|
|
|
- String mapperPath = projectPath + File.separator + "railway-business/src/main/resources/mapper/business" + File.separator + classInfo.getClassName() + "Mapper.xml";
|
|
|
- //写入文件
|
|
|
- FileUtil.writeToFile(controllerPath, freemarkerTool.processString("code-generator/railway/back/controller.ftl", params));
|
|
|
- FileUtil.writeToFile(servicePath, freemarkerTool.processString("code-generator/railway/back/service.ftl", params));
|
|
|
- FileUtil.writeToFile(serviceImplPath, freemarkerTool.processString("code-generator/railway/back/serviceImpl.ftl", params));
|
|
|
- FileUtil.writeToFile(daoPath, freemarkerTool.processString("code-generator/railway/back/dao.ftl", params));
|
|
|
- FileUtil.writeToFile(entityPath, freemarkerTool.processString("code-generator/railway/back/entity.ftl", params));
|
|
|
- FileUtil.writeToFile(mapperPath, freemarkerTool.processString("code-generator/railway/back/mybatis.ftl", params));
|
|
|
- // 计算,生成代码行数
|
|
|
+ String controllerPath = projectPath + File.separator
|
|
|
+ + "railway-admin/src/main/java/com/railway/web/controller/business/catenary"
|
|
|
+ + File.separator + classInfo.getClassName() + "Controller.java";
|
|
|
+ String servicePath = projectPath + File.separator
|
|
|
+ + "railway-business/src/main/java/com/railway/business/catenary" + File.separator
|
|
|
+ + "service" + File.separator + "I" + classInfo.getClassName() + "Service.java";
|
|
|
+ String serviceImplPath = projectPath + File.separator
|
|
|
+ + "railway-business/src/main/java/com/railway/business//catenary" + File.separator
|
|
|
+ + "service" + File.separator + "impl" + File.separator + classInfo.getClassName()
|
|
|
+ + "ServiceImpl.java";
|
|
|
+ String daoPath = projectPath + File.separator
|
|
|
+ + "railway-business/src/main/java/com/railway/business/catenary" + File.separator
|
|
|
+ + "mapper" + File.separator + classInfo.getClassName() + "Mapper.java";
|
|
|
+ String entityPath = projectPath + File.separator
|
|
|
+ + "railway-business/src/main/java/com/railway/business/catenary" + File.separator
|
|
|
+ + "domain" + File.separator + classInfo.getClassName() + ".java";
|
|
|
+ String mapperPath =
|
|
|
+ projectPath + File.separator + "railway-business/src/main/resources/mapper/business"
|
|
|
+ + File.separator + classInfo.getClassName() + "Mapper.xml";
|
|
|
+ //写入文件
|
|
|
+ FileUtil.writeToFile(controllerPath,
|
|
|
+ freemarkerTool.processString("code-generator/railway/back/controller.ftl", params));
|
|
|
+ FileUtil.writeToFile(servicePath,
|
|
|
+ freemarkerTool.processString("code-generator/railway/back/service.ftl", params));
|
|
|
+ FileUtil.writeToFile(serviceImplPath,
|
|
|
+ freemarkerTool.processString("code-generator/railway/back/serviceImpl.ftl", params));
|
|
|
+ FileUtil.writeToFile(daoPath,
|
|
|
+ freemarkerTool.processString("code-generator/railway/back/dao.ftl", params));
|
|
|
+ FileUtil.writeToFile(entityPath,
|
|
|
+ freemarkerTool.processString("code-generator/railway/back/entity.ftl", params));
|
|
|
+ FileUtil.writeToFile(mapperPath,
|
|
|
+ freemarkerTool.processString("code-generator/railway/back/mybatis.ftl", params));
|
|
|
+ // 计算,生成代码行数
|
|
|
// int lineNum = 0;
|
|
|
// for (Map.Entry<String, String> item : result.entrySet()) {
|
|
|
// if (item.getValue() != null) {
|
|
|
@@ -133,14 +153,14 @@ public class IndexController {
|
|
|
// }
|
|
|
// }
|
|
|
// log.info("生成代码行数:{}", lineNum);
|
|
|
- //测试环境可自行开启
|
|
|
- //log.info("生成代码数据:{}", result);
|
|
|
- return new ReturnT<>(result);
|
|
|
- } catch (IOException | TemplateException e) {
|
|
|
- log.error(e.getMessage(), e);
|
|
|
- return new ReturnT<>(ReturnT.FAIL_CODE, "表结构解析失败" + e.getMessage());
|
|
|
- }
|
|
|
-
|
|
|
+ //测试环境可自行开启
|
|
|
+ //log.info("生成代码数据:{}", result);
|
|
|
+ return new ReturnT<>(result);
|
|
|
+ } catch (IOException | TemplateException e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ return new ReturnT<>(ReturnT.FAIL_CODE, "表结构解析失败" + e.getMessage());
|
|
|
}
|
|
|
|
|
|
+ }
|
|
|
+
|
|
|
}
|