博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用FreeMaker生成java代码
阅读量:6868 次
发布时间:2019-06-26

本文共 2830 字,大约阅读时间需要 9 分钟。

hot3.png

1)首先下载FreeMaker的jar包(下载后导入工程)

下载地址:(我使用的版本是2.3.26)

http://www.apache.org/dyn/closer.cgi/incubator/freemarker/engine/2.3.26-incubating/binaries/apache-freemarker-2.3.26-incubating-bin.tar.gz

maven依赖

org.freemarker
freemarker
2.3.26-incubating

2)测试项目目录结构

160756_EIPn_2437179.png

3)Person.ftl

package ${packageName}; /** *  <#if author == "xzw"> @author xzw 
*/public class ${className} { <#list attrs as a> private ${a.type} ${a.field};
<#list attrs as a> public void set${a.field?cap_first}(${a.type} ${a.field}){ this.${a.field} = ${a.field}; } public ${a.type} get${a.field?cap_first}(){ return this.${a.field}; }
}

 

4) Attr.java

package com.JobData;public class Attr{	public String field;	public String type;		public Attr(String field,  String type){		this.field = field;		this.type = type;	}		public String getField(){		return this.field;	}		public String getType(){		return this.type;	}		public void setField(String field){		this.field = field;	}		public void setType(String type){		this.type = type;	}}

 

5)CodeGenerator.java

package com.main;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.StringWriter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import com.JobData.Attr;import freemarker.template.Configuration;import freemarker.template.Template;import freemarker.template.TemplateException;public class CodeGenerator {		@SuppressWarnings("deprecation")	public static void main(String[] args){		List list = new ArrayList();		list.add(new Attr("username", "String"));		list.add(new Attr("password", "String"));		list.add(new Attr("age", "int"));		list.add(new Attr("text", "String"));				Map
root = new HashMap
(); root.put("packageName", "com.JobData"); root.put("className", "Person"); root.put("attrs", list); root.put("author", "xzw"); Configuration cfg = new Configuration(); /*path模板路径为编译类的路径*/ String path = "D:/WorkSpace/FreeMaker/bin/com/ftl"; try { cfg.setDirectoryForTemplateLoading(new File(path)); Template template = cfg.getTemplate("/Person.ftl"); /*判断该目录是否存在,不存在则新建该目录*/ File dir = new File("D:/WorkSpace/FreeMaker/src/com/java/com/JobData/"); if(!dir.exists()){ dir.mkdirs(); } OutputStream out = new FileOutputStream( new File(dir+"Person.java")); template.process(root, new OutputStreamWriter(out)); } catch (IOException e) { System.out.println(e.getMessage()); } catch (TemplateException e) { System.out.println(e.getMessage()); } }}

 

简单好用,适合很多重复的代码生成,降低出错率。

 

 

 

转载于:https://my.oschina.net/xiaozhiwen/blog/920305

你可能感兴趣的文章
程序员常用借口指南
查看>>
linux 中是否为integer 判断
查看>>
定时自动执行SQL存储过程(图文详解)
查看>>
R语言的包管理功能
查看>>
工厂模式
查看>>
JupyterHub on Kubernetes--定制用户环境
查看>>
sed
查看>>
不以成败论战略
查看>>
我的友情链接
查看>>
一个开源系统的架构分析
查看>>
系统基本功能
查看>>
der pem cer crt key pfx等概念及区别
查看>>
我的友情链接
查看>>
flume-ng 1.5.0安装部署
查看>>
http长连接与短连接
查看>>
数据库中快速备份一个表的数据,或者只备份表结构
查看>>
vue-cli
查看>>
文件处理
查看>>
代码调试包Infragistics Windows Forms Test Automation发布v16.1|附下载
查看>>
我的友情链接
查看>>