定义模板:
package com.media.dao;
import java.util.List;
import com.media.bean.${model_name};
import com.media.exceptions.DAOException;
/**
* ${model_name_cn}接口
*
* @author ${author}
* @link ${link}
*
* @version $Revision: 1.00 $ $Date: ${date?string("yyyy-MM-dd HH:mm:ss")}
*/
public interface I${model_name}DAO extends IGenericDAO<${model_name}>{
/**
* 根据${model_name_cn}编号查找${model_name_cn}信息
*
* @param ${instant}Id ${model_name_cn}编号
* @return ${model_name} ${model_name_cn}对象
* @throws DAOException
*/
public ${model_name} find${model_name}ById(Long ${instant}Id) throws DAOException;
/**
* 批量物理删除${model_name_cn}(不可恢复)
* @param ${instant}Ids ${model_name_cn}编号
* @throws DAOException
*/
public void delete${model_name_list}(Long[] ${instant}Ids) throws DAOException;
/**
* 物理删除${model_name_cn}(不可恢复)
* @param ${instant}Id ${model_name_cn}编号
* @throws DAOException
*/
public void delete${model_name}(Long ${instant}Id) throws DAOException;
/**
* 保存${model_name_cn}
* @param ${instant}
* @throws DAOException
*/
public void save${model_name}(${model_name} ${instant}) throws DAOException;
/**
* 更新${model_name_cn}
* @param ${instant}
* @throws DAOException
*/
public void update${model_name}(${model_name} ${instant}) throws DAOException;
/**
* 利用hql语句查询${model_name_cn}信息
* @param hsql
* @throws DAOException
*/
public List<${model_name}> find${model_name_list}(String hsql) throws DAOException;
/**
* 利用hql语句查询${model_name_cn}信息
* @param hsql
* @throws DAOException
*/
public List<${model_name}> find${model_name_list}(String hsql,Object[] params) throws DAOException;
}
输出内容:
package com;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class FreeMarkerUtil {
private Configuration cfg;
public void init() throws Exception {
// 初始化FreeMarker配置
// 创建一个Configuration实例
cfg = new Configuration();
// 设置FreeMarker的模版文件位置
cfg.setDirectoryForTemplateLoading(new File(System
.getProperty("user.dir") + "\\template"));
}
public void process(FreeMarkerUtil hf) throws Exception {
Map root = new HashMap();
String Module = "";
String model_name = "User";
String model_name_list = "Users";
String instant = "user";
String model_name_cn = "用户";
String author = "xxxx";
String link = "<a href=http://www.xxxx.com.cn>xxxx</a>";
Date date = new Date();
root.put("module", Module);
root.put("model_name", model_name);
root.put("model_name_list", model_name_list);
root.put("instant", instant);
root.put("model_name_cn", model_name_cn);
root.put("author", author);
root.put("link", link);
root.put("date", date);
String projectPath = "D://Testout//";
String fileName = "I" + model_name + "DAO.java";
String savePath = "src//com//media//dao//";
Template template = cfg.getTemplate("IDAO.ftl");
hf.buildTemplate(root, projectPath, savePath, fileName, template);
}
public void buildTemplate(Map root, String projectPath, String savePath,
String fileName, Template template) {
String realFileName = projectPath + savePath + fileName;
String realSavePath = projectPath + "/" + savePath;
File newsDir = new File(realSavePath);
if (!newsDir.exists()) {
newsDir.mkdirs();
}
try {
// SYSTEM_ENCODING = "UTF-8";
Writer out = new OutputStreamWriter(new FileOutputStream(
realFileName));
template.process(root, out);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
FreeMarkerUtil hf = new FreeMarkerUtil();
hf.init();
hf.process(hf);
}
}