1、官网下载jar包 maven中配置的jar包
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.27-incubating</version>
</dependency>
2、编写freemarker工具类
FreeMarkerInit.java
package com.freemarker.utils;
import java.io.File;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
public class FreeMarkerInit {
private static FreeMarkerInit single= new FreeMarkerInit();;
private FreeMarkerInit() {}
//静态工厂方法
public static FreeMarkerInit getInstance() {
return single;
}
public Template getDefinedTemplate(String templateName) throws Exception{
Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
cfg.setDirectoryForTemplateLoading(new File("D:/freemarker/freemarker/src/main/resources/template"));
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
return cfg.getTemplate(templateName);
}
}
2、编写一个实体 Attribute.java
package com.freemarker.utils;
public class Attribute {
private String type;
private String name;
public Attribute(String type, String name) {
this.type = type;
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3、编写javabean模板javabean.ftl
package ${packageName};
public class ${className}{
<#-- 循环类型及属性 -->
<#list attrs as attr>
private ${attr.type} ${attr.name}
</#list>
<#-- 循环生成set get方法 -->
<#list attrs as attr>
public void set${attr.name}(${attr.type} ${attr.name}) {
this.${attr.name} = ${attr.name};
}
public ${attr.type} get${attr.name}() {
return ${attr.name};
}
</#list>
}
4、编写测试
package com.freemarker.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.freemarker.node.reportNode;
import com.freemarker.node.test;
import com.freemarker.utils.Attribute;
import com.freemarker.utils.FreeMarkerInit;
import freemarker.template.Template;
public class TemplateTest {
//生成bean
public void gen1() throws Exception{
//获取模板
Template temp = FreeMarkerInit.getInstance().getDefinedTemplate("javabean.ftl");
//设置根值
Map<String, Object> root = new HashMap<String, Object>();
root.put("packageName", "com.freemarker.bean");
root.put("className", "Person");
List<Attribute> attr_list = new ArrayList<Attribute>();
attr_list.add(new Attribute("Long", "id"));
attr_list.add(new Attribute("String", "name"));
attr_list.add(new Attribute("int", "age"));
root.put("attrs", attr_list);
OutputStream fos = new FileOutputStream( new File("D:/bean", "Person.java"));
Writer out = new OutputStreamWriter(fos);
temp.process(root, out);
fos.flush();
fos.close();
}
public static void main(String[] args) {
TemplateTest test = new TemplateTest();
try {
test.gen1();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
4、生成代码截图