您的位置:首页 > 编程语言 > Java开发

我的第一个FreeMarker模板生成java代码的例子

2009-10-27 09:08 531 查看
第一步.新建一个模板文件以.ftl结尾。

IDAO.ftl

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;

}


第二步.写一个freemaker的工具类用于生成代码。

FreeMarkerUtil.java

注意:工程必须引入freemaker.jar

package com.media.test;

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 com.media.utils.Constants;

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(
"D://Workspaces//MyEclipse75//s2sh//WebRoot//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 = "张何兵";
String link = "<a href=http://www.media999.com.cn>北京华亚美科技有限公司</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://Workspaces//MyEclipse75//s2sh//";

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);

fileName = model_name + "DAOHibernate.java";
savePath = "src//com//media//dao//hibernate//";
template = cfg.getTemplate("DAOHibernate.ftl");
hf.buildTemplate(root, projectPath, savePath, fileName, template);

fileName = model_name + "Service.java";
savePath = "src//com//media//service//";
template = cfg.getTemplate("Service.ftl");
hf.buildTemplate(root, projectPath, savePath, fileName, template);

fileName = model_name + "ServiceImpl.java";
savePath = "src//com//media//service//impl//";
template = cfg.getTemplate("ServiceImpl.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), Constants.SYSTEM_ENCODING);

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);
}

}


第三步:运行该工具类,生成代码。

生成代码如下:

IUserDAO.java

package com.media.dao;

import java.util.List;

import com.media.bean.User;
import com.media.exceptions.DAOException;

/**
* 用户接口
*
* @author 张何兵
* @link <a href=http://www.media999.com.cn>北京华亚美科技有限公司</a>
*
* @version $Revision: 1.00 $ $Date: 2009-10-26 17:09:12
*/
public interface IUserDAO extends IGenericDAO<User>{

/**
* 根据用户编号查找用户信息
*
* @param userId 用户编号
* @return User 用户对象
* @throws DAOException
*/
public User findUserById(Long userId) throws DAOException;

/**
* 批量物理删除用户(不可恢复)
* @param userIds  用户编号
* @throws DAOException
*/
public void deleteUsers(Long[] userIds) throws DAOException;

/**
* 物理删除用户(不可恢复)
* @param userId  用户编号
* @throws DAOException
*/
public void deleteUser(Long userId) throws DAOException;

/**
* 保存用户
* @param user
* @throws DAOException
*/
public void saveUser(User user) throws DAOException;

/**
* 更新用户
* @param user
* @throws DAOException
*/
public void updateUser(User user) throws DAOException;

/**
* 利用hql语句查询用户信息
* @param hsql
* @throws DAOException
*/
public List<User> findUsers(String hsql) throws DAOException;

/**
* 利用hql语句查询用户信息
* @param hsql
* @throws DAOException
*/
public List<User> findUsers(String hsql,Object[] params) throws DAOException;

}


OK,大功告成。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: