一. 什么是Freemarker
百科说明:FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页、电子邮件、配置文件、源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。
FreeMarker是免费的,基于Apache许可证2.0版本发布。其模板编写为FreeMarker Template Language(FTL),属于简单、专用的语言。需要准备数据在真实编程语言中来显示,比如数据库查询和业务运算, 之后模板显示已经准备好的数据。在模板中,主要用于如何展现数据, 而在模板之外注意于要展示什么数据。
二. Freemarker 需下载的 JAR 包
freemarker.jar
http://123yun.newhua.com/down/freemarker-2.3.19.tar.zip
三. Demo 结构说明
bean
AnnotationBean 类文件注释信息
ParameterBean 动态参数, 用于传递freemarker 模板所需数据
utils
DateTimeUtils 日期工具类
EmptyUitls 判断是否为空工具类
StringUtils 字符串工具类
main
FreemarkerDemo freemarker 主文件
四. Demo 类代码
/**
* 类文件注释信息
* @author Administrator
*
*/
public class AnnotationBean {
/**
* 作者
*/
private String author;
/**
* 发布日期
*/
private String date;
public AnnotationBean() {
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
/**
* 动态参数bean
* @author Administrator
*
*/
public class ParameterBean {
/**
* 所属项目名
*/
private String project;
/**
* 模块名称
*/
private String moduleName;
/**
* 包名
*/
private String packageName;
/**
* 实体类名
*/
private String className;
/**
* 实体类中文名
*/
private String classChineseName;
/**
* 首字母小写的类名
*/
private String firstLowCaseClassName;
/**
* 全部转换为小写类名
*/
private String lowCaseClassName;
/**
* 所需继承类
*/
private String extendClassName;
/**
* 通用table名
*/
private String tableName;
public ParameterBean() {
}
public String getModuleName() {
return moduleName;
}
public void setModuleName(String moduleName) {
this.moduleName = moduleName;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getExtendClassName() {
return extendClassName;
}
public void setExtendClassName(String extendClassName) {
this.extendClassName = extendClassName;
}
public String getFirstLowCaseClassName() {
return StringUtils.getWordwithLowCaseFirstWord(className);
}
public String getClassChineseName() {
return classChineseName;
}
public void setClassChineseName(String classChineseName) {
this.classChineseName = classChineseName;
}
public String getProject() {
return project;
}
public void setProject(String project) {
this.project = project;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getLowCaseClassName() {
return StringUtils.getLowCaseStr(className);
}
}
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
@SuppressWarnings({"unchecked","rawtypes"})
public class FreemarkerDemo {
/**
* 类的注解
*/
private static String ANNOTATION = "annotation";
/**
* 变量参数
*/
private static String PARAMETER = "parameter";
/**
* freemarker 模块所在的目录
*/
private static final String DEFAULT_FTL_DIR = "ftls/center/";
/**
* freemarker 模块文件名
*/
private static final String ACTION_FTL_FILE = "demoTemplate.ftl";
/**
* 生成的文件存储位置
*/
private static final String DEFAULT_GENERATE_PATH = "generateFolder";
private static void generateActionClass(AnnotationBean annotation,ParameterBean parameter) throws Exception{
Configuration configuration;
Template template;
Writer writer;
// 1. 预先加载要装载的数据
Map dataMap = new HashMap();
// 加载注解
dataMap.put(ANNOTATION, annotation);
// 加载参数
dataMap.put(PARAMETER, parameter);
// 2. 创建Freemarker配置实例
configuration = new Configuration();
configuration.setDirectoryForTemplateLoading(new File(DEFAULT_FTL_DIR));
// 3. 加载模板文件
template = configuration.getTemplate(ACTION_FTL_FILE);
// 4. 创建文件
File file = new File(DEFAULT_GENERATE_PATH + parameter.getClassName() + "Action.java");
if (!file.exists()) {
file.createNewFile();
}
writer = new FileWriter(file);
template.process(dataMap, writer);
writer.flush();
writer.close();
System.out.println("自动生成" + parameter.getClassName() + "代码成功!");
}
public static void main(String[] args) {
try {
AnnotationBean annotation = new AnnotationBean();
annotation.setAuthor("Billy");
annotation.setDate(DateTimeUtils.getStrFromDate(new Date()));
ParameterBean parameter = new ParameterBean();
parameter.setProject("project");
parameter.setModuleName("userManagement");;
parameter.setExtendClassName("BaseAction");
parameter.setPackageName("userpackage");
parameter.setClassName("User");;
parameter.setClassChineseName("用户管理");
generateActionClass(annotation,parameter);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTimeUtils {
private static final String ZN_YYYYMMDD = "yyyy年MM月dd日";
/**
* 获取格式化日期
* @param date
* @return
*/
public static String getStrFromDate(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat(ZN_YYYYMMDD);
return sdf.format(date);
}
public static void main(String[] args) {
System.out.println(getStrFromDate(new Date()));
}
}
public class EmptyUitls {
public static boolean isNotEmpty(String str) {
if(null != str && str.length() > 0 ) {
return true;
} else {
return false;
}
}
public static boolean isEmpty(String str) {
return !isNotEmpty(str);
}
}
public class StringUtils {
/**
* 转换为首字母小写的值
* @param sourceWord
* @return
*/
public static String getWordwithLowCaseFirstWord(String sourceWord) {
String result = "";
if(EmptyUitls.isNotEmpty(sourceWord)) {
StringBuilder sb = new StringBuilder("");
sb.append(sourceWord.substring(0, 1).toLowerCase());
sb.append(sourceWord.substring(1,sourceWord.length()));
result = sb.toString();
}
return result;
}
/**
* 转换为全部小写字符串
* @param sourceWord
* @return
*/
public static String getLowCaseStr(String sourceWord) {
String result = "";
if(EmptyUitls.isNotEmpty(sourceWord)) {
result = sourceWord.toLowerCase();
}
return result;
}
public static void main(String[] args) {
System.out.println(getWordwithLowCaseFirstWord("ZxDF"));
}
}
五. 小结
拒绝重复性工作,拒绝平庸!