freemarker 模板生成 文件

2024-09-05 23:38
文章标签 模板 生成 freemarker

本文主要是介绍freemarker 模板生成 文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近公司比较忙,好久没写文章了,今天来一篇。

要生成的目标java文件

 

package com.cs.qdog.swift.objects;  public class F32B {  private Double amount;  private String currency;  public Double getAmount() {  return amount;  }  public void setAmount(Double amount) {  this.amount = amount;  }  public String getCurrency() {  return currency;  }  public void setCurrency(String currency) {  this.currency = currency;  }  
}  

 

 

 

模板文件SwiftFieldClass.txt(可以是任意格式的文件)

 

package com.cs.qdog.swift.objects;  public class ${class} {  <#list properties as prop>  private ${prop.type} ${prop.name};  </#list>  <#list properties as prop>  public ${prop.type} get${prop.name?cap_first}(){  return ${prop.name};  }  public void set${prop.name?cap_first}(${prop.type} ${prop.name}){  this.${prop.name} = ${prop.name};  }  </#list>  }  


java 代码

 

 

    package com.cs.qdog.swift.objects;  import java.io.File;  import java.io.IOException;  import java.io.OutputStreamWriter;  import java.io.Writer;  import java.util.Collection;  import java.util.HashMap;  import java.util.HashSet;  import java.util.Map;  import freemarker.template.Configuration;  import freemarker.template.DefaultObjectWrapper;  import freemarker.template.Template;  import freemarker.template.TemplateException;  public class GenObjects {  public static void main(String[] args) throws IOException,  TemplateException {  /* ------------------------------------------------------------------- */  /* You usually do it only once in the whole application life-cycle: */  /* Create and adjust the configuration */  Configuration cfg = new Configuration();  cfg.setDirectoryForTemplateLoading(new File(  "D:/Temp/EclipseWorkSpace/GenSwiftFields/templates"));  //模板父路径cfg.setObjectWrapper(new DefaultObjectWrapper());  /* ------------------------------------------------------------------- */  /* You usually do these for many times in the application life-cycle: */  /* Get or create a template */  Template temp = cfg.getTemplate("SwiftFieldClass.");  //模板文件,相对于setDirectoryForTemplateLoading设置的路径/* Create a data-model */  Map<String, Object> root = new HashMap<String, Object>();  //注意必须有一个根结点 data-modelroot.put("class", "F32B");  Collection<Map<String, String>> properties = new HashSet<Map<String, String>>();  root.put("properties", properties);  /* subfield 1: currency */  Map<String, String> currency = new HashMap<String, String>();  currency.put("name", "currency");  currency.put("type", "String");  properties.add(currency);  /* subfield 2: amount */  Map<String, String> amount = new HashMap<String, String>();  amount.put("name", "amount");  amount.put("type", "Double");  properties.add(amount);  /* Merge data-model with template */  String targetPath="***";//生成的目标文件的父目录String targetFile="****";//生成目标文件的名字if (!targetPath.exists())targetPath.mkdirs();File targetFile = new File(targetBasePath,targetFileName);if (!targetFile.exists())targetFile.createNewFile();Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), "UTF-8"));  temp.process(root, out);  out.flush();  }  }  

 

 

 

 

 

这篇关于freemarker 模板生成 文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1140390

相关文章

Python实现自动化Word文档样式复制与内容生成

《Python实现自动化Word文档样式复制与内容生成》在办公自动化领域,高效处理Word文档的样式和内容复制是一个常见需求,本文将展示如何利用Python的python-docx库实现... 目录一、为什么需要自动化 Word 文档处理二、核心功能实现:样式与表格的深度复制1. 表格复制(含样式与内容)2

python如何生成指定文件大小

《python如何生成指定文件大小》:本文主要介绍python如何生成指定文件大小的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python生成指定文件大小方法一(速度最快)方法二(中等速度)方法三(生成可读文本文件–较慢)方法四(使用内存映射高效生成

Maven项目中集成数据库文档生成工具的操作步骤

《Maven项目中集成数据库文档生成工具的操作步骤》在Maven项目中,可以通过集成数据库文档生成工具来自动生成数据库文档,本文为大家整理了使用screw-maven-plugin(推荐)的完... 目录1. 添加插件配置到 pom.XML2. 配置数据库信息3. 执行生成命令4. 高级配置选项5. 注意事

MybatisX快速生成增删改查的方法示例

《MybatisX快速生成增删改查的方法示例》MybatisX是基于IDEA的MyBatis/MyBatis-Plus开发插件,本文主要介绍了MybatisX快速生成增删改查的方法示例,文中通过示例代... 目录1 安装2 基本功能2.1 XML跳转2.2 代码生成2.2.1 生成.xml中的sql语句头2

Java如何根据word模板导出数据

《Java如何根据word模板导出数据》这篇文章主要为大家详细介绍了Java如何实现根据word模板导出数据,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... pom.XML文件导入依赖 <dependency> <groupId>cn.afterturn</groupId>

使用Python自动化生成PPT并结合LLM生成内容的代码解析

《使用Python自动化生成PPT并结合LLM生成内容的代码解析》PowerPoint是常用的文档工具,但手动设计和排版耗时耗力,本文将展示如何通过Python自动化提取PPT样式并生成新PPT,同时... 目录核心代码解析1. 提取 PPT 样式到 jsON关键步骤:代码片段:2. 应用 JSON 样式到

SpringBoot实现二维码生成的详细步骤与完整代码

《SpringBoot实现二维码生成的详细步骤与完整代码》如今,二维码的应用场景非常广泛,从支付到信息分享,二维码都扮演着重要角色,SpringBoot是一个非常流行的Java基于Spring框架的微... 目录一、环境搭建二、创建 Spring Boot 项目三、引入二维码生成依赖四、编写二维码生成代码五

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

PyQt5+Python-docx实现一键生成测试报告

《PyQt5+Python-docx实现一键生成测试报告》作为一名测试工程师,你是否经历过手动填写测试报告的痛苦,本文将用Python的PyQt5和python-docx库,打造一款测试报告一键生成工... 目录引言工具功能亮点工具设计思路1. 界面设计:PyQt5实现数据输入2. 文档生成:python-

Python中Flask模板的使用与高级技巧详解

《Python中Flask模板的使用与高级技巧详解》在Web开发中,直接将HTML代码写在Python文件中会导致诸多问题,Flask内置了Jinja2模板引擎,完美解决了这些问题,下面我们就来看看F... 目录一、模板渲染基础1.1 为什么需要模板引擎1.2 第一个模板渲染示例1.3 模板渲染原理二、模板