使用freemarker生成javabean

1、官网下载jar包 maven中配置的jar包

<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.27-incubating</version>
</dependency>

2、编写freemarker工具类

FreeMarkerInit.java

package com.freemarker.utils;

import java.io.File;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

public class FreeMarkerInit {
    
	private static FreeMarkerInit single= new FreeMarkerInit();; 
	private FreeMarkerInit() {}  
	//静态工厂方法   
    public static FreeMarkerInit getInstance() {  
        return single;  
    } 
    
    public Template getDefinedTemplate(String templateName) throws Exception{
    	 Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
         cfg.setDirectoryForTemplateLoading(new File("D:/freemarker/freemarker/src/main/resources/template"));   
         cfg.setDefaultEncoding("UTF-8");
         cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
         return cfg.getTemplate(templateName);
    }
}
2、编写一个实体 Attribute.java

package com.freemarker.utils;

public class Attribute {
    
	private String type;
	private String name;
	public Attribute(String type, String name) {
		this.type = type;
		this.name = name;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}
3、编写javabean模板javabean.ftl

package ${packageName};

public class ${className}{

	  <#-- 循环类型及属性 -->
	  <#list attrs as attr>
	  private ${attr.type} ${attr.name}
	  </#list>
	  
	  <#-- 循环生成set get方法 -->
	  <#list attrs as attr>
	  public void set${attr.name}(${attr.type} ${attr.name}) {
		this.${attr.name} = ${attr.name};
	  }
	  public ${attr.type} get${attr.name}() {
		return ${attr.name};
	  }
      </#list>
}
4、编写测试

package com.freemarker.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.freemarker.node.reportNode;
import com.freemarker.node.test;
import com.freemarker.utils.Attribute;
import com.freemarker.utils.FreeMarkerInit;

import freemarker.template.Template;

public class TemplateTest {

	//生成bean
	public void gen1() throws Exception{
		//获取模板
		Template temp = FreeMarkerInit.getInstance().getDefinedTemplate("javabean.ftl");
		//设置根值
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("packageName", "com.freemarker.bean");
		root.put("className", "Person");
		List<Attribute> attr_list = new ArrayList<Attribute>();
		attr_list.add(new Attribute("Long", "id"));
        attr_list.add(new Attribute("String", "name"));
        attr_list.add(new Attribute("int", "age"));
        root.put("attrs", attr_list);
        
        OutputStream fos = new  FileOutputStream( new File("D:/bean", "Person.java"));
        Writer out = new OutputStreamWriter(fos);
        temp.process(root, out);
        fos.flush();  
        fos.close();
	}
	
	
	
	public static void main(String[] args) {
		TemplateTest test = new TemplateTest();
		try {
			test.gen1();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
4、生成代码截图







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值