1. 前言
前幾天寫(xiě)了篇關(guān)于Mybatis Plus代碼生成器的文章,不少同學(xué)私下問(wèn)我這個(gè)代碼生成器是如何運(yùn)作的,為什么要用到一些模板引擎,所以今天來(lái)說(shuō)明下代碼生成器的流程。
2. 代碼生成器的使用場(chǎng)景
我們?cè)诰幋a中存在很多樣板代碼,格式較為固定,結(jié)構(gòu)隨著項(xiàng)目的迭代也比較穩(wěn)定,而且數(shù)量巨大,這種代碼寫(xiě)多了也沒(méi)有什么技術(shù)含量,在這種情況下代碼生成器可以有效提高我們的效率,其它情況并不適于使用代碼生成器。
3. 代碼生成器的制作流程
首先我們要制作模板,把樣板代碼的固定格式抽出來(lái)。然后把動(dòng)態(tài)屬性綁定到模板中,就像做填空題一樣。所以在這個(gè)流程中模板引擎是最合適的。我們通過(guò)使用模板引擎的語(yǔ)法將數(shù)據(jù)動(dòng)態(tài)地解析到靜態(tài)模板中去,然后導(dǎo)出為編程中對(duì)應(yīng)的文件就行了。
另外模板引擎有著豐富的綁定數(shù)據(jù)的指令集,可以讓我們根據(jù)條件動(dòng)態(tài)的綁定數(shù)據(jù)到模板中去。以Freemarker為例:
三元表達(dá)式:
1
|
${ true ? 'checked' : '' } |
還有我們等下要用的遍歷列表:
1
2
3
|
<#list fields as field> private ${field.fieldType} ${field.fieldName}; </#list> |
在Java開(kāi)發(fā)中我們常用的模板引擎有Freemarker、Velocity、Thymeleaf ,隨著Web開(kāi)發(fā)中前后端分離的流行模板引擎的使用場(chǎng)景正在被壓縮,但是它依然是一門(mén)有用的技術(shù)。
4. 代碼生成器演示
接下來(lái),我們以Freemarker為例寫(xiě)一個(gè)簡(jiǎn)單的代碼生成器,來(lái)生成POJO類。需要引入Freemarker的依賴。
1
2
3
4
5
|
< dependency > < groupId >org.freemarker</ groupId > < artifactId >freemarker</ artifactId > < version >2.3.28</ version > </ dependency > |
4.1 模板制作
POJO的結(jié)構(gòu)可以分為以下幾部分:
java.lang 包無(wú)需導(dǎo)入。
所以將這些規(guī)則封裝到配置類中:
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
|
public class JavaProperties { // 包名 private final String pkg; // 類名 private final String entityName; // 屬性集合 需要改寫(xiě) equals hash 保證名字可不重復(fù) 類型可重復(fù) private final Set<Field> fields = new LinkedHashSet<>(); // 導(dǎo)入類的不重復(fù)集合 private final Set<String> imports = new LinkedHashSet<>(); public JavaProperties(String entityName, String pkg) { this .entityName = entityName; this .pkg = pkg; } public void addField(Class<?> type, String fieldName) { // 處理 java.lang final String pattern = "java.lang" ; String fieldType = type.getName(); if (!fieldType.startsWith(pattern)) { // 處理導(dǎo)包 imports.add(fieldType); } Field field = new Field(); // 處理成員屬性的格式 int i = fieldType.lastIndexOf( "." ); field.setFieldType(fieldType.substring(i + 1 )); field.setFieldName(fieldName); fields.add(field); } public String getPkg() { return pkg; } public String getEntityName() { return entityName; } public Set<Field> getFields() { return fields; } public Set<String> getImports() { return imports; } /** * 成員屬性封裝對(duì)象. */ public static class Field { // 成員屬性類型 private String fieldType; // 成員屬性名稱 private String fieldName; public String getFieldType() { return fieldType; } public void setFieldType(String fieldType) { this .fieldType = fieldType; } public String getFieldName() { return fieldName; } public void setFieldName(String fieldName) { this .fieldName = fieldName; } /** * 一個(gè)類的成員屬性 一個(gè)名稱只能出現(xiàn)一次 * 我們可以通過(guò)覆寫(xiě)equals hash 方法 然后放入Set * * @param o 另一個(gè)成員屬性 * @return 比較結(jié)果 */ @Override public boolean equals(Object o) { if ( this == o) return true ; if (o == null || getClass() != o.getClass()) return false ; Field field = (Field) o; return Objects.equals(fieldName, field.fieldName); } @Override public int hashCode() { return Objects.hash(fieldType, fieldName); } } } |
接著就是靜態(tài)模板e(cuò)ntity.ftl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package ${pkg}; <#list imports as impt> import ${impt}; </#list> /** * the ${entityName} type * @author felord.cn */ public class ${entityName} { <#list fields as field> private ${field.fieldType} ${field.fieldName}; </#list> } |
這里用到了Freemarker綁定數(shù)據(jù)的語(yǔ)法,比如List迭代渲染。
4.2 生成器編寫(xiě)
Freemarker通過(guò)聲明配置并獲取模板對(duì)象freemarker.template,該對(duì)象的process方法可以將動(dòng)態(tài)數(shù)據(jù)綁定到模板中并導(dǎo)出為文件,最終實(shí)現(xiàn)了代碼生成器,核心代碼如下:
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
|
/** * 簡(jiǎn)單的代碼生成器. * * @param rootPath maven 的 java 目錄 * @param templatePath 模板存放的文件夾 * @param templateName 模板的名稱 * @param javaProperties 需要渲染對(duì)象的封裝 * @throws IOException the io exception * @throws TemplateException the template exception */ public static void autoCodingJavaEntity(String rootPath, String templatePath, String templateName, JavaProperties javaProperties) throws IOException, TemplateException { // freemarker 配置 Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS); configuration.setDefaultEncoding( "UTF-8" ); // 指定模板的路徑 configuration.setDirectoryForTemplateLoading( new File(templatePath)); // 根據(jù)模板名稱獲取路徑下的模板 Template template = configuration.getTemplate(templateName); // 處理路徑問(wèn)題 final String ext = ".java" ; String javaName = javaProperties.getEntityName().concat(ext); String packageName = javaProperties.getPkg(); String out = rootPath.concat(Stream.of(packageName.split( "\\." )) .collect(Collectors.joining( "/" , "/" , "/" + javaName))); // 定義一個(gè)輸出流來(lái)導(dǎo)出代碼文件 OutputStreamWriter outputStreamWriter = new OutputStreamWriter( new FileOutputStream(out)); // freemarker 引擎將動(dòng)態(tài)數(shù)據(jù)綁定的模板并導(dǎo)出為文件 template.process(javaProperties, outputStreamWriter); } |
通過(guò)執(zhí)行以下代碼即可生成一個(gè)UserEntity的POJO:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
// 路徑根據(jù)自己項(xiàng)目的特點(diǎn)調(diào)整 String rootPath = "C:\\Users\\felord\\IdeaProjects\\codegenerator\\src\\main\\java" ; String packageName = "cn.felord.code" ; String templatePath = "C:\\Users\\felord\\IdeaProjects\\codegenerator\\src\\main\\resources\\templates" ; String templateName = "entity.ftl" ; JavaProperties userEntity = new JavaProperties( "UserEntity" , packageName); userEntity.addField(String. class , "username" ); userEntity.addField(LocalDate. class , "birthday" ); userEntity.addField(LocalDateTime. class , "addTime" ); userEntity.addField(Integer. class , "gender" ); userEntity.addField(Integer. class , "age" ); autoCodingJavaEntity(rootPath, templatePath, templateName, userEntity); |
生成的效果是不是跟手寫(xiě)的差不多:
5. 總結(jié)
這就是大部分代碼生成器的機(jī)制,希望可以解答一些網(wǎng)友的疑問(wèn)。多多關(guān)注:碼農(nóng)小胖哥 獲取更多干貨,相關(guān)的DEMO可通過(guò)公眾號(hào)回復(fù)codegen獲取。如果你有疑問(wèn)可以通過(guò)微信MSW_623進(jìn)行溝通。
到此這篇關(guān)于Java代碼生成器的制作流程詳解的文章就介紹到這了,更多相關(guān)Java代碼生成器 制作內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://www.cnblogs.com/felordcn/p/13304856.html