FreeMarker的代码生成方法

1、设置模板文件

public class ${className!}{

<#list classInfo as x>
    /**
    *  ${x.common!}
    */
    private ${x.dateType!} ${x.colName};
</#list>

<#list classInfo as x>
    /**
    *  get ${x.common!}
    */
    public ${x.dateType!} get${x.colName?cap_first}(){
        return this.${x.colName} ;
    }
    /**
    *  set  ${x.common!}
    */
    public void set${x.colName?cap_first}(${x.dateType!} ${x.colName}){
        this.${x.colName} = ${x.colName};
    }
</#list>
}

2、字段定义辅助类

package com.code.template;

public class BeanInfos {
	private String dateType;
	
	private String colName;
	
	private String importClass;

	private String common;
	
	
	
	public BeanInfos(String dateType, String colName, String importClass,String common) {
		super();
		this.dateType = dateType;
		this.colName = colName;
		this.importClass = importClass;
		this.common = common ;
	}

	public String getDateType() {
		return dateType;
	}

	public void setDateType(String dateType) {
		this.dateType = dateType;
	}

	public String getColName() {
		return colName;
	}

	public void setColName(String colName) {
		this.colName = colName;
	}

	public String getImportClass() {
		return importClass;
	}

	public void setImportClass(String importClass) {
		this.importClass = importClass;
	}

	public String getCommon() {
		return common;
	}

	public void setCommon(String common) {
		this.common = common;
	}
	
	
}

3、代码生成方法

package com.code.template;

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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


public class FreeMarkerTemplate {
	public static void main(String[] args) throws Exception {
		Configuration config = new Configuration();
		config.setDirectoryForTemplateLoading(new File("/home/res"));
		Template template = config.getTemplate("pojo.properties");
		Map<String, Object> mapsMap = new HashMap<String, Object>();
		mapsMap.put("className", "Test");
		List<BeanInfos> listsList = new ArrayList<BeanInfos>();
	
		listsList.add(new BeanInfos("String", "userName", "java.lang.String","用户名"));
		listsList.add(new BeanInfos("String", "passWord", "java.lang.String","密码"));
		listsList.add(new BeanInfos("String", "userRole", "java.lang.String","角色"));
		mapsMap.put("classInfo", listsList);
		
		PrintWriter pw = new PrintWriter(System.out);
		template.process(mapsMap, pw);
	}
}

4、生成结果:

public class Test{

    /**
    *  用户名
    */
    private String userName;
    /**
    *  密码
    */
    private String passWord;
    /**
    *  角色
    */
    private String userRole;

    /**
    *  get 用户名
    */
    public String getUserName(){
        return this.userName ;
    }
    /**
    *  set  用户名
    */
    public void setUserName(String userName){
        this.userName = userName;
    }
    /**
    *  get 密码
    */
    public String getPassWord(){
        return this.passWord ;
    }
    /**
    *  set  密码
    */
    public void setPassWord(String passWord){
        this.passWord = passWord;
    }
    /**
    *  get 角色
    */
    public String getUserRole(){
        return this.userRole ;
    }
    /**
    *  set  角色
    */
    public void setUserRole(String userRole){
        this.userRole = userRole;
    }
}

import 和package信息没有整理。也没有使用数据库...

你可能感兴趣的:(FreeMarker的代码生成方法)