Skip to content

Commit c0a5b63

Browse files
committed
202109022011 增加《FreeMarker 快速入门》
1 parent 4af9ec2 commit c0a5b63

File tree

7 files changed

+270
-15
lines changed

7 files changed

+270
-15
lines changed

README.md

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
# Java 架构师的工具箱
22

33

4-
5-
64
## 项目1:junit5-practice
75

86
### 参考文档
97

108
- [Idea-Maven多模块开发](https://www.jianshu.com/p/274455dc9469)
9+
1110
- [idea报错:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile](https://blog.csdn.net/gao_jun1/article/details/109997200)
11+
1212
- [junit5-examples](https://github.com/mkyong/junit5-examples)
13-
- [该升级你的JUnit版本了——JUnit5基本介绍](https://zhuanlan.zhihu.com/p/111706639)
14-
- [Java单元测试之JUnit 5快速上手【实践代码】](https://www.cnblogs.com/one12138/p/11536492.html)
15-
- [junit 5中三种不同指定用例测试顺序](https://blog.csdn.net/jackyrongvip/article/details/89389387)
1613

14+
- [该升级你的JUnit版本了——JUnit5基本介绍](https://zhuanlan.zhihu.com/p/111706639)
1715

16+
- [Java单元测试之JUnit 5快速上手【实践代码】](https://www.cnblogs.com/one12138/p/11536492.html)
1817

18+
- [junit 5中三种不同指定用例测试顺序](https://blog.csdn.net/jackyrongvip/article/details/89389387)
1919

20+
2021

2122
## 项目2:javamail-practice
2223

2324
### 参考文档
2425

2526
- [JavaMail 实现邮件推送功能(网易邮箱)](https://blog.csdn.net/weixin_43967679/article/details/107879747)
2627

28+
- [Java中File使用--创建文件](https://blog.csdn.net/m0_37989184/article/details/93025734)
29+
30+
- [FreeMarker 快速入门](https://www.cnblogs.com/itdragon/p/7750903.html)
31+
2732

2833

2934
## 项目3:poi-practice
@@ -36,23 +41,13 @@
3641

3742

3843

39-
40-
41-
42-
4344
## TODO
4445

45-
- SendMail
46-
- 解析Excel
4746
- Swagger
4847
- sql过滤模糊查询条件中的特殊字符
4948

5049

5150

52-
53-
54-
55-
5651
# 其他参考文档
5752

5853
- [IDEA如何设置author头注解](https://blog.csdn.net/weixin_42555514/article/details/106826894)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.coderdream;
2+
3+
public class AutoCodeDemo {
4+
5+
public static void main(String[] args) {
6+
System.out.println("通过简单的 <代码自动生产程序> 演示 FreeMarker的HelloWorld!");
7+
}
8+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.coderdream;
2+
3+
/**
4+
* @author :CoderDream
5+
* @date :Created in 2021/9/2 19:38
6+
* @description:
7+
* @modified By:CoderDream
8+
* @version: $
9+
*/
10+
import java.io.BufferedWriter;
11+
import java.io.File;
12+
import java.io.FileOutputStream;
13+
import java.io.OutputStreamWriter;
14+
import java.io.Writer;
15+
import java.util.*;
16+
17+
import freemarker.template.Configuration;
18+
import freemarker.template.Template;
19+
20+
/**
21+
* 最常见的问题:
22+
* java.io.FileNotFoundException: xxx does not exist. 解决方法:要有耐心
23+
* FreeMarker jar 最新的版本(2.3.23)提示 Configuration 方法被弃用
24+
* 代码自动生产基本原理:
25+
* 数据填充 freeMarker 占位符
26+
*/
27+
public class FreemarkerDemo {
28+
29+
private static final String TEMPLATE_PATH = "/template";
30+
//private static final String CLASS_PATH = "src/main/java/com/coderdream/";
31+
private static final String CLASS_PATH = "javamail-practice/src/main/java/com/coderdream/";
32+
33+
public static void main(String[] args) {
34+
method2();
35+
}
36+
37+
private static void method1() {
38+
// step1 创建freeMarker配置实例
39+
Configuration configuration = new Configuration();
40+
Writer out = null;
41+
try {
42+
// step2 获取模版路径
43+
// configuration.setDirectoryForTemplateLoading(FreemarkerDemo.class, TEMPLATE_PATH);
44+
configuration.setClassForTemplateLoading(TemplateFactory.class, TEMPLATE_PATH);
45+
// step3 创建数据模型
46+
Map<String, Object> dataMap = new HashMap<String, Object>();
47+
dataMap.put("classPath", "com.coderdream");
48+
dataMap.put("className", "AutoCodeDemo");
49+
dataMap.put("helloWorld", "通过简单的 <代码自动生产程序> 演示 FreeMarker的HelloWorld!");
50+
// step4 加载模版文件
51+
Template template = configuration.getTemplate("hello.ftl");
52+
// step5 生成数据
53+
File docFile = new File(CLASS_PATH + "AutoCodeDemo.java");
54+
if(!docFile.exists()) {
55+
//docFile.mkdirs();
56+
docFile.createNewFile();
57+
}
58+
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
59+
// step6 输出文件
60+
template.process(dataMap, out);
61+
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^AutoCodeDemo.java 文件创建成功 !");
62+
} catch (Exception e) {
63+
e.printStackTrace();
64+
} finally {
65+
try {
66+
if (null != out) {
67+
out.flush();
68+
}
69+
} catch (Exception e2) {
70+
e2.printStackTrace();
71+
}
72+
}
73+
}
74+
75+
private static void method2() {
76+
// step1 创建freeMarker配置实例
77+
Configuration configuration = new Configuration();
78+
Writer out = null;
79+
try {
80+
// step2 获取模版路径
81+
// configuration.setDirectoryForTemplateLoading(FreemarkerDemo.class, TEMPLATE_PATH);
82+
configuration.setClassForTemplateLoading(TemplateFactory.class, TEMPLATE_PATH);
83+
// step3 创建数据模型
84+
Map<String, Object> dataMap = new HashMap<String, Object>();
85+
dataMap.put("name", "itdragon博客");
86+
dataMap.put("dateTime", new Date());
87+
88+
List<User> users = new ArrayList<User>();
89+
users.add(new User(1, "ITDragon 博客"));
90+
users.add(new User(2, "欢迎"));
91+
users.add(new User(3, "You!"));
92+
dataMap.put("users", users);
93+
// step4 加载模版文件
94+
Template template = configuration.getTemplate("stringFreeMarker.ftl");
95+
// step5 生成数据
96+
out = new OutputStreamWriter(System.out);
97+
// step6 输出文件
98+
template.process(dataMap, out);
99+
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^AutoCodeDemo.java 文件创建成功 !");
100+
} catch (Exception e) {
101+
e.printStackTrace();
102+
} finally {
103+
try {
104+
if (null != out) {
105+
out.flush();
106+
}
107+
} catch (Exception e2) {
108+
e2.printStackTrace();
109+
}
110+
}
111+
}
112+
113+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.coderdream;
2+
3+
/**
4+
* @author :CoderDream
5+
* @date :Created in 2021/9/2 20:07
6+
* @description:
7+
* @modified By:CoderDream
8+
* @version: $
9+
*/
10+
public class User {
11+
12+
private Integer id;
13+
private String name;
14+
15+
public User() {
16+
}
17+
18+
public User(Integer id, String name) {
19+
this.id = id;
20+
this.name = name;
21+
}
22+
23+
public Integer getId() {
24+
return id;
25+
}
26+
27+
public void setId(Integer id) {
28+
this.id = id;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return "User [id=" + id + ", name=" + name + "]";
42+
}
43+
44+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package ${classPath};
2+
3+
public class ${className} {
4+
5+
public static void main(String[] args) {
6+
System.out.println("${helloWorld}");
7+
}
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
其他FreeMarker文件
2+
<#macro addMethod a b >
3+
result : ${a + b}
4+
</#macro>
5+
<#assign otherName="另外一个FreeMarker的变量">
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
字符串输出:
2+
${"Hello ${name} !"} / ${"Hello " + name + " !"}
3+
<#assign cname=r"特殊字符完成输出(http://www.baidu.com)">
4+
${cname}
5+
6+
字符串截取 :
7+
通过下标直接获取下标对应的字母: ${name[2]}
8+
起点下标..结尾下标截取字符串:${name[0..5]}
9+
10+
算数运算:
11+
<#-- 支持"+"、"-"、"*"、"/"、"%"运算符 -->
12+
<#assign number1 = 10>
13+
<#assign number2 = 5>
14+
"+" : ${number1 + number2}
15+
"-" : ${number1 - number2}
16+
"*" : ${number1 * number2}
17+
"/" : ${number1 / number2}
18+
"%" : ${number1 % number2}
19+
20+
比较运算符:
21+
<#if number1 + number2 gte 12 || number1 - number2 lt 6>
22+
"*" : ${number1 * number2}
23+
<#else>
24+
"/" : ${number1 / number2}
25+
</#if>
26+
27+
内建函数:
28+
<#assign data = "abcd1234">
29+
第一个字母大写:${data?cap_first}
30+
所有字母小写:${data?lower_case}
31+
所有字母大写:${data?upper_case}
32+
<#assign floatData = 12.34>
33+
数值取整数:${floatData?int}
34+
获取集合的长度:${users?size}
35+
时间格式化:${dateTime?string("yyyy-MM-dd")}
36+
37+
空判断和对象集合:
38+
<#if users??>
39+
<#list users as user >
40+
${user.id} - ${user.name}
41+
</#list>
42+
<#else>
43+
${user!"变量为空则给一个默认值"}
44+
</#if>
45+
46+
Map集合:
47+
<#assign mapData={"name":"程序员", "salary":15000}>
48+
直接通过Key获取 Value值:${mapData["name"]}
49+
通过Key遍历Map:
50+
<#list mapData?keys as key>
51+
Key: ${key} - Value: ${mapData[key]}
52+
</#list>
53+
通过Value遍历Map:
54+
<#list mapData?values as value>
55+
Value: ${value}
56+
</#list>
57+
58+
List集合:
59+
<#assign listData=["ITDragon", "blog", "is", "cool"]>
60+
<#list listData as value>${value} </#list>
61+
62+
include指令:
63+
引入其他文件:<#include "otherFreeMarker.ftl" />
64+
65+
macro宏指令:
66+
<#macro mo>
67+
定义无参数的宏macro--${name}
68+
</#macro>
69+
使用宏macro: <@mo />
70+
<#macro moArgs a b c>
71+
定义带参数的宏macro-- ${a+b+c}
72+
</#macro>
73+
使用带参数的宏macro: <@moArgs a=1 b=2 c=3 />
74+
75+
命名空间:
76+
<#import "otherFreeMarker.ftl" as otherFtl>
77+
${otherFtl.otherName}
78+
<@otherFtl.addMethod a=10 b=20 />
79+
<#assign otherName="修改otherFreeMarker.ftl中的otherName变量值"/>
80+
${otherFtl.otherName}
81+
<#assign otherName="修改otherFreeMarker.ftl中的otherName变量值" in otherFtl />
82+
${otherFtl.otherName}

0 commit comments

Comments
 (0)