使用FreeMarker模板生成java代码的例子

本文介绍如何使用模板和Freemaker工具自动生成用户接口代码,包括DAO接口、Hibernate实现、服务层和服务实现层,通过简化开发过程提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步.新建一个模板文件以.ftl结尾。

IDAO.ftl

 

[java]  view plain copy
  1. package com.media.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.media.bean.${model_name};  
  6. import com.media.exceptions.DAOException;  
  7.   
  8. /** 
  9.  * ${model_name_cn}接口  
  10.  *  
  11.  * @author ${author} 
  12.  * @link ${link} 
  13.  *  
  14.  * @version $Revision: 1.00 $ $Date: ${date?string("yyyy-MM-dd HH:mm:ss")} 
  15.  */  
  16. public interface I${model_name}DAO extends IGenericDAO<${model_name}>{  
  17.       
  18.     /** 
  19.      * 根据${model_name_cn}编号查找${model_name_cn}信息 
  20.      *  
  21.      * @param ${instant}Id ${model_name_cn}编号 
  22.      * @return ${model_name} ${model_name_cn}对象 
  23.      * @throws DAOException 
  24.      */   
  25.     public ${model_name} find${model_name}ById(Long ${instant}Id) throws DAOException;  
  26.       
  27.     /** 
  28.      * 批量物理删除${model_name_cn}(不可恢复) 
  29.      * @param ${instant}Ids  ${model_name_cn}编号 
  30.      * @throws DAOException 
  31.      */  
  32.     public void delete${model_name_list}(Long[] ${instant}Ids) throws DAOException;  
  33.       
  34.     /** 
  35.      * 物理删除${model_name_cn}(不可恢复) 
  36.      * @param ${instant}Id  ${model_name_cn}编号 
  37.      * @throws DAOException 
  38.      */  
  39.     public void delete${model_name}(Long ${instant}Id) throws DAOException;  
  40.       
  41.     /** 
  42.      * 保存${model_name_cn} 
  43.      * @param ${instant} 
  44.      * @throws DAOException 
  45.      */  
  46.     public void save${model_name}(${model_name} ${instant}) throws DAOException;  
  47.       
  48.     /** 
  49.      * 更新${model_name_cn} 
  50.      * @param ${instant} 
  51.      * @throws DAOException 
  52.      */  
  53.     public void update${model_name}(${model_name} ${instant}) throws DAOException;  
  54.       
  55.     /** 
  56.      * 利用hql语句查询${model_name_cn}信息 
  57.      * @param hsql 
  58.      * @throws DAOException 
  59.      */  
  60.     public List<${model_name}> find${model_name_list}(String hsql) throws DAOException;  
  61.       
  62.     /** 
  63.      * 利用hql语句查询${model_name_cn}信息 
  64.      * @param hsql 
  65.      * @throws DAOException 
  66.      */  
  67.     public List<${model_name}> find${model_name_list}(String hsql,Object[] params) throws DAOException;  
  68.   
  69. }  

 

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

FreeMarkerUtil.java

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

 

[java]  view plain copy
  1. package com.media.test;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.OutputStreamWriter;  
  6. import java.io.Writer;  
  7. import java.util.Date;  
  8. import java.util.HashMap;  
  9. import java.util.Map;  
  10.   
  11. import com.media.utils.Constants;  
  12.   
  13. import freemarker.template.Configuration;  
  14. import freemarker.template.Template;  
  15.   
  16. public class FreeMarkerUtil {  
  17.   
  18.     private Configuration cfg;  
  19.   
  20.     public void init() throws Exception {  
  21.         // 初始化FreeMarker配置  
  22.         // 创建一个Configuration实例  
  23.         cfg = new Configuration();  
  24.         // 设置FreeMarker的模版文件位置  
  25.         cfg.setDirectoryForTemplateLoading(new File(  
  26.                 "D://Workspaces//MyEclipse75//s2sh//WebRoot//template"));  
  27.     }  
  28.   
  29.     public void process(FreeMarkerUtil hf) throws Exception {  
  30.   
  31.         Map root = new HashMap();  
  32.   
  33.         String Module = "";  
  34.         String model_name = "User";  
  35.         String model_name_list = "Users";  
  36.         String instant = "user";  
  37.         String model_name_cn = "用户";  
  38.         String author = "张何兵";  
  39.         String link = "<a href=http://www.media999.com.cn>北京华亚美科技有限公司</a>";// 模块开发公司网地址  
  40.         Date date = new Date();  
  41.   
  42.         root.put("module", Module);  
  43.         root.put("model_name", model_name);  
  44.         root.put("model_name_list", model_name_list);  
  45.         root.put("instant", instant);  
  46.         root.put("model_name_cn", model_name_cn);  
  47.         root.put("author", author);  
  48.         root.put("link", link);  
  49.         root.put("date", date);  
  50.   
  51.         String projectPath = "D://Workspaces//MyEclipse75//s2sh//";  
  52.   
  53.         String fileName = "I" + model_name + "DAO.java";  
  54.   
  55.         String savePath = "src//com//media//dao//";  
  56.   
  57.         Template template = cfg.getTemplate("IDAO.ftl");  
  58.   
  59.         hf.buildTemplate(root, projectPath, savePath, fileName, template);  
  60.   
  61.         fileName = model_name + "DAOHibernate.java";  
  62.         savePath = "src//com//media//dao//hibernate//";  
  63.         template = cfg.getTemplate("DAOHibernate.ftl");  
  64.         hf.buildTemplate(root, projectPath, savePath, fileName, template);  
  65.   
  66.         fileName = model_name + "Service.java";  
  67.         savePath = "src//com//media//service//";  
  68.         template = cfg.getTemplate("Service.ftl");  
  69.         hf.buildTemplate(root, projectPath, savePath, fileName, template);  
  70.   
  71.         fileName = model_name + "ServiceImpl.java";  
  72.         savePath = "src//com//media//service//impl//";  
  73.         template = cfg.getTemplate("ServiceImpl.ftl");  
  74.         hf.buildTemplate(root, projectPath, savePath, fileName, template);  
  75.   
  76.     }  
  77.   
  78.     public void buildTemplate(Map root, String projectPath, String savePath,  
  79.             String fileName, Template template) {  
  80.         String realFileName = projectPath + savePath + fileName;  
  81.   
  82.         String realSavePath = projectPath + "/" + savePath;  
  83.   
  84.         File newsDir = new File(realSavePath);  
  85.         if (!newsDir.exists()) {  
  86.             newsDir.mkdirs();  
  87.         }  
  88.   
  89.         try {  
  90.             // SYSTEM_ENCODING = "UTF-8";  
  91.             Writer out = new OutputStreamWriter(new FileOutputStream(  
  92.                     realFileName), Constants.SYSTEM_ENCODING);  
  93.   
  94.             template.process(root, out);  
  95.         } catch (Exception e) {  
  96.             e.printStackTrace();  
  97.         }  
  98.   
  99.     }  
  100.   
  101.     public static void main(String[] args) throws Exception {  
  102.         FreeMarkerUtil hf = new FreeMarkerUtil();  
  103.         hf.init();  
  104.         hf.process(hf);  
  105.     }  
  106.   
  107. }  

 

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

生成代码如下:

IUserDAO.java

 

[java]  view plain copy
  1. package com.media.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.media.bean.User;  
  6. import com.media.exceptions.DAOException;  
  7.   
  8. /** 
  9.  * 用户接口  
  10.  *  
  11.  * @author 张何兵 
  12.  * @link <a href=http://www.media999.com.cn>北京华亚美科技有限公司</a> 
  13.  *  
  14.  * @version $Revision: 1.00 $ $Date: 2009-10-26 17:09:12 
  15.  */  
  16. public interface IUserDAO extends IGenericDAO<User>{  
  17.       
  18.     /** 
  19.      * 根据用户编号查找用户信息 
  20.      *  
  21.      * @param userId 用户编号 
  22.      * @return User 用户对象 
  23.      * @throws DAOException 
  24.      */   
  25.     public User findUserById(Long userId) throws DAOException;  
  26.       
  27.     /** 
  28.      * 批量物理删除用户(不可恢复) 
  29.      * @param userIds  用户编号 
  30.      * @throws DAOException 
  31.      */  
  32.     public void deleteUsers(Long[] userIds) throws DAOException;  
  33.       
  34.     /** 
  35.      * 物理删除用户(不可恢复) 
  36.      * @param userId  用户编号 
  37.      * @throws DAOException 
  38.      */  
  39.     public void deleteUser(Long userId) throws DAOException;  
  40.       
  41.     /** 
  42.      * 保存用户 
  43.      * @param user 
  44.      * @throws DAOException 
  45.      */  
  46.     public void saveUser(User user) throws DAOException;  
  47.       
  48.     /** 
  49.      * 更新用户 
  50.      * @param user 
  51.      * @throws DAOException 
  52.      */  
  53.     public void updateUser(User user) throws DAOException;  
  54.       
  55.     /** 
  56.      * 利用hql语句查询用户信息 
  57.      * @param hsql 
  58.      * @throws DAOException 
  59.      */  
  60.     public List<User> findUsers(String hsql) throws DAOException;  
  61.       
  62.     /** 
  63.      * 利用hql语句查询用户信息 
  64.      * @param hsql 
  65.      * @throws DAOException 
  66.      */  
  67.     public List<User> findUsers(String hsql,Object[] params) throws DAOException;  
  68.   
  69. }  
  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值