Chinaunix首页 | 论坛 | 博客
  • 博客访问: 614275
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-04-23 10:30:02

一、导入maven依赖 

       

点击(此处)折叠或打开

  1. <dependency>
  2.              <groupId>org.freemarker</groupId>
  3.              <artifactId>freemarker</artifactId>
  4.              <version>2.3.23</version>
  5.          </dependency>



二、新建com.freemarker.hello.templates包,并在该包下编写模板文件test.ftl


点击(此处)折叠或打开

  1. package ${classPath};
  2. public class ${className} {

  3.    private Integer ${Id};

  4.    private String ${userName};

  5.    private String ${password};


  6.     public Integer get${Id}(){

  7.         return ${Id};

  8.     }


  9.     public void set${Id}(Integer ${Id}){

  10.         this.${Id}=${Id};

  11.     }


  12.     public String get${userName}(){

  13.         return ${userName};

  14.     }

  15.     public void set${userName}(String ${userName}){

  16.         this.${userName}=${userName};

  17.     }


  18.     public String get${password}(){

  19.         return ${password};

  20.     }


  21.     public void set${password}(String ${password}){

  22.         this.${password}=${password};

  23.     }

  24. }


三、编写运行生成对应Java代码类


点击(此处)折叠或打开

  1. package com.freemark.hello;



  2. import java.io.BufferedWriter;

  3. import java.io.File;

  4. import java.io.FileOutputStream;

  5. import java.io.OutputStreamWriter;

  6. import java.io.Writer;

  7. import java.util.HashMap;

  8. import java.util.Map;



  9. import freemarker.template.Configuration;

  10. import freemarker.template.Template;



  11. //java项目www fhadmin org

  12. public class FreemarkerDemo {



  13.     private static final String TEMPLATE_PATH = "src/main/java/com/freemark/hello/templates";

  14.     private static final String CLASS_PATH = "src/main/java/com/freemark/hello";


  15.     public static void main(String[] args) {

  16.         // step1 创建freeMarker配置实例

  17.         Configuration configuration = new Configuration();

  18.         Writer out = null;

  19.         try {

  20.             // step2 获取模版路径

  21.             configuration.setDirectoryForTemplateLoading(new File(TEMPLATE_PATH));

  22.             // step3 创建数据模型

  23.             Map dataMap = new HashMap();

  24.             dataMap.put("classPath", "com.freemark.hello");

  25.             dataMap.put("className", "User");

  26.             dataMap.put("Id", "Id");

  27.             dataMap.put("userName", "userName");

  28.             dataMap.put("password","password");

  29.             // step4 加载模版文件

  30.             Template template = configuration.getTemplate("test.ftl");

  31.             // step5 生成数据

  32.             File docFile = new File(CLASS_PATH + "\\" + "User.java");

  33.             out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));

  34.             // step6 输出文件

  35.             template.process(dataMap, out);

  36.             System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^User.java 文件创建成功 !");

  37.         } catch (Exception e) {

  38.             e.printStackTrace();

  39.         } finally {

  40.             try {

  41.                 if (null != out) {

  42.                     out.flush();

  43.                 }

  44.             } catch (Exception e2) {

  45.                 e2.printStackTrace();

  46.             }

  47.         }

  48.     }


  49. }


四、步骤三成功,刷新(refresh)项目即可,看到com.freemark.hello有一个User类。


阅读(1856) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~