Velocity 模板引擎介绍
在现今的软件开发过程中,软件开发人员将更多的精力投入在了重复的相似劳动中。特别是在如今特别流行的 MVC 架构模式中,软件各个层次的功能更加独立,同时代码的相似度也更加高。所以我们需要寻找一种来减少软件开发人员重复劳动的方法,让程序员将更多的精力放在业务逻辑以及其他更加具有创造力的工作上。Velocity 这个模板引擎就可以在一定程度上解决这个问题。
Velocity 是一个基于 Java 的模板引擎框架,提供的模板语言可以使用在 Java 中定义的对象和变量上。Velocity 是 Apache 基金会的项目,开发的目标是分离 MVC 模式中的持久化层和业务层。但是在实际应用过程中,Velocity 不仅仅被用在了 MVC 的架构中,还可以被用在以下一些场景中。
1.Web 应用:开发者在不使用 JSP 的情况下,可以用 Velocity 让 HTML 具有动态内容的特性。
2. 源代码生成:Velocity 可以被用来生成 Java 代码、SQL 或者 PostScript。有很多开源和商业开发的软件是使用 Velocity 来开发的。
3. 自动 Email:很多软件的用户注册、密码提醒或者报表都是使用 Velocity 来自动生成的。使用 Velocity 可以在文本文件里面生成邮件内容,而不是在 Java 代码中拼接字符串。
4. 转换 xml:Velocity 提供一个叫 Anakia 的 ant 任务,可以读取 XML 文件并让它能够被 Velocity 模板读取。一个比较普遍的应用是将 xdoc 文档
jar下载地址:
http://velocity.apache.org/download.cgi?cm_mc_uid=42093115221914615698815&cm_mc_sid_50200000=1466501065
模板文件:
/*
* 文件名:${model.fileName}
* 版权:Copyright 2007-${year} 517na Tech. Co. Ltd. All Rights Reserved.
* 描述: ${model.fileName}
* 修改人:${author}
* 修改时间:${date}
* 修改内容:新增
*/
package ${model.boPackageName};
import java.io.Serializable;
#foreach($import in ${model.dataImport})
import $import;
#end
/**
* ${model.tableName}.
* @author ${author}
*/
public class ${model.modelBoName} implements Serializable {
/**
* 序列化.
*/
private static final long serialVersionUID = 1L;
/**
* @表名
*/
private String tableName;
/**
* 获取 表名.
* @return 表名
*/
public String getTableName(){
return tableName;
};
/**
* 设置 表名.
* @param tableName 表名
*/
public void setTableName(String tableName){
this.tableName=tableName;
};
#foreach($fieldMap in ${model.rowsData})
/**
* @$fieldMap.get('fieldChName')
*/
private $fieldMap.get('javaType') $fieldMap.get('fieldNameFormat');
#end
#foreach($fieldMap in ${model.rowsData})
/**
* 获取 $fieldMap.get('fieldChName').
* @return 返回$fieldMap.get('fieldChName')
*/
public $fieldMap.get('javaType') get$fieldMap.get('fieldName')() {
return $fieldMap.get('fieldNameFormat');
}
/**
* 设置 $fieldMap.get('fieldChName').
* @param $fieldMap.get('fieldNameFormat') $fieldMap.get('fieldChName')
*/
public void set$fieldMap.get('fieldName')($fieldMap.get('javaType') $fieldMap.get('fieldNameFormat')) {
this.$fieldMap.get('fieldNameFormat') = $fieldMap.get('fieldNameFormat');
}
#end
}
模板工具类
/*
* 文件名:ExcelModel.java
* 版权:Copyright 2007-2015 517na Tech. Co. Ltd. All Rights Reserved.
* 描述: ExcelModel.java
* 修改人:peiyu
* 修改时间:2015年7月16日
* 修改内容:新增
*/
package com.better517na.autocodgen.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* excelModel.
* @author zhengmo
*/
public class ExcelModel implements Serializable {
/**
* @序列化
*/
private static final long serialVersionUID = 1L;
/**
* @表ID
*/
private String tableId;
/**
* @表名
*/
private String tableName;
/**
* @keyid 主键时的key
*/
private String tableKey;
/**
* @表说明
*/
private String tableDesc;
/**
* @带Bo的类名
*/
private String modelBoName;
/**
* @小写带Bo的类名
*/
private String lowerModelBoName;
/**
* @类名
*/
private String modelName;
/**
* @Bo包名
*/
private String boPackageName;
/**
* @全路径类名
*/
private String modelClass;
/**
* @项目名称
*/
private String projectName;
/**
* @项目包名
*/
private String projectPackageName;
/**
* @文件名
*/
private String fileName;
/**
* @项目类型
*/
private Integer projectType;
/**
* @是否批量插入
*/
private Boolean isBatchInsert;
/**
* @服务接口
*/
private String serviceInterfaceClass;
/**
* @业务接口
*/
private String businessInterfaceClass;
/**
* @数据层接口
*/
private String daoInterfaceClass;
/**
* @导入的包
*/
private Set<String> dataImport = new HashSet<String>();
/**
* @行集合
*/
private List<Map<String, Object>> rowsData = new ArrayList<Map<String, Object>>();
/**
* 设置tableId.
* @return 返回tableId
*/
public String getTableId() {
return tableId;
}
/**
* 获取tableId.
* @param tableid 要设置的tableid
* @return ExcelModel
*/
public ExcelModel setTableId(String tableid) {
this.tableId = tableid;
return this;
}
/**
* 设置tableName.
* @return 返回tableName
*/
public String getTableName() {
return tableName;
}
/**
* 获取tableName.
* @param tablename 要设置的tablename
* @return ExcelModel
*/
public ExcelModel setTableName(String tablename) {
this.tableName = tablename;
return this;
}
/**
* 设置tableKey.
* @return 返回tableKey
*/
public String getTableKey() {
return tableKey;
}
/**
* 获取tableKey.
* @param tableKey 要设置的tableKey
*/
public void setTableKey(String tableKey) {
this.tableKey = tableKey;
}
/**
* 设置tableDesc.
* @return 返回tableDesc
*/
public String getTableDesc() {
return tableDesc;
}
/**
* 获取tableDesc.
* @param tabledesc 要设置的tabledesc
* @return ExcelModel
*/
public ExcelModel setTableDesc(String tabledesc) {
this.tableDesc = tabledesc;
return this;
}
/**
* 设置rowsData.
* @return 返回rowsData
*/
public List<Map<String, Object>> getRowsData() {
return rowsData;
}
/**
* 获取rowsData.
* @param rowsdata 要设置的rowsdata
* @return ExcelModel
*/
public ExcelModel setRowsData(List<Map<String, Object>> rowsdata) {
this.rowsData = rowsdata;
return this;
}
/**
* 设置modelBoName.
* @return 返回modelBoName
*/
public String getModelBoName() {
return modelBoName;
}
/**
* 获取modelBoName.
* @param modelBoName 要设置的modelBoName
*/
public void setModelBoName(String modelBoName) {
this.modelBoName = modelBoName;
}
/**
* 设置lowerModelBoName.
* @return 返回lowerModelBoName
*/
public String getLowerModelBoName() {
return lowerModelBoName;
}
/**
* 获取lowerModelBoName.
* @param lowerModelBoName 要设置的lowerModelBoName
*/
public void setLowerModelBoName(String lowerModelBoName) {
this.lowerModelBoName = lowerModelBoName;
}
/**
* 设置modelName.
* @return 返回modelName
*/
public String getModelName() {
return modelName;
}
/**
* 获取modelName.
* @param modelName 要设置的modelName
*/
public void setModelName(String modelName) {
this.modelName = modelName;
}
/**
* 设置boPackageName.
* @return 返回boPackageName
*/
public String getBoPackageName() {
return boPackageName;
}
/**
* 获取boPackageName.
* @param boPackageName 要设置的boPackageName
*/
public void setBoPackageName(String boPackageName) {
this.boPackageName = boPackageName;
}
/**
* 设置modelClass.
* @return 返回modelClass
*/
public String getModelClass() {
return modelClass;
}
/**
* 获取modelClass.
* @param modelClass 要设置的modelClass
*/
public void setModelClass(String modelClass) {
this.modelClass = modelClass;
}
/**
* 设置projectName.
* @return 返回projectName
*/
public String getProjectName() {
return projectName;
}
/**
* 获取projectName.
* @param projectName 要设置的projectName
*/
public void setProjectName(String projectName) {
this.projectName = projectName;
}
/**
* 设置projectPackageName.
* @return 返回projectPackageName
*/
public String getProjectPackageName() {
return projectPackageName;
}
/**
* 获取projectPackageName.
* @param projectPackageName 要设置的projectPackageName
*/
public void setProjectPackageName(String projectPackageName) {
this.projectPackageName = projectPackageName;
}
/**
* 设置fileName.
* @return 返回fileName
*/
public String getFileName() {
return fileName;
}
/**
* 获取fileName.
* @param fileName 要设置的fileName
*/
public void setFileName(String fileName) {
this.fileName = fileName;
}
/**
* 设置dataImport.
* @return 返回dataImport
*/
public Set<String> getDataImport() {
return dataImport;
}
/**
* 获取dataImport.
* @param dataImport 要设置的dataImport
*/
public void setDataImport(Set<String> dataImport) {
this.dataImport = dataImport;
}
/**
* 设置projectType.
* @return 返回projectType
*/
public Integer getProjectType() {
return projectType;
}
/**
* 获取projectType.
* @param projectType 要设置的projectType
*/
public void setProjectType(Integer projectType) {
this.projectType = projectType;
}
/**
* 设置isBatchInsert.
* @return 返回isBatchInsert
*/
public Boolean getIsBatchInsert() {
return isBatchInsert;
}
/**
* 获取isBatchInsert.
* @param isBatchInsert 要设置的isBatchInsert
*/
public void setIsBatchInsert(Boolean isBatchInsert) {
this.isBatchInsert = isBatchInsert;
}
/**
* 设置serviceInterfaceClass.
* @return 返回serviceInterfaceClass
*/
public String getServiceInterfaceClass() {
return serviceInterfaceClass;
}
/**
* 获取serviceInterfaceClass.
* @param serviceInterfaceClass 要设置的serviceInterfaceClass
*/
public void setServiceInterfaceClass(String serviceInterfaceClass) {
this.serviceInterfaceClass = serviceInterfaceClass;
}
/**
* 设置businessInterfaceClass.
* @return 返回businessInterfaceClass
*/
public String getBusinessInterfaceClass() {
return businessInterfaceClass;
}
/**
* 获取businessInterfaceClass.
* @param businessInterfaceClass 要设置的businessInterfaceClass
*/
public void setBusinessInterfaceClass(String businessInterfaceClass) {
this.businessInterfaceClass = businessInterfaceClass;
}
/**
* 设置daoInterfaceClass.
* @return 返回daoInterfaceClass
*/
public String getDaoInterfaceClass() {
return daoInterfaceClass;
}
/**
* 获取daoInterfaceClass.
* @param daoInterfaceClass 要设置的daoInterfaceClass
*/
public void setDaoInterfaceClass(String daoInterfaceClass) {
this.daoInterfaceClass = daoInterfaceClass;
}
}
根据模板生成代码测试类:
public static void main(String[] args) throws Exception {
VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.setProperty("input.encoding", "utf-8");
ve.setProperty("output.encoding", "utf-8");
ve.init();
Template t = ve.getTemplate("com\\better517na\\autocodgen\\resources\\modelBo.vm");
VelocityContext ctx = new VelocityContext();
/*************************model属性设置开始*************************/
List<Map<String, Object>> rowsdata = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("fieldName", "KeyID");
map.put("fieldChName", "主键keyID");
map.put("javaType", "String");
map.put("fieldNameFormat", "keyID");
rowsdata.add(map);
map= new LinkedHashMap<String, Object>();
map.put("fieldName", "Name");
map.put("fieldChName", "姓名");
map.put("javaType", "String");
map.put("fieldNameFormat", "name");
rowsdata.add(map);
/***********************model属性设置结束************************/
ExcelModel model=new ExcelModel();
model.setFileName("ClientTable");
model.setTableName("客户信息表");
model.setModelName("ClientTable");
model.setRowsData(rowsdata);
model.setModelBoName("ClientTable");
model.setBoPackageName("com.aaa.bbb");
ctx.put("model", model);
ctx.put("author", "peiyu");
ctx.put("year", new SimpleDateFormat("yyyy").format(new Date()));
ctx.put("date", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
StringWriter sw = new StringWriter();
//模板合并,得到期望文件
t.merge(ctx, sw);
System.out.println(sw.toString());
}
生成的代码:
/*
* 文件名:ClientTable
* 版权:Copyright 2007-2016 517na Tech. Co. Ltd. All Rights Reserved.
* 描述: ClientTable
* 修改人:peiyu
* 修改时间:2016-06-21
* 修改内容:新增
*/
package com.aaa.bbb;
import java.io.Serializable;
/**
* 客户信息表.
* @author peiyu
*/
public class ClientTable implements Serializable {
/**
* 序列化.
*/
private static final long serialVersionUID = 1L;
/**
* @表名
*/
private String tableName;
/**
* 获取 表名.
* @return 表名
*/
public String getTableName(){
return tableName;
};
/**
* 设置 表名.
* @param tableName 表名
*/
public void setTableName(String tableName){
this.tableName=tableName;
};
/**
* @主键keyID
*/
private String keyID;
/**
* @姓名
*/
private String name;
/**
* 获取 主键keyID.
* @return 返回主键keyID
*/
public String getKeyID() {
return keyID;
}
/**
* 设置 主键keyID.
* @param keyID 主键keyID
*/
public void setKeyID(String keyID) {
this.keyID = keyID;
}
/**
* 获取 姓名.
* @return 返回姓名
*/
public String getName() {
return name;
}
/**
* 设置 姓名.
* @param name 姓名
*/
public void setName(String name) {
this.name = name;
}
}