动态表单及动态建表实现原理
2009-09-22 00:00:00 来源:WEB开发网FormGenerator : 创建访问数据库Groovy代码
public class FormGenerator {
protected Map root = new HashMap();
private static Logger log = LoggerFactory.getLogger(FormGenerator.class);
protected String path ;
protected String packageName ;
private Form form ;
protected Configuration getConfig(String resource) {
Configuration cfg = new Configuration();
cfg.setDefaultEncoding("UTF-8");
cfg.setClassForTemplateLoading(this.getClass(), resource);
return cfg;
}
public FormGenerator(Form form){
this.form = form;
}
public String generator(){
String returnstr = null;
Template t;
try {
t = getConfig("/template").getTemplate("FormService.ftl");
//Writer out = new OutputStreamWriter(new FileOutputStream(new File(path)),"UTF-8");
Writer out = new StringWriter();
t.process(getMapContext(), out);
returnstr = out.toString();
log.debug(returnstr);
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
}
return returnstr;
}
@SuppressWarnings("unchecked")
Map getMapContext() {
root.put("entity", form);
root.put("insert", SqlHelper.buildInsertStatement(form));
root.put("update", SqlHelper.buildUpdateStatement(form));
root.put("insertParameter", SqlHelper.buildInsertparameter(form));
root.put("updateParameter", SqlHelper.buildUpdateparameter(form));
root.put("delete", SqlHelper.buildDeleteStatement(form));
root.put("query", SqlHelper.buildQueryStatement(form));
return root;
}
}
更多精彩
赞助商链接