|
1 | 1 | package com.softdev.system.generator.controller; |
2 | 2 |
|
3 | 3 | import com.softdev.system.generator.entity.ClassInfo; |
| 4 | +import com.softdev.system.generator.entity.ParamInfo; |
4 | 5 | import com.softdev.system.generator.entity.ReturnT; |
| 6 | +import com.softdev.system.generator.service.GeneratorService; |
| 7 | +import com.softdev.system.generator.util.CodeGenerateException; |
5 | 8 | import com.softdev.system.generator.util.FreemarkerTool; |
6 | 9 | import com.softdev.system.generator.util.TableParseUtil; |
7 | 10 | import freemarker.template.TemplateException; |
8 | 11 | import lombok.extern.slf4j.Slf4j; |
9 | 12 | import org.apache.commons.lang3.StringUtils; |
10 | 13 | import org.springframework.beans.factory.annotation.Autowired; |
11 | 14 | import org.springframework.stereotype.Controller; |
12 | | -import org.springframework.web.bind.annotation.RequestMapping; |
13 | | -import org.springframework.web.bind.annotation.RequestParam; |
14 | | -import org.springframework.web.bind.annotation.ResponseBody; |
| 15 | +import org.springframework.web.bind.annotation.*; |
15 | 16 |
|
16 | 17 | import java.io.IOException; |
17 | 18 | import java.util.HashMap; |
|
26 | 27 | public class IndexController { |
27 | 28 |
|
28 | 29 | @Autowired |
29 | | - private FreemarkerTool freemarkerTool; |
| 30 | + private GeneratorService generatorService; |
30 | 31 |
|
31 | | - @RequestMapping("/") |
| 32 | + @GetMapping("/") |
32 | 33 | public String index() { |
33 | 34 | return "index"; |
34 | 35 | } |
35 | 36 |
|
36 | | - @RequestMapping("/genCode") |
| 37 | + @PostMapping("/genCode") |
37 | 38 | @ResponseBody |
38 | | - public ReturnT<Map<String, String>> codeGenerate(String tableSql, |
39 | | - //2019-2-10 liutf 修改为@RequestParam参数校验 |
40 | | - @RequestParam(required = false, defaultValue = "大狼狗") String authorName, |
41 | | - @RequestParam(required = false, defaultValue = "com.softdev.system")String packageName, |
42 | | - @RequestParam(required = false, defaultValue = "ApiReturnUtil")String returnUtil, |
43 | | - @RequestParam(required = false, defaultValue = "true")boolean isUnderLineToCamelCase, |
44 | | - @RequestParam(required = false, defaultValue = "boolean")String tinyintTransType |
45 | | - ) { |
46 | | - |
| 39 | + public ReturnT<Map<String, String>> codeGenerate( ParamInfo paramInfo ) { |
47 | 40 |
|
48 | 41 | try { |
49 | 42 |
|
50 | | - if (StringUtils.isBlank(tableSql)) { |
| 43 | + if (StringUtils.isBlank(paramInfo.getTableSql())) { |
51 | 44 | return new ReturnT<>(ReturnT.FAIL_CODE, "表结构信息不可为空"); |
52 | 45 | } |
53 | 46 |
|
54 | 47 | // parse table |
55 | | - ClassInfo classInfo = TableParseUtil.processTableIntoClassInfo(tableSql, isUnderLineToCamelCase, tinyintTransType); |
| 48 | + ClassInfo classInfo = null; |
| 49 | + switch (paramInfo.getDataType()){ |
| 50 | + //parse json |
| 51 | + case "json":classInfo = TableParseUtil.processJsonToClassInfo(paramInfo);break; |
| 52 | + //parse sql by regex |
| 53 | + case "sql-regex":classInfo = TableParseUtil.processTableToClassInfoByRegex(paramInfo);break; |
| 54 | + //default parse sql by java |
| 55 | + default : classInfo = TableParseUtil.processTableIntoClassInfo(paramInfo);break; |
| 56 | + } |
56 | 57 |
|
57 | | - // code genarete |
| 58 | + // process the param |
58 | 59 | Map<String, Object> params = new HashMap<String, Object>(8); |
59 | 60 | params.put("classInfo", classInfo); |
60 | | - params.put("authorName", authorName); |
61 | | - params.put("packageName", packageName); |
62 | | - params.put("returnUtil", returnUtil); |
63 | | - |
64 | | - // result |
65 | | - Map<String, String> result = new HashMap<String, String>(32); |
| 61 | + params.put("authorName", paramInfo.getAuthorName()); |
| 62 | + params.put("packageName", paramInfo.getPackageName()); |
| 63 | + params.put("returnUtil", paramInfo.getReturnUtil()); |
66 | 64 |
|
67 | | - //UI |
68 | | - result.put("swagger-ui", freemarkerTool.processString("code-generator/ui/swagger-ui.ftl", params)); |
69 | | - result.put("element-ui", freemarkerTool.processString("code-generator/ui/element-ui.ftl", params)); |
70 | | - result.put("bootstrap-ui", freemarkerTool.processString("code-generator/ui/bootstrap-ui.ftl", params)); |
71 | | - //mybatis old |
72 | | - result.put("controller", freemarkerTool.processString("code-generator/mybatis/controller.ftl", params)); |
73 | | - result.put("service", freemarkerTool.processString("code-generator/mybatis/service.ftl", params)); |
74 | | - result.put("service_impl", freemarkerTool.processString("code-generator/mybatis/service_impl.ftl", params)); |
75 | | - result.put("mapper", freemarkerTool.processString("code-generator/mybatis/mapper.ftl", params)); |
76 | | - result.put("mybatis", freemarkerTool.processString("code-generator/mybatis/mybatis.ftl", params)); |
77 | | - result.put("model", freemarkerTool.processString("code-generator/mybatis/model.ftl", params)); |
78 | | - //jpa |
79 | | - result.put("entity", freemarkerTool.processString("code-generator/jpa/entity.ftl", params)); |
80 | | - result.put("repository", freemarkerTool.processString("code-generator/jpa/repository.ftl", params)); |
81 | | - result.put("jpacontroller", freemarkerTool.processString("code-generator/jpa/jpacontroller.ftl", params)); |
82 | | - //jdbc template |
83 | | - result.put("jtdao", freemarkerTool.processString("code-generator/jdbc-template/jtdao.ftl", params)); |
84 | | - result.put("jtdaoimpl", freemarkerTool.processString("code-generator/jdbc-template/jtdaoimpl.ftl", params)); |
85 | | - //beetsql |
86 | | - result.put("beetlmd", freemarkerTool.processString("code-generator/beetlsql/beetlmd.ftl", params)); |
87 | | - result.put("beetlentity", freemarkerTool.processString("code-generator/beetlsql/beetlentity.ftl", params)); |
88 | | - result.put("beetlentitydto", freemarkerTool.processString("code-generator/beetlsql/beetlentitydto.ftl", params)); |
89 | | - result.put("beetlcontroller", freemarkerTool.processString("code-generator/beetlsql/beetlcontroller.ftl", params)); |
90 | | - //mybatis plus |
91 | | - result.put("pluscontroller", freemarkerTool.processString("code-generator/mybatis-plus/pluscontroller.ftl", params)); |
92 | | - result.put("plusmapper", freemarkerTool.processString("code-generator/mybatis-plus/plusmapper.ftl", params)); |
93 | | - //util |
94 | | - result.put("util", freemarkerTool.processString("code-generator/util/util.ftl", params)); |
95 | | - result.put("json", freemarkerTool.processString("code-generator/util/json.ftl", params)); |
96 | | - result.put("xml", freemarkerTool.processString("code-generator/util/xml.ftl", params)); |
97 | | - //sql generate |
98 | | - result.put("select", freemarkerTool.processString("code-generator/sql/select.ftl", params)); |
99 | | - result.put("insert", freemarkerTool.processString("code-generator/sql/insert.ftl", params)); |
100 | | - result.put("update", freemarkerTool.processString("code-generator/sql/update.ftl", params)); |
101 | | - result.put("delete", freemarkerTool.processString("code-generator/sql/delete.ftl", params)); |
| 65 | + // generate the code 需要加新的模板请在里面改 |
| 66 | + Map<String, String> result = generatorService.getResultByParams(params); |
102 | 67 |
|
103 | | - // 计算,生成代码行数 |
104 | | - int lineNum = 0; |
105 | | - for (Map.Entry<String, String> item: result.entrySet()) { |
106 | | - if (item.getValue() != null) { |
107 | | - lineNum += StringUtils.countMatches(item.getValue(), "\n"); |
108 | | - } |
109 | | - } |
110 | | - log.info("生成代码行数:{}", lineNum); |
111 | | - //测试环境可自行开启 |
112 | | - //log.info("生成代码数据:{}", result); |
113 | 68 | return new ReturnT<>(result); |
114 | 69 | } catch (IOException | TemplateException e) { |
115 | 70 | log.error(e.getMessage(), e); |
116 | | - return new ReturnT<>(ReturnT.FAIL_CODE, "表结构解析失败"+e.getMessage()); |
| 71 | + return new ReturnT<>(ReturnT.FAIL_CODE, e.getMessage()); |
| 72 | + } catch (CodeGenerateException e) { |
| 73 | + log.error(e.getMessage(), e); |
| 74 | + return new ReturnT<>(ReturnT.FAIL_CODE, e.getMessage()); |
117 | 75 | } |
118 | 76 |
|
119 | 77 | } |
|
0 commit comments