-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtmp_CodeGenerator.bak
More file actions
124 lines (104 loc) · 10.9 KB
/
Copy pathtmp_CodeGenerator.bak
File metadata and controls
124 lines (104 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.axin.OJ.generate;
import cn.hutool.core.io.FileUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.io.FileWriter;
import java.io.Writer;
/**
* 浠g爜鐢熸垚鍣? *
* @author NanoDa Team
* @from <a href="https://www.code-nav.cn">缂栫▼瀵艰埅瀛︿範鍦?/a>
*/
public class CodeGenerator {
/**
* 鐢ㄦ硶锛氫慨鏀圭敓鎴愬弬鏁板拰鐢熸垚璺緞锛屾敞閲婃帀涓嶉渶瑕佺殑鐢熸垚閫昏緫锛岀劧鍚庤繍琛屽嵆鍙? *
* @param args
* @throws TemplateException
* @throws IOException
*/
public static void main(String[] args) throws TemplateException, IOException {
// 鎸囧畾鐢熸垚鍙傛暟
String packageName = "com.axin.OJ";
String dataName = "鐢ㄦ埛璇勮";
String dataKey = "userComment";
String upperDataKey = "UserComment";
// 灏佽鐢熸垚鍙傛暟
Map<String, Object> dataModel = new HashMap<>();
dataModel.put("packageName", packageName);
dataModel.put("dataName", dataName);
dataModel.put("dataKey", dataKey);
dataModel.put("upperDataKey", upperDataKey);
// 鐢熸垚璺緞榛樿鍊? String projectPath = System.getProperty("user.dir");
// 鍙傝€冭矾寰勶紝鍙互鑷繁璋冩暣涓嬮潰鐨?outputPath
String inputPath = projectPath + File.separator + "src/main/resources/templates/妯℃澘鍚嶇О.java.ftl";
String outputPath = String.format("%s/generator/鍖呭悕/%s绫诲悗缂€.java", projectPath, upperDataKey);
// 1銆佺敓鎴?Controller
// 鎸囧畾鐢熸垚璺緞
inputPath = projectPath + File.separator + "src/main/resources/templates/TemplateController.java.ftl";
outputPath = String.format("%s/generator/controller/%sController.java", projectPath, upperDataKey);
// 鐢熸垚
doGenerate(inputPath, outputPath, dataModel);
System.out.println("鐢熸垚 Controller 鎴愬姛锛屾枃浠惰矾寰勶細" + outputPath);
// 2銆佺敓鎴?Service 鎺ュ彛鍜屽疄鐜扮被
// 鐢熸垚 Service 鎺ュ彛
inputPath = projectPath + File.separator + "src/main/resources/templates/TemplateService.java.ftl";
outputPath = String.format("%s/generator/service/%sService.java", projectPath, upperDataKey);
doGenerate(inputPath, outputPath, dataModel);
System.out.println("鐢熸垚 Service 鎺ュ彛鎴愬姛锛屾枃浠惰矾寰勶細" + outputPath);
// 鐢熸垚 Service 瀹炵幇绫? inputPath = projectPath + File.separator + "src/main/resources/templates/TemplateServiceImpl.java.ftl";
outputPath = String.format("%s/generator/service/impl/%sServiceImpl.java", projectPath, upperDataKey);
doGenerate(inputPath, outputPath, dataModel);
System.out.println("鐢熸垚 Service 瀹炵幇绫绘垚鍔燂紝鏂囦欢璺緞锛? + outputPath);
// 3銆佺敓鎴愭暟鎹ā鍨嬪皝瑁呯被锛堝寘鎷?DTO 鍜?VO锛? // 鐢熸垚 DTO
inputPath = projectPath + File.separator + "src/main/resources/templates/model/TemplateAddRequest.java.ftl";
outputPath = String.format("%s/generator/model/dto/%sAddRequest.java", projectPath, upperDataKey);
doGenerate(inputPath, outputPath, dataModel);
inputPath = projectPath + File.separator + "src/main/resources/templates/model/TemplateQueryRequest.java.ftl";
outputPath = String.format("%s/generator/model/dto/%sQueryRequest.java", projectPath, upperDataKey);
doGenerate(inputPath, outputPath, dataModel);
inputPath = projectPath + File.separator + "src/main/resources/templates/model/TemplateEditRequest.java.ftl";
outputPath = String.format("%s/generator/model/dto/%sEditRequest.java", projectPath, upperDataKey);
doGenerate(inputPath, outputPath, dataModel);
inputPath = projectPath + File.separator + "src/main/resources/templates/model/TemplateUpdateRequest.java.ftl";
outputPath = String.format("%s/generator/model/dto/%sUpdateRequest.java", projectPath, upperDataKey);
doGenerate(inputPath, outputPath, dataModel);
System.out.println("鐢熸垚 DTO 鎴愬姛锛屾枃浠惰矾寰勶細" + outputPath);
// 鐢熸垚 VO
inputPath = projectPath + File.separator + "src/main/resources/templates/model/TemplateVO.java.ftl";
outputPath = String.format("%s/generator/model/vo/%sVO.java", projectPath, upperDataKey);
doGenerate(inputPath, outputPath, dataModel);
System.out.println("鐢熸垚 VO 鎴愬姛锛屾枃浠惰矾寰勶細" + outputPath);
}
/**
* 鐢熸垚鏂囦欢
*
* @param inputPath 妯℃澘鏂囦欢杈撳叆璺緞
* @param outputPath 杈撳嚭璺緞
* @param model 鏁版嵁妯″瀷
* @throws IOException
* @throws TemplateException
*/
public static void doGenerate(String inputPath, String outputPath, Object model) throws IOException, TemplateException {
// new 鍑?Configuration 瀵硅薄锛屽弬鏁颁负 FreeMarker 鐗堟湰鍙? Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
// 鎸囧畾妯℃澘鏂囦欢鎵€鍦ㄧ殑璺緞
File templateDir = new File(inputPath).getParentFile();
configuration.setDirectoryForTemplateLoading(templateDir);
// 璁剧疆妯℃澘鏂囦欢浣跨敤鐨勫瓧绗﹂泦
configuration.setDefaultEncoding("utf-8");
// 鍒涘缓妯℃澘瀵硅薄锛屽姞杞芥寚瀹氭ā鏉? String templateName = new File(inputPath).getName();
Template template = configuration.getTemplate(templateName);
// 鏂囦欢涓嶅瓨鍦ㄥ垯鍒涘缓鏂囦欢鍜岀埗鐩綍
if (!FileUtil.exist(outputPath)) {
FileUtil.touch(outputPath);
}
// 鐢熸垚
Writer out = new FileWriter(outputPath);
template.process(model, out);
// 鐢熸垚鏂囦欢鍚庡埆蹇樹簡鍏抽棴鍝? out.close();
}
}