# 学习视频
# 引入依赖
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
1
2
3
4
5
2
3
4
5
# 定义模板
<#--package ${classPath};-->
public class ${className} {
private Integer ${Id};
private String ${userName};
private String ${password};
public Integer get${Id}(){
return ${Id};
}
public void set${Id}(Integer ${Id}){
this.${Id}=${Id};
}
public String get${userName}(){
return ${userName};
}
public void set${userName}(String ${userName}){
this.${userName}=${userName};
}
public String get${password}(){
return ${password};
}
public void set${password}(String ${password}){
this.${password}=${password};
}
}
<#list list as l>
<#if l=="1">
${l}
</#if>
</#list>
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
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
# 实现代码
import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
/**
* @author liuhuan
* @title: ftpDemo
* @description: TODO
* @projectName BHGService
* @date 2021/6/222:42 下午
* @return V1.0.0
*/
public class ftlDemo {
private static final String TEMPLATE_PATH = "/Users/liuhuan/个人文档/JavaSourceCode/高超项目/BHGService/src/test/java";
private static final String CLASS_PATH = "/Users/liuhuan/个人文档/JavaSourceCode/高超项目/BHGService/src/test/java";
@Test
public void demo(){
// step1 创建freeMarker配置实例
Configuration configuration = new Configuration();
Writer out = null;
try {
// step2 获取模版路径
configuration.setDirectoryForTemplateLoading(new File (TEMPLATE_PATH));
// step3 创建数据模型
Map<String, Object> dataMap = new HashMap<String, Object> ();
dataMap.put("classPath", "com.chen.www.chendemo");
dataMap.put("className", "User");
dataMap.put("Id", "Id");
dataMap.put("userName", "userName");
dataMap.put("password","password");
List<String> list = new ArrayList<>();
list.add ("1");
list.add ("2");
dataMap.put ("list", list);
// step4 加载模版文件
Template template = configuration.getTemplate("demo.ftl");
// step5 生成数据
File docFile = new File(CLASS_PATH+"/User.java");
out = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (docFile)));
// step6 输出文件
template.process(dataMap, out);
System.out.println("文件创建成功 !");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != out) {
out.flush();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
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
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
# list循环
List<String> list = new ArrayList<>();
list.add ("1");
list.add ("2");
dataMap.put ("list", list);
1
2
3
4
2
3
4
<#list list as l>
<#if l=="1">
${l}
</#if>
</#list>
1
2
3
4
5
2
3
4
5