目录
1、简介
Apache FreeMarker 是一个模板引擎:一个基于模板和变化数据生成文本输出(HTML网页、电子邮件、配置文件、源代码等)的Java库。模板是用FreeMarker模板语言(FTL)编写的,它是一种简单的、专门的语言(不是像PHP那样的全面的编程语言)
2、实例
1)创建一个maven项目,我的相关类最终目录,就3个文件
2)创建模板文件hello.ftl,ftl 语法可简单网上了解下
package ${classPath};
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author yourFather
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ${className} {
<#list properties as property>
/**
* ${property.comment}
*/
private ${property.type} ${property.name};
</#list>
public static void main(String[] args) {
System.out.println("${helloWorld}");
}
}
3)创建生成功能类 GenerateFile,运行main方法后,即会发现生成了UserDemo类
pom文件引入的依赖:
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.30</version> </dependency>
package com.future.docker.freemarker;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.*;
import freemarker.template.Configuration;
import freemarker.template.Template;
/**
* @author yourFather
*/
public class GenerateFile {
private static final String TEMPLATE_PATH = "src/main/java/com/future/docker/freemarker/template";
private static final String CLASS_PATH = "src/main/java/com/future/docker/freemarker/output";
public static void main(String[] args) {
// step1 创建freeMarker配置实例,参数:版本实例(FreeMarker 2.3.23之前不带参数的Configuration方法被弃用)
Configuration configuration = new Configuration(Configuration.VERSION_2_3_30);
Writer out = null;
try {
// step2 获取模版路径
configuration.setDirectoryForTemplateLoading(new File(TEMPLATE_PATH));
// step3 创建数据模型
Map<String, Object> dataMap = fillData("UserDemo","com.future.docker.freemarker.output");
// step4 加载模版文件
Template template = configuration.getTemplate("hello.ftl");
// step5 生成数据
File docFile = new File(CLASS_PATH + "\\" + "UserDemo.java");
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
// step6 输出文件
template.process(dataMap, out);
System.out.println("UserDemo.java 文件创建成功 !");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != out) {
out.flush();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
public static Map<String, Object> fillData(String className,String classPath){
String[] properties = {"name","sex","age"};
String[] types = {"String","String","Integer"};
String[] comments = {"姓名","性别","年龄"};
List<Map> listMap = new ArrayList<>();
for(int i =0; i<properties.length; i++){
Map<String, Object> map = new HashMap<>();
map.put("name",properties[i]);
map.put("type",types[i]);
map.put("comment",comments[i]);
listMap.add(map);
}
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("properties", listMap);
dataMap.put("classPath", classPath);
dataMap.put("className", className);
dataMap.put("helloWorld", "生成实体类");
return dataMap;
}
}
4)生成的UserDemo如下
package com.future.docker.freemarker.output;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author yourFather
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserDemo {
/**
* 姓名
*/
private String name;
/**
* 性别
*/
private String sex;
/**
* 年龄
*/
private Integer age;
public static void main(String[] args) {
System.out.println("生成实体类");
}
}