第一步。写一个模板类。用于封装信息。
FreeMakerTemplateModel.java
package com.media.genericmechine; import java.util.HashMap; import java.util.Map; public class FreeMakerTemplateModel { // 项目路径 private String projectPath; // 模板文件路径比如:WebRoot/template private String templatePath; // 模板文件的名称比如:IDAO.ftl private String templateName; // 要存储的位置如:src/com/media/dao/hibernate/ // (一般是类的包的路径) private String savePath; // 要保存的文件的名称,一般是类名称 private String saveFileName; //数据models注入 @SuppressWarnings("unchecked") private Map models = new HashMap(); @SuppressWarnings("unchecked") public Map getModels() { return models; } @SuppressWarnings("unchecked") public void setModels(Map models) { this.models = models; } public String getProjectPath() { return projectPath; } public void setProjectPath(String projectPath) { this.projectPath = projectPath; } public String getTemplatePath() { return templatePath; } public void setTemplatePath(String templatePath) { this.templatePath = templatePath; } public String getTemplateName() { return templateName; } public void setTemplateName(String templateName) { this.templateName = templateName; } public String getSavePath() { return savePath; } public void setSavePath(String savePath) { this.savePath = savePath; } public String getSaveFileName() { return saveFileName; } public void setSaveFileName(String saveFileName) { this.saveFileName = saveFileName; } }
第二步。写一个服务类,用来创建模板。
ModelGenerateService.java
package com.media.genericmechine; import java.io.IOException; import java.util.Date; public class ModelGenerateService { /** * * @param modelArray * 模型列表 * @param templatePath * 模版路径 * @param Module * 模块名称 * @param projctePath * 项目路径 * @throws ClassNotFoundException * @throws IOException */ @SuppressWarnings("unchecked") public void builderCode(String[] modelArray, String templatePath, String Module, String projectPath, String modelPath) throws ClassNotFoundException, IOException { for (int i = 0, j = modelArray.length; i < j; i++) { String temp = modelArray[i]; /** 初始化工程数据 **/ String[] tempStr = org.apache.commons.lang.StringUtils.split(temp, "|"); // String folder = tempStr[0];//模块路径 String model_name = tempStr[0];// 模块model名称 String model_name_list = tempStr[1];// 模块model名称复数 String instant = tempStr[2];// 模块model实例名称 String model_name_cn = tempStr[3];// 模块中文名称 String author = "张何兵";// 模块开发作者 String link = "<a href="http://www.media999.com.cn" mce_href="http://www.media999.com.cn">北京华亚美科技有限公司</a>";// 模块开发公司网地址 Date date = new Date();// 模块开发日期 FreeMakerTemplateModel t = new FreeMakerTemplateModel(); t.getModels().put("module", Module); t.getModels().put("model_name", model_name); t.getModels().put("model_name_list", model_name_list); t.getModels().put("instant", instant); t.getModels().put("model_name_cn", model_name_cn); t.getModels().put("author", author); t.getModels().put("link", link); t.getModels().put("date", date); t.setProjectPath(projectPath); t.setTemplatePath(templatePath); /** 生成dao接口 */ t.setTemplateName("IDAO.ftl"); t.setSavePath("src/com/media/dao/"); t.setSaveFileName("I" + model_name + "DAO.java"); FreeMakerUtil.builderTemplate(t); /** 生成DAO实现 */ t.setTemplateName("DAOHibernate.ftl"); t.setSavePath("src/com/media/dao/hibernate/"); t.setSaveFileName(model_name + "DAOHibernate.java"); FreeMakerUtil.builderTemplate(t); /** 生成Service接口 */ t.setTemplateName("IService.ftl"); t.setSavePath("src/com/media/service/"); t.setSaveFileName("I" + model_name + "Service.java"); FreeMakerUtil.builderTemplate(t); /** 生成Service实现 */ t.setTemplateName("ServiceImpl.ftl"); t.setSavePath("src/com/media/service/"); t.setSaveFileName(model_name + "ServiceImpl.java"); FreeMakerUtil.builderTemplate(t); } } }
第三步。在相应的action中来调用服务类的builderCode方法来进行生成代码。
代码省略。