日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

freemarker 生成 Java 代码

發布時間:2024/8/26 java 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 freemarker 生成 Java 代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、導入maven依賴?

<project xmlns=" xmlns:xsi=" xsi:schemaLocation="

? <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};

//java項目www fhadmin org

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;

//java項目www fhadmin org

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 生成 Java 代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。