標籤:代碼 main 如何 art 意思 config ack 本質 buffered
FreeMarker根據模板產生Java代碼,光這句話,大家想必也知道它的應用了,比如流行的DRY原則,該原則的意思,可簡單概述為"不要寫重複的代碼"。
比如Java中三層架構,資料訪問層,商務邏輯層,表現層,光這三層就出現重複性的增刪改查及其相關的介面代碼。
如何不寫重複的增刪改查相關的代碼,可以參考我的MP實戰系列文章和MyBatis的逆向工程(針對Java相關架構):
mybatis逆向工程之maven工程
MP實戰系列(六)之代碼產生器講解
其中MP實戰系列(六)之代碼產生器講解 用的是volocity模板引擎。原理與FreeMarker本質上是一致的,這個本質一致,你可以理解為都是基於已有的模板進行代碼產生。
下面進入簡單樣本講解:
一、匯入maven依賴
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.test</groupId> <artifactId>freemarket</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <!-- <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <warSourceDirectory>src/main/webapp</warSourceDirectory> </configuration> </plugin> --> </plugins> <finalName>${project.artifactId}</finalName> </build></project>
二、建立com.freemarker.hello.templates包,並在該包下編寫模板檔案test.ftl
package ${classPath};public class ${className} { private Integer ${Id}; private String ${userName}; private String ${password}; public Integer get${Id}(){ return ${Id}; } public void set${Id}(Integer ${Id}){ this.${Id}=${Id}; } public String get${userName}(){ return ${userName}; } public void set${userName}(String ${userName}){ this.${userName}=${userName}; } public String get${password}(){ return ${password}; } public void set${password}(String ${password}){ this.${password}=${password}; }}
三、編寫運行產生對應Java代碼類
package com.freemark.hello;import java.io.BufferedWriter;import java.io.File;import java.io.FileOutputStream;import java.io.OutputStreamWriter;import java.io.Writer;import java.util.HashMap;import java.util.Map;import freemarker.template.Configuration;import freemarker.template.Template;public class FreemarkerDemo { private static final String TEMPLATE_PATH = "src/main/java/com/freemark/hello/templates"; private static final String CLASS_PATH = "src/main/java/com/freemark/hello"; public static void main(String[] args) { // step1 建立freeMarker配置執行個體 Configuration configuration = new Configuration(); Writer out = null; try { // step2 擷取模版路徑 configuration.setDirectoryForTemplateLoading(new File(TEMPLATE_PATH)); // step3 建立資料模型 Map<String, Object> dataMap = new HashMap<String, Object>(); dataMap.put("classPath", "com.freemark.hello"); dataMap.put("className", "User"); dataMap.put("Id", "Id"); dataMap.put("userName", "userName"); dataMap.put("password","password"); // step4 載入模版檔案 Template template = configuration.getTemplate("test.ftl"); // step5 產生資料 File docFile = new File(CLASS_PATH + "\\" + "User.java"); out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile))); // step6 輸出檔案 template.process(dataMap, out); System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^User.java 檔案建立成功 !"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != out) { out.flush(); } } catch (Exception e2) { e2.printStackTrace(); } } }}
四、步驟三成功,重新整理(refresh)項目即可,看到com.freemark.hello有一個User類
小結:該樣本非常簡單,freemarker的強大不足以在此體現,不過該樣本對於初學者或者準備研究freemarker的開發人員有一定的啟示意義。
比如:遵守"DRY原則",對於開發效率的提高,協助非常大。
FreeMarker之根據模板產生Java代碼