之前写了个自动生成bean的东东
不过用起来有些麻烦
如果想要做复杂点的东西就完全靠不住拉,要写到死,改起来也不方便
于是打算用FreeMarker实现这类功能
不多说先上个demo
package com.auto.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;
public class CreateBeanByTemplate {
/**
* @param tempfile
* @param savepath
* @param m
* @return
*/
public static String writeTemplate(String tempfile,String savepath,Map m){
String s=null;
try {
Configuration c=new Configuration();
String tempfilepath = System.getProperty("user.dir")
+ "\\src\\com\\template";
c.setDirectoryForTemplateLoading(new File(tempfilepath));
freemarker.template.Template t=c.getTemplate(tempfile);
Writer out=new OutputStreamWriter(new FileOutputStream(new File(savepath)));
t.process(m, out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TemplateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return s;
}
/**
* 读取文件
* @param filename
* @return
*/
public ArrayList<String> readFile(String filename) {
ArrayList<String> line = new ArrayList<String>();
try {
File f = new File(filename);
RandomAccessFile rf = new RandomAccessFile(f, "r");
String temp;
while (rf.read() != -1) {
rf.seek(rf.getFilePointer() - 1);
temp = new String(rf.readLine().getBytes("8859_1"));
if(temp.trim().length()>1){
line.add(temp);
}
}
rf.close();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
/**
* @param BeanFile
* @return
*/
public Map getBeanMode(String BeanFile){
ArrayList<String> al=this.readFile(System.getProperty("user.dir")+ "\\beanDepict\\"+BeanFile);
Map m=new HashMap<String, String>();
m.put("class", BeanFile);
List properties = new ArrayList<HashMap<String,String>>();
for (String as : al) {
String [] s=as.split(" ");
Map e=new HashMap<String, String>();
e.put("type", s[1]);
e.put("name", s[0]);
e.put("notes1", s[2]);
if(s.length>4){
e.put("notes2", s[4]);
}
properties.add(e);
}
m.put("properties", properties);
return m;
}
public static void main(String args[]){
CreateBeanByTemplate t=new CreateBeanByTemplate();
String ParametFile="interfacename";
Map m=t.getBeanMode(ParametFile);
String path = System.getProperty("user.dir")
+ "\\src\\com\\business\\reqbean\\"+ParametFile+".java";
new CreateBeanByTemplate().writeTemplate("bean.ftl",path , m);
}
}
ftl模板如下
package com.business.reqbean;
/**FreeMaker Template自动生成代码*/
public class ${class} {
<#list properties as prop>
/**${prop.notes1}*/
/**${prop.notes2!""}*/
private ${prop.type} ${prop.name};
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>
}
是不是比之前的处理要方便点呢