|
1 |
| -package com.softdev.system.generator.controller; |
2 |
| - |
3 |
| -import com.alibaba.fastjson.JSON; |
4 |
| -import com.softdev.system.generator.entity.ClassInfo; |
5 |
| -import com.softdev.system.generator.entity.ParamInfo; |
6 |
| -import com.softdev.system.generator.entity.ReturnT; |
7 |
| -import com.softdev.system.generator.service.GeneratorService; |
8 |
| -import com.softdev.system.generator.util.CodeGenerateException; |
9 |
| -import com.softdev.system.generator.util.TableParseUtil; |
10 |
| -import freemarker.template.TemplateException; |
11 |
| -import lombok.extern.slf4j.Slf4j; |
12 |
| -import org.apache.commons.lang3.StringUtils; |
13 |
| -import org.springframework.beans.factory.annotation.Autowired; |
14 |
| -import org.springframework.stereotype.Controller; |
15 |
| -import org.springframework.web.bind.annotation.GetMapping; |
16 |
| -import org.springframework.web.bind.annotation.PostMapping; |
17 |
| -import org.springframework.web.bind.annotation.RequestBody; |
18 |
| -import org.springframework.web.bind.annotation.ResponseBody; |
19 |
| - |
20 |
| -import java.io.IOException; |
21 |
| -import java.util.HashMap; |
22 |
| -import java.util.Map; |
23 |
| - |
24 |
| -/** |
25 |
| - * spring boot code generator |
26 |
| - * @author zhengk/moshow |
27 |
| - */ |
28 |
| -@Controller |
29 |
| -@Slf4j |
30 |
| -public class IndexController { |
31 |
| - |
32 |
| - @Autowired |
33 |
| - private GeneratorService generatorService; |
34 |
| - |
35 |
| - @GetMapping("/") |
36 |
| - public String index() { |
37 |
| - return "index"; |
38 |
| - } |
39 |
| - |
40 |
| - @PostMapping("/genCode") |
41 |
| - @ResponseBody |
42 |
| - public ReturnT<Map<String, String>> codeGenerate(@RequestBody ParamInfo paramInfo ) { |
43 |
| - |
44 |
| - try { |
45 |
| - |
46 |
| - if (StringUtils.isBlank(paramInfo.getTableSql())) { |
47 |
| - return new ReturnT<>(ReturnT.FAIL_CODE, "表结构信息不可为空"); |
48 |
| - } |
49 |
| - |
50 |
| - // parse table |
51 |
| - ClassInfo classInfo = null; |
52 |
| - switch (paramInfo.getDataType()){ |
53 |
| - //parse json |
54 |
| - case "json":classInfo = TableParseUtil.processJsonToClassInfo(paramInfo);break; |
55 |
| - //parse sql by regex |
56 |
| - case "sql-regex":classInfo = TableParseUtil.processTableToClassInfoByRegex(paramInfo);break; |
57 |
| - //default parse sql by java |
58 |
| - default : classInfo = TableParseUtil.processTableIntoClassInfo(paramInfo);break; |
59 |
| - } |
60 |
| - |
61 |
| - // process the param |
62 |
| - Map<String, Object> params = new HashMap<String, Object>(8); |
63 |
| - params.put("classInfo", classInfo); |
64 |
| - params.put("tableName", classInfo==null?System.currentTimeMillis():classInfo.getTableName()); |
65 |
| - params.put("authorName", paramInfo.getAuthorName()); |
66 |
| - params.put("packageName", paramInfo.getPackageName()); |
67 |
| - params.put("returnUtil", paramInfo.getReturnUtil()); |
68 |
| - params.put("swagger", paramInfo.isSwagger()); |
69 |
| - |
70 |
| - //log the params |
71 |
| - //log.info(JSON.toJSONString(paramInfo)); |
72 |
| - |
73 |
| - log.info("generator table:"+(classInfo==null?"":classInfo.getTableName()) |
74 |
| - +",field size:"+((classInfo==null||classInfo.getFieldList()==null)?"":classInfo.getFieldList().size())); |
75 |
| - |
76 |
| - // generate the code 需要加新的模板请在里面改 |
77 |
| - Map<String, String> result = generatorService.getResultByParams(params); |
78 |
| - |
79 |
| - return new ReturnT<>(result); |
80 |
| - } catch (IOException | TemplateException e) { |
81 |
| - log.error(e.getMessage(), e); |
82 |
| - return new ReturnT<>(ReturnT.FAIL_CODE, e.getMessage()); |
83 |
| - } catch (CodeGenerateException e) { |
84 |
| - log.error(e.getMessage(), e); |
85 |
| - return new ReturnT<>(ReturnT.FAIL_CODE, e.getMessage()); |
86 |
| - } |
87 |
| - |
88 |
| - } |
89 |
| - |
90 |
| -} |
| 1 | +package com.softdev.system.generator.controller; |
| 2 | + |
| 3 | +import com.alibaba.fastjson.JSON; |
| 4 | +import com.softdev.system.generator.entity.ClassInfo; |
| 5 | +import com.softdev.system.generator.entity.ParamInfo; |
| 6 | +import com.softdev.system.generator.entity.ReturnT; |
| 7 | +import com.softdev.system.generator.service.GeneratorService; |
| 8 | +import com.softdev.system.generator.util.CodeGenerateException; |
| 9 | +import com.softdev.system.generator.util.TableParseUtil; |
| 10 | +import freemarker.template.TemplateException; |
| 11 | +import lombok.extern.slf4j.Slf4j; |
| 12 | +import org.apache.commons.lang3.StringUtils; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.stereotype.Controller; |
| 15 | +import org.springframework.web.bind.annotation.GetMapping; |
| 16 | +import org.springframework.web.bind.annotation.PostMapping; |
| 17 | +import org.springframework.web.bind.annotation.RequestBody; |
| 18 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +/** |
| 25 | + * spring boot code generator |
| 26 | + * @author zhengk/moshow |
| 27 | + */ |
| 28 | +@Controller |
| 29 | +@Slf4j |
| 30 | +public class IndexController { |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private GeneratorService generatorService; |
| 34 | + |
| 35 | + @GetMapping("/") |
| 36 | + public String index() { |
| 37 | + return "index"; |
| 38 | + } |
| 39 | + |
| 40 | + @PostMapping("/genCode") |
| 41 | + @ResponseBody |
| 42 | + public ReturnT<Map<String, String>> codeGenerate(@RequestBody ParamInfo paramInfo ) { |
| 43 | + |
| 44 | + try { |
| 45 | + |
| 46 | + if (StringUtils.isBlank(paramInfo.getTableSql())) { |
| 47 | + return new ReturnT<>(ReturnT.FAIL_CODE, "表结构信息不可为空"); |
| 48 | + } |
| 49 | + |
| 50 | + // parse table |
| 51 | + ClassInfo classInfo = null; |
| 52 | + switch (paramInfo.getDataType()){ |
| 53 | + //parse json |
| 54 | + case "json":classInfo = TableParseUtil.processJsonToClassInfo(paramInfo);break; |
| 55 | + //parse sql by regex |
| 56 | + case "sql-regex":classInfo = TableParseUtil.processTableToClassInfoByRegex(paramInfo);break; |
| 57 | + //default parse sql by java |
| 58 | + default : classInfo = TableParseUtil.processTableIntoClassInfo(paramInfo);break; |
| 59 | + } |
| 60 | + |
| 61 | + // process the param |
| 62 | + Map<String, Object> params = new HashMap<String, Object>(8); |
| 63 | + params.put("classInfo", classInfo); |
| 64 | + params.put("tableName", classInfo==null?System.currentTimeMillis():classInfo.getTableName()); |
| 65 | + params.put("authorName", paramInfo.getAuthorName()); |
| 66 | + params.put("packageName", paramInfo.getPackageName()); |
| 67 | + params.put("returnUtil", paramInfo.getReturnUtil()); |
| 68 | + params.put("swagger", paramInfo.isSwagger()); |
| 69 | + |
| 70 | + //log the params |
| 71 | + //log.info(JSON.toJSONString(paramInfo)); |
| 72 | + |
| 73 | + log.info("generator table:"+(classInfo==null?"":classInfo.getTableName()) |
| 74 | + +",field size:"+((classInfo==null||classInfo.getFieldList()==null)?"":classInfo.getFieldList().size())); |
| 75 | + |
| 76 | + // generate the code 需要加新的模板请在里面改 |
| 77 | + Map<String, String> result = generatorService.getResultByParams(params); |
| 78 | + |
| 79 | + return new ReturnT<>(result); |
| 80 | + } catch (IOException | TemplateException | CodeGenerateException e) { |
| 81 | + log.error(e.getMessage(), e); |
| 82 | + return new ReturnT<>(ReturnT.FAIL_CODE, e.getMessage()); |
| 83 | + } |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments