1 Application Scenarios
It is often necessary to create a form dynamically, or to add a new data template, which makes common Java solutions difficult because of the need to create tables dynamically at run time and to maintain table fields and even tables relationships dynamically.
2 Implementation Tools
Hibernate + Spring + Groovy +freemarker
Hibernate is very simple. responsible for creating database tables This prevents us from writing complex SQL and judgments ourselves.
Spring acts as a bridge to connect the links.
Groovy is a dynamic language that creates access to the database, or control layer code, from the template when the project is run.
Freamker can generate hibernate configuration files, as well as groovy code, based on templates that are well defined in advance.
3 Principle of implementation
First create a form and fromattribute two table relationships to One-to-many. The form table records the name, category, and even the CSS style information for the form when it is generated dynamically. Fromattribute Records form field information, such as name, category, and so on. Once you have the form and the information for the form item, you can create the database table.
Test code:
public void TestGenerator () {
form form = Formservice.getall (). get (0);
list<formattribute> List = Formattributeservice
. Getattributelistbyformid (Form.getid ());
form.setformattributelist (list);
dbgenerator dg = new Dbgenerator (form, dataSource);
Dg.generator ();
}
Dbgenerator
import java.io.IOException;
import Java.io.StringWriter;
import Java.io.Writer;
import java.sql.SQLException;
import Java.util.HashMap;
import Java.util.Map;
import java.util.Properties;
import Javax.sql.DataSource;
import Org.hibernate.tool.hbm2ddl.SchemaExport;
import Org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class Dbgenerator {
private DataSource DataSource;
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 dbgenerator (Form form, DataSource DataSource) {
this.form = form;
This.datasource = DataSource;
}
public void Generator () {
if (null = = Form.getformattributelist () | | | form.getformattributelist (). Size () = 0) {
return;
}
Template t;
try {
t = GetConfig ("/template"). GetTemplate ("HIBERNATE.FTL");
Writer out = new StringWriter ();
t.process (Getmapcontext (), out);
String XML = out.tostring ();
createtable (XML);
log.debug (XML);
} catch (IOException e) {
E.printstacktrace ();
} catch (Templateexception e) {
E.printstacktrace ();
}
}
@SuppressWarnings ("unchecked")
Map Getmapcontext () {
root.put ("entity", form);
return root;
}
public void CreateTable (String xml) {
org.hibernate.cfg.Configuration conf = new org.hibernate.cfg.Configuration ();
Conf.configure ("/hibernate/hibernate.cfg.xml");
Properties Extraproperties = new properties ();
extraproperties.put ("Hibernate.hbm2ddl.auto", "create");
conf.addproperties (extraproperties);
conf.addxml (XML);
Schemaexport Dbexport;
try {
dbexport = new Schemaexport (conf, datasource.getconnection ());
//Dbexport.setoutputfile (path);
Dbexport.create (False, True);
} catch (SQLException e) {
//TODO auto-generated catch block
E.printstacktrace ();
}
}
}
class Hibernategenerator {
}