Skip to content

Commit 0341e53

Browse files
author
moshowgame
committed
代码重构!异常处理优化,Freemarker相关工具类优化,简化模板生成部分,通过template.json来配置需要生成的模板,不需要配置java文件。
1 parent 6149d99 commit 0341e53

18 files changed

+377
-298
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
|更新日期|更新内容|
2929
|-|-|
30+
|20200517|1.代码重构!异常处理优化,Freemarker相关工具类优化,简化模板生成部分,通过template.json来配置需要生成的模板,不需要配置java文件。|
3031
|20200503|1.优化对特殊字符的处理,对于包含#和$等特殊字符的,在模板使用井和¥代替便可,escapeString方法会自动处理<br> 2.优化mybatisplus实体类相关(感谢@chunchengmeigui的反馈) 3.修优化对所有类型的判断(感谢@cnlw的反馈) 4.移除swagger-entity,该功能已经包含在‘swagger-ui’的下拉选项中 5.升级hutool和lombok版本|
3132
|20200306|1.提交一套layuimini+mybatisplus的模板. 2.修复mybatisplus一些相关问题. |
3233
|20200206|1.新增历史记录功能,自动保存最近生成的对象 2.新增swagger开关选项和修复@Column带name参数(感谢@liuyu-struggle的建议) 3.去除mybatis模板中的方括号[]和修改模板里的类注释样式(感谢@gaohanghang的PR)|
@@ -65,7 +66,7 @@
6566
|20180913|修复字段没有描述以及类型为DATE型导致的问题.新增JPA的Controller模板.|
6667
|20180831|初始化项目.新增JPA系列Entity+Repository模板.|
6768

68-
# FieldName
69+
# ClassInfo/TableInfo
6970
|字段名|说明|
7071
|-|-|
7172
|packageName|自定义的包名|
@@ -77,6 +78,12 @@
7778
|fieldName|字段名|
7879
|fieldComment|字段备注|
7980

81+
# how to add a new template
82+
1. code-generator中找到对应分类,新增一个.ftl文件
83+
2. 根据类信息编写freemarker模板.ftl文件
84+
3. 修改template.json文件,新增模板信息
85+
4. index页面增加一个button
86+
5. reload,test,complete
8087

8188
<img src="./codegenerator1.png">
8289
<img src="./codegenerator2.png">

codegenerator1.png

-73.6 KB
Loading

codegenerator2.png

28.8 KB
Loading

codegenerator3.png

-4.01 KB
Loading

codegenerator4.png

-3.81 KB
Loading
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
package com.softdev.system.generator.config;
2-
3-
import javax.servlet.http.HttpServletRequest;
4-
5-
import com.softdev.system.generator.entity.ReturnT;
6-
import org.springframework.web.bind.annotation.ControllerAdvice;
7-
import org.springframework.web.bind.annotation.ExceptionHandler;
8-
import org.springframework.web.bind.annotation.ResponseBody;
9-
10-
@ControllerAdvice
11-
public class GlobalDefaultExceptionHandler {
12-
13-
@ExceptionHandler(Exception.class)
14-
@ResponseBody
15-
public ReturnT defaultExceptionHandler(HttpServletRequest req,Exception e) {
16-
e.printStackTrace();
17-
return new ReturnT<>(ReturnT.FAIL_CODE, e.getMessage());
18-
}
19-
20-
}
1+
package com.softdev.system.generator.config;
2+
3+
import com.softdev.system.generator.entity.ReturnT;
4+
import org.springframework.web.bind.annotation.ControllerAdvice;
5+
import org.springframework.web.bind.annotation.ExceptionHandler;
6+
import org.springframework.web.bind.annotation.ResponseBody;
7+
8+
import javax.servlet.http.HttpServletRequest;
9+
10+
@ControllerAdvice
11+
public class GlobalDefaultExceptionHandler {
12+
13+
@ExceptionHandler(Exception.class)
14+
@ResponseBody
15+
public ReturnT defaultExceptionHandler(HttpServletRequest req,Exception e) {
16+
e.printStackTrace();
17+
return new ReturnT<>(ReturnT.FAIL_CODE, e.getMessage());
18+
}
19+
20+
}
Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
package com.softdev.system.generator.controller;
22

3-
import com.alibaba.fastjson.JSON;
43
import com.softdev.system.generator.entity.ClassInfo;
54
import com.softdev.system.generator.entity.ParamInfo;
65
import com.softdev.system.generator.entity.ReturnT;
76
import com.softdev.system.generator.service.GeneratorService;
8-
import com.softdev.system.generator.util.CodeGenerateException;
97
import com.softdev.system.generator.util.TableParseUtil;
10-
import freemarker.template.TemplateException;
118
import lombok.extern.slf4j.Slf4j;
12-
import org.apache.commons.lang3.StringUtils;
139
import org.springframework.beans.factory.annotation.Autowired;
1410
import org.springframework.stereotype.Controller;
1511
import org.springframework.web.bind.annotation.GetMapping;
1612
import org.springframework.web.bind.annotation.PostMapping;
1713
import org.springframework.web.bind.annotation.RequestBody;
1814
import org.springframework.web.bind.annotation.ResponseBody;
1915

20-
import java.io.IOException;
2116
import java.util.HashMap;
2217
import java.util.Map;
2318

@@ -39,49 +34,39 @@ public String index() {
3934

4035
@PostMapping("/genCode")
4136
@ResponseBody
42-
public ReturnT<Map<String, String>> codeGenerate(@RequestBody ParamInfo paramInfo ) {
37+
public ReturnT<Map<String, String>> codeGenerate(@RequestBody ParamInfo paramInfo ) throws Exception {
4338

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());
39+
if (paramInfo.getTableSql().trim().length()<1) {
40+
return new ReturnT<>(ReturnT.FAIL_CODE, "表结构信息不可为空");
41+
}
6942

70-
//log the params
71-
//log.info(JSON.toJSONString(paramInfo));
43+
//1.Parse Table Structure 表结构解析
44+
ClassInfo classInfo = null;
45+
switch (paramInfo.getDataType()){
46+
//JSON模式:parse field from json string
47+
case "json":classInfo = TableParseUtil.processJsonToClassInfo(paramInfo);break;
48+
//正则表达式模式(非完善版本):parse sql by regex
49+
case "sql-regex":classInfo = TableParseUtil.processTableToClassInfoByRegex(paramInfo);break;
50+
//默认模式:default parse sql by java
51+
default : classInfo = TableParseUtil.processTableIntoClassInfo(paramInfo);break;
52+
}
7253

73-
log.info("generator table:"+(classInfo==null?"":classInfo.getTableName())
74-
+",field size:"+((classInfo==null||classInfo.getFieldList()==null)?"":classInfo.getFieldList().size()));
54+
//2.Set the params 设置表格参数
55+
Map<String, Object> params = new HashMap<String, Object>(8);
56+
params.put("classInfo", classInfo);
57+
params.put("tableName", classInfo==null?System.currentTimeMillis():classInfo.getTableName());
58+
params.put("authorName", paramInfo.getAuthorName());
59+
params.put("packageName", paramInfo.getPackageName());
60+
params.put("returnUtil", paramInfo.getReturnUtil());
61+
params.put("swagger", paramInfo.isSwagger());
7562

76-
// generate the code 需要加新的模板请在里面改
77-
Map<String, String> result = generatorService.getResultByParams(params);
63+
//log the generated table and filed size记录解析了什么表,有多少个字段
64+
log.info("generated table:" + classInfo.getTableName() + ",field size:" + (classInfo.getFieldList() == null ? "" : classInfo.getFieldList().size()));
7865

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-
}
66+
//3.generate the code by freemarker template and param . Freemarker根据参数和模板生成代码
67+
Map<String, String> result = generatorService.getResultByParams(params);
8468

69+
return new ReturnT<>(result);
8570
}
8671

8772
}
Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
package com.softdev.system.generator.entity;
2-
3-
import lombok.Data;
4-
5-
/**
6-
* Post data - ParamInfo
7-
* @author zhengkai.blog.csdn.net
8-
*/
9-
@Data
10-
public class ParamInfo {
11-
private String tableSql;
12-
private String authorName;
13-
private String packageName;
14-
private String returnUtil;
15-
private String nameCaseType;
16-
private String tinyintTransType;
17-
private String dataType;
18-
private boolean swagger;
19-
20-
@Data
21-
public static class NAME_CASE_TYPE{
22-
public static String CAMEL_CASE="CamelCase";
23-
public static String UNDER_SCORE_CASE="UnderScoreCase";
24-
public static String UPPER_UNDER_SCORE_CASE="UpperUnderScoreCase";
25-
}
26-
}
1+
package com.softdev.system.generator.entity;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* Post data - ParamInfo
7+
* @author zhengkai.blog.csdn.net
8+
*/
9+
@Data
10+
public class ParamInfo {
11+
private String tableSql;
12+
private String authorName;
13+
private String packageName;
14+
private String returnUtil;
15+
private String nameCaseType;
16+
private String tinyintTransType;
17+
private String dataType;
18+
private boolean swagger;
19+
20+
@Data
21+
public static class NAME_CASE_TYPE{
22+
public static String CAMEL_CASE="CamelCase";
23+
public static String UNDER_SCORE_CASE="UnderScoreCase";
24+
public static String UPPER_UNDER_SCORE_CASE="UpperUnderScoreCase";
25+
}
26+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.softdev.system.generator.entity;
2+
3+
import lombok.Data;
4+
5+
import java.io.Serializable;
6+
7+
@Data
8+
public class TemplateConfig implements Serializable {
9+
10+
public static final long serialVersionUID = 66L;
11+
12+
Integer id;
13+
String name;
14+
String group;
15+
String path;
16+
String description;
17+
18+
}
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
package com.softdev.system.generator.service;
2-
3-
import freemarker.template.TemplateException;
4-
import org.springframework.stereotype.Service;
5-
6-
import java.io.IOException;
7-
import java.util.Map;
8-
9-
/**
10-
* GeneratorService
11-
* @author zhengkai.blog.csdn.net
12-
*/
13-
public interface GeneratorService {
14-
15-
public Map<String,String> getResultByParams(Map<String, Object> params) throws IOException, TemplateException;
16-
17-
}
1+
package com.softdev.system.generator.service;
2+
3+
import freemarker.template.TemplateException;
4+
import org.springframework.stereotype.Service;
5+
6+
import java.io.IOException;
7+
import java.util.Map;
8+
9+
/**
10+
* GeneratorService
11+
* @author zhengkai.blog.csdn.net
12+
*/
13+
public interface GeneratorService {
14+
15+
public Map<String,String> getResultByParams(Map<String, Object> params) throws IOException, TemplateException;
16+
17+
}

0 commit comments

Comments
 (0)