天天看點

利用Freemarker實作表到功能界面的一鍵生成

      利用Freemarker産生項目所需的常用檔案,可以根據一個表産生對應的ibtais的java及xml檔案,及前台的JSP檔案,這樣可以大大的提高普通程式編寫人員的開發效率,及從表到界面及CURD功能利用這個工程可以直接生成好,下面提供下最核心的檔案産生類,代碼如下:

  1. package com.hundsun.pms.generator.util; 
  2. import com.hundsun.pms.generator.api.Constants; 
  3. import com.hundsun.pms.generator.model.page.Page; 
  4. import freemarker.template.Configuration; 
  5. import freemarker.template.DefaultObjectWrapper; 
  6. import freemarker.template.Template; 
  7. import freemarker.template.TemplateException; 
  8. import org.apache.commons.lang.StringUtils; 
  9. import org.springframework.util.ResourceUtils; 
  10. import java.io.*; 
  11. import java.net.URL; 
  12. import java.util.Locale; 
  13. import java.util.Map; 
  14. /** 
  15.  * Created by IntelliJ IDEA. 
  16.  * User: pangj 
  17.  * Date: 2012-3-6 
  18.  * Time: 10:05:26 
  19.  * 檔案操作工具類 
  20.  */ 
  21. public class FreemarkerUtil { 
  22.     public static String PROJECT_ROOT = "projectRoot"; 
  23.     public static String PACKAGE_NAME = "packageName"; 
  24.     public static String CLASS_SIMPLE_NAME = "classSimpleName"; 
  25.     public static String MODULES_PACKAGE = "modulesPackage"; 
  26.     public static String METHOD_NAME = "methodName"; 
  27.     //模闆根目錄 
  28.     private static final String templateRootWithClasspath = "classpath*:com/hundsun/pms/generator/resource/template"; 
  29.     private static final String templateRoot = "com/hundsun/pms/generator/resource/template"; 
  30.     //工程根目錄 
  31.     private static String projectRoot; 
  32.     //包名 
  33.     private static String packageName; 
  34.     //服務所在的包路徑 
  35.     private static String modulesPackage; 
  36.     private static String classSimpleName; 
  37.     private static String templateName; 
  38.     private static String methodName; 
  39.     public static final String FILE_BO_NAME_SUFFIX = "Bo.java"; 
  40.     public static final String FILE_BOIMPL_NAME_SUFFIX = "BoImpl.java"; 
  41.     public static final String FILE_DAO_NAME_SUFFIX = "Dao.java"; 
  42.     public static final String FILE_DAOIMPL_NAME_SUFFIX = "DaoImpl.java"; 
  43.     public static final String FILE_DTO_NAME_SUFFIX = ".java"; 
  44.     public static final String FILE_SQL_MAP_SUFFIX = ".xml"; 
  45.     public static final String FILE_SERVICE_SUFFIX = ".service"; 
  46.     public static final String FILE_COMPONENT_SUFFIX = ".component"; 
  47.     public static final String FILE_MODULE_NAME = "module.xml"; 
  48.     public static final String FILE_PAGE_SUFFIX = ".jsp"; 
  49.     /** 
  50.      * 資料綁定至模闆 
  51.      * 
  52.      * @param tpName 模闆名 
  53.      * @param param  參數 
  54.      * @return 
  55.      */ 
  56.     public static void process(String tpName, Map<String, Object> param) { 
  57.         init(tpName, param); 
  58.         Writer out = null; 
  59.         try { 
  60.             Template temp = getTemplate(templateName); 
  61.             String fileDir = getFileDir(); 
  62.             File dir = new File(fileDir); 
  63.             if (!dir.exists()) { 
  64.                 if (!dir.mkdirs()) { 
  65.                     MsgUtil.print("建立目錄:" + fileDir + "失敗"); 
  66.                     return; 
  67.                 } 
  68.             } 
  69.             File f = new File(fileDir + getFileName()); 
  70.             out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "utf-8")); 
  71.             try { 
  72.                 temp.setEncoding("utf-8"); 
  73.                 temp.process(param, out); 
  74.                 MsgUtil.print("檔案:" + getFileDir() + getFileName() + "生成完成!"); 
  75.             } catch (TemplateException e) { 
  76.                 MsgUtil.print("檔案:" + getFileDir() + getFileName() + "生成失敗,請檢查原因!"); 
  77.                 MsgUtil.print(e.getMessage()); 
  78.             } 
  79.             out.flush(); 
  80.             out.close(); 
  81.         } catch (IOException e) { 
  82.             e.printStackTrace(); 
  83.             MsgUtil.print(e.getMessage()); 
  84.         } 
  85.     } 
  86.     /** 
  87.      * 生成頁面 
  88.      * 
  89.      * @param tpName 模闆名 
  90.      * @param jsp    jsp頁面全路徑 
  91.      * @param page   頁面對象 
  92.      */ 
  93.     public static void process(String tpName, String jsp, Page page) { 
  94.         Writer writer = null; 
  95.         try { 
  96.             Template tmp = FreemarkerUtil.getTemplate(tpName); 
  97.             String dir = jsp.substring(0, jsp.lastIndexOf("\\")); 
  98.             File fdir = new File(dir); 
  99.             if (!fdir.exists()) { 
  100.                 if (!fdir.mkdirs()) { 
  101.                     MsgUtil.print("建立目錄:" + fdir + "失敗"); 
  102.                     return; 
  103.                 } 
  104.             } 
  105.             File file = new File(jsp); 
  106.             writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8")); 
  107.             tmp.setEncoding("utf-8"); 
  108.             tmp.process(page, writer); 
  109.             writer.flush(); 
  110.             writer.close(); 
  111.             MsgUtil.print("頁面:" + jsp + " 生成成功!"); 
  112.         } catch (TemplateException e) { 
  113.             e.printStackTrace(); 
  114.             MsgUtil.print(e.getMessage()); 
  115.         } catch (IOException e) { 
  116.             e.printStackTrace(); 
  117.             MsgUtil.print(e.getMessage()); 
  118.         } 
  119.     } 
  120.     public static Template getTemplate(String templateName) { 
  121.         Configuration cfg = new Configuration(); 
  122.         Template temp = null; 
  123.         URL path = FreemarkerUtil.class.getClassLoader().getSystemResource(""); 
  124.         File tmpRootFile = null; 
  125.         if (path == null) { 
  126.             tmpRootFile = getResourceURL("resource/template"); 
  127.         } else { 
  128.             String tmpRoot = path + templateRoot; 
  129.             tmpRoot = tmpRoot.replaceAll("file:/", ""); 
  130.             tmpRootFile = new File(tmpRoot); 
  131.         } 
  132.         if (tmpRootFile == null) { 
  133.             throw new RuntimeException("無法取得模闆根路徑!"); 
  134.         } 
  135.         try { 
  136.             cfg.setDefaultEncoding("utf-8"); 
  137.             cfg.setOutputEncoding("utf-8"); 
  138.             cfg.setDirectoryForTemplateLoading(tmpRootFile); 
  139.             /*cfg.setDirectoryForTemplateLoading(getResourceURL());*/ 
  140.             cfg.setObjectWrapper(new DefaultObjectWrapper()); 
  141.             temp = cfg.getTemplate(templateName); 
  142.         } catch (IOException e) { 
  143.             e.printStackTrace(); 
  144.             MsgUtil.print(e.getMessage()); 
  145.         } 
  146.         return temp; 
  147.     } 
  148.     public static File getResourceURL(String templatePath) { 
  149.         try { 
  150.             URL url = ResourceUtils.getURL(templateRootWithClasspath); 
  151.             String path = url.getPath(); 
  152.             path = path.replace(templateRootWithClasspath, templatePath); 
  153.             MsgUtil.print("File path is:"+path); 
  154.             path = StringUtils.replace(path, "%20", " "); 
  155.             return new File(path); 
  156.         } catch (FileNotFoundException e) { 
  157.             throw new RuntimeException("不能擷取路徑:" + templateRootWithClasspath); 
  158.         } 
  159.     } 
  160.     private static void init(String tpName, Map<String, Object> param) { 
  161.         projectRoot = (String) param.get(PROJECT_ROOT); 
  162.         packageName = (String) param.get(PACKAGE_NAME); 
  163.         classSimpleName = (String) param.get(CLASS_SIMPLE_NAME); 
  164.         modulesPackage = (String) param.get(MODULES_PACKAGE); 
  165.         methodName = (String) param.get(METHOD_NAME); 
  166.         templateName = tpName; 
  167.     } 
  168.     private static String getFileName() { 
  169.         if (Constants.TEMPLATE_BO.equals(templateName)) { 
  170.             return classSimpleName + FILE_BO_NAME_SUFFIX; 
  171.         } else if (Constants.TEMPLATE_BOIMPL.equals(templateName)) { 
  172.             return classSimpleName + FILE_BOIMPL_NAME_SUFFIX; 
  173.         } else if (Constants.TEMPLATE_DAO.equals(templateName)) { 
  174.             return classSimpleName + FILE_DAO_NAME_SUFFIX; 
  175.         } else if (Constants.TEMPLATE_DAOIMPL.equals(templateName)) { 
  176.             return classSimpleName + FILE_DAOIMPL_NAME_SUFFIX; 
  177.         } else if (Constants.TEMPLATE_DTO.equals(templateName)) { 
  178.             return classSimpleName + FILE_DTO_NAME_SUFFIX; 
  179.         } else if (Constants.TEMPLATE_SQLMAP.equals(templateName)) { 
  180.             return classSimpleName + FILE_SQL_MAP_SUFFIX; 
  181.         } else if (Constants.TEMPLATE_SERVICE.equals(templateName)) { 
  182.             return methodName + FILE_SERVICE_SUFFIX; 
  183.         } else if (Constants.TEMPLATE_COMPONENT.equals(templateName)) { 
  184.             return NamingRuleConvert.firstLetterToLowerCase(classSimpleName) + FILE_COMPONENT_SUFFIX; 
  185.         } else if (Constants.TEMPLATE_CS.equals(templateName) || Constants.TEMPLATE_DS.equals(templateName) || 
  186.                 Constants.TEMPLATE_MODULE.equals(templateName)) { 
  187.             return FILE_MODULE_NAME; 
  188.         } 
  189.         return null; 
  190.     } 
  191.     private static String getFileDir() { 
  192.         String path = null; 
  193.         if (packageName != null) 
  194.             path = packageName.replaceAll("\\.", "/"); 
  195.         String servicePath = null; 
  196.         if (!StringUtils.isEmpty(modulesPackage)) { 
  197.             servicePath = modulesPackage.replaceAll("\\.", "/"); 
  198.         } 
  199.         if (Constants.TEMPLATE_BO.equals(templateName)) { 
  200.             return projectRoot + Constants.SRC_DIR + path + "/bo/"; 
  201.         } else if (Constants.TEMPLATE_BOIMPL.equals(templateName)) { 
  202.             return projectRoot + Constants.SRC_DIR + path + "/bo/impl/"; 
  203.         } else if (Constants.TEMPLATE_DAO.equals(templateName)) { 
  204.             return projectRoot + Constants.SRC_DIR + path + "/dao/"; 
  205.         } else if (Constants.TEMPLATE_DAOIMPL.equals(templateName)) { 
  206.             return projectRoot + Constants.SRC_DIR + path + "/dao/impl/"; 
  207.         } else if (Constants.TEMPLATE_DTO.equals(templateName)) { 
  208.             return projectRoot + Constants.SRC_DIR + path + "/dto/"; 
  209.         } else if (Constants.TEMPLATE_SQLMAP.equals(templateName)) { 
  210.             return projectRoot + Constants.SRC_DIR + path + "/dao/maps/"; 
  211.         } else if (Constants.TEMPLATE_SERVICE.equals(templateName) || (Constants.TEMPLATE_CS.equals(templateName))) { 
  212.             return projectRoot + "/modules/" + servicePath + "/cs/"; 
  213.         } else if (Constants.TEMPLATE_COMPONENT.equals(templateName) || Constants.TEMPLATE_DS.equals(templateName)) { 
  214.             return projectRoot + "/modules/" + servicePath + "/ds/"; 
  215.         } else if (Constants.TEMPLATE_MODULE.equals(templateName)) { 
  216.             return projectRoot + "/modules/" + servicePath + "/"; 
  217.         } 
  218.         return null; 
  219.     } 

繼續閱讀