我的第一個FreeMarker模板產生java代碼的例子

來源:互聯網
上載者:User

第一步.建立一個模板檔案以.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,大功告成。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.