动态表单及动态建表实现原理
1 应用场景项目中往往需要动态的创建一个表单,或者添加一个新的数据模板,这时候因为需要在运行时动态的创建表以及动态的维护表字段甚至表关系 使得普通java解决方案变得困难重重。
2 实现工具
Hibernate + Spring + Groovy +FreemarkerHibernate 作用很简单负责创建数据库表这样可以避免我们自己去写复杂的sql和判断。Spring 作为桥梁起到连接纽带的作用。Groovy做为动态语言,在项目运行时根据模板创建访问数据库,或者控制层代码。Freamker 可以根据提前定义好的模板生成 hibernate配置文件,以及Groovy代码。 3 实现原理
首先创建Form 和 FromAttribute 两张表关系一对多。Form表记录表单的名称,类别,甚至是作为在动态生成表单时的css样式信息。FromAttribute记录表单字段信息,如名称,类别等。有了表单以及表单项的信息后就可以创建数据库表了。
测试代码:
http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedBlock.gifpublic void testGenerator() http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif Form form = formService.getAll().get(0);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif List<FormAttribute> list = formAttributeService
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif .getAttributeListByFormId(form.getId());
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif form.setFormAttributeList(list);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif DbGenerator dg = new DbGenerator(form, dataSource);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif dg.generator();
http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif }
http://www.blogjava.net/Images/OutliningIndicators/None.gif
DbGenerator
http://www.blogjava.net/Images/OutliningIndicators/None.gif
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport java.io.IOException;
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport java.io.StringWriter;
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport java.io.Writer;
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport java.sql.SQLException;
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport java.util.HashMap;
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport java.util.Map;
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport java.util.Properties;
http://www.blogjava.net/Images/OutliningIndicators/None.gif
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport javax.sql.DataSource;
http://www.blogjava.net/Images/OutliningIndicators/None.gif
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport org.hibernate.tool.hbm2ddl.SchemaExport;
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport org.slf4j.Logger;
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport org.slf4j.LoggerFactory;
http://www.blogjava.net/Images/OutliningIndicators/None.gif
http://www.blogjava.net/Images/OutliningIndicators/None.gif
http://www.blogjava.net/Images/OutliningIndicators/None.gif
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport freemarker.template.Configuration;
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport freemarker.template.Template;
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport freemarker.template.TemplateException;
http://www.blogjava.net/Images/OutliningIndicators/None.gif
http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedBlock.gifpublic class DbGenerator http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif private DataSource dataSource;
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif protected Map root = new HashMap();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif private static Logger log = LoggerFactory.getLogger(FormGenerator.class);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif protected String path;
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif protected String packageName;
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif private Form form;
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif protected Configuration getConfig(String resource) http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif Configuration cfg = new Configuration();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif cfg.setDefaultEncoding("UTF-8");
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif cfg.setClassForTemplateLoading(this.getClass(), resource);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif return cfg;
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif public DbGenerator(Form form ,DataSource dataSource) http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif this.form = form;
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif this.dataSource = dataSource;
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif public void generator() http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif if(null == form.getFormAttributeList() || form.getFormAttributeList().size() == 0)http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif return ;
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif Template t;
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif try http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif t = getConfig("/template").getTemplate("hibernate.ftl");
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif Writer out = new StringWriter();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif t.process(getMapContext(), out);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif String xml = out.toString();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif createTable(xml);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif log.debug(xml);
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif } catch (IOException e) http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif e.printStackTrace();
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif } catch (TemplateException e) http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif e.printStackTrace();
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif @SuppressWarnings("unchecked")
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif Map getMapContext() http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif root.put("entity", form);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif return root;
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif public void createTable(String xml) http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif org.hibernate.cfg.Configuration conf = new org.hibernate.cfg.Configuration();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif conf.configure("/hibernate/hibernate.cfg.xml");
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif Properties extraProperties = new Properties();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif extraProperties.put("hibernate.hbm2ddl.auto", "create");
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif conf.addProperties(extraProperties);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif conf.addXML(xml);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif SchemaExport dbExport;
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif try http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif dbExport = new SchemaExport(conf, dataSource.getConnection());
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif // dbExport.setOutputFile(path);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif dbExport.create(false, true);
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif } catch (SQLException e) http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif // TODO Auto-generated catch block
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif e.printStackTrace();
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.blogjava.net/Images/OutliningIndicators/None.gif
http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedBlock.gifclass hibernateGenerator http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif}
hibernate.ftl
http://www.blogjava.net/Images/OutliningIndicators/None.gif<?xml version="1.0" encoding="UTF-8"?>
http://www.blogjava.net/Images/OutliningIndicators/None.gif
http://www.blogjava.net/Images/OutliningIndicators/None.gif<!DOCTYPE hibernate-mapping
http://www.blogjava.net/Images/OutliningIndicators/None.gifPUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
http://www.blogjava.net/Images/OutliningIndicators/None.gif "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
http://www.blogjava.net/Images/OutliningIndicators/None.gif
http://www.blogjava.net/Images/OutliningIndicators/None.gif<hibernate-mapping>
http://www.blogjava.net/Images/OutliningIndicators/None.gif <class
http://www.blogjava.net/Images/OutliningIndicators/None.gif name="${entity.name}"
http://www.blogjava.net/Images/OutliningIndicators/None.gif table="`${entity.tableName}`"
http://www.blogjava.net/Images/OutliningIndicators/None.gif dynamic-update="false"
http://www.blogjava.net/Images/OutliningIndicators/None.gif dynamic-insert="false"
http://www.blogjava.net/Images/OutliningIndicators/None.gif select-before-update="false"
http://www.blogjava.net/Images/OutliningIndicators/None.gif optimistic-lock="version">
http://www.blogjava.net/Images/OutliningIndicators/None.gif <id
http://www.blogjava.net/Images/OutliningIndicators/None.gif name="id"
http://www.blogjava.net/Images/OutliningIndicators/None.gif column="id"
http://www.blogjava.net/Images/OutliningIndicators/None.gif type="java.lang.String"
http://www.blogjava.net/Images/OutliningIndicators/None.gif unsaved-value="null">
http://www.blogjava.net/Images/OutliningIndicators/None.gif <generator class="uuid" />
http://www.blogjava.net/Images/OutliningIndicators/None.gif </id>
http://www.blogjava.net/Images/OutliningIndicators/None.gif <#if entity.formAttributeList?exists>
http://www.blogjava.net/Images/OutliningIndicators/None.gif <#list entity.formAttributeList as attr>
http://www.blogjava.net/Images/OutliningIndicators/None.gif <#if attr.name == "id">
http://www.blogjava.net/Images/OutliningIndicators/None.gif <#else>
http://www.blogjava.net/Images/OutliningIndicators/None.gif <property
http://www.blogjava.net/Images/OutliningIndicators/None.gif name="${attr.name}"
http://www.blogjava.net/Images/OutliningIndicators/None.gif type="java.lang.String"
http://www.blogjava.net/Images/OutliningIndicators/None.gif update="true"
http://www.blogjava.net/Images/OutliningIndicators/None.gif insert="true"
http://www.blogjava.net/Images/OutliningIndicators/None.gif access="property"
http://www.blogjava.net/Images/OutliningIndicators/None.gif column="`${attr.columnName}`"
http://www.blogjava.net/Images/OutliningIndicators/None.gif length="${attr.length}"
http://www.blogjava.net/Images/OutliningIndicators/None.gif not-null="false"
http://www.blogjava.net/Images/OutliningIndicators/None.gif unique="false"
http://www.blogjava.net/Images/OutliningIndicators/None.gif />
http://www.blogjava.net/Images/OutliningIndicators/None.gif
http://www.blogjava.net/Images/OutliningIndicators/None.gif </#if>
http://www.blogjava.net/Images/OutliningIndicators/None.gif </#list>
http://www.blogjava.net/Images/OutliningIndicators/None.gif </#if>
http://www.blogjava.net/Images/OutliningIndicators/None.gif
http://www.blogjava.net/Images/OutliningIndicators/None.gif </class>
http://www.blogjava.net/Images/OutliningIndicators/None.gif
http://www.blogjava.net/Images/OutliningIndicators/None.gif</hibernate-mapping>
http://www.blogjava.net/Images/OutliningIndicators/None.gif
hibernate.cfg.xml
http://www.blogjava.net/Images/OutliningIndicators/None.gif<!DOCTYPE hibernate-configuration
http://www.blogjava.net/Images/OutliningIndicators/None.gif PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
http://www.blogjava.net/Images/OutliningIndicators/None.gif "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
http://www.blogjava.net/Images/OutliningIndicators/None.gif
http://www.blogjava.net/Images/OutliningIndicators/None.gif<hibernate-configuration>
http://www.blogjava.net/Images/OutliningIndicators/None.gif<session-factory>
http://www.blogjava.net/Images/OutliningIndicators/None.gif <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
http://www.blogjava.net/Images/OutliningIndicators/None.gif <property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
http://www.blogjava.net/Images/OutliningIndicators/None.gif <property name="connection.url">jdbc:jtds:sqlserver://127.0.0.1:1433;databasename=struts;SelectMethod=cursor</property>
http://www.blogjava.net/Images/OutliningIndicators/None.gif <property name="connection.username">sa</property>
http://www.blogjava.net/Images/OutliningIndicators/None.gif <property name="connection.password">sa</property>
http://www.blogjava.net/Images/OutliningIndicators/None.gif
http://www.blogjava.net/Images/OutliningIndicators/None.gif <property name="show_sql">true</property>
http://www.blogjava.net/Images/OutliningIndicators/None.gif <property name="hibernate.hbm2ddl.auto">update</property>
http://www.blogjava.net/Images/OutliningIndicators/None.gif
http://www.blogjava.net/Images/OutliningIndicators/None.gif<!--
http://www.blogjava.net/Images/OutliningIndicators/None.gif <mapping resource="hibernate/FormAttribute.hbm.xml" />
http://www.blogjava.net/Images/OutliningIndicators/None.gif <mapping resource="hibernate/Form.hbm.xml" />
http://www.blogjava.net/Images/OutliningIndicators/None.gif -->
http://www.blogjava.net/Images/OutliningIndicators/None.gif</session-factory>
http://www.blogjava.net/Images/OutliningIndicators/None.gif
http://www.blogjava.net/Images/OutliningIndicators/None.gif</hibernate-configuration>
创建好数据库后 就要利用groovy动态创建访问代码了:先看测试代码 再看具体实现:
http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedBlock.gifpublic void testGroovy() http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif Form form = formService.get("1");
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif List<FormAttribute> list = formAttributeService
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif .getAttributeListByFormId(form.getId());
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif form.setFormAttributeList(list);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif FormGenerator fg = new FormGenerator(form);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif String groovycode = fg.generator();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif ClassLoader parent = getClass().getClassLoader();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif GroovyClassLoader loader = new GroovyClassLoader(parent);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif Class groovyClass = loader.parseClass(groovycode);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif GroovyObject groovyObject = null;
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif try http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif groovyObject = (GroovyObject) groovyClass.newInstance();
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif } catch (InstantiationException e) http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif e.printStackTrace();
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif } catch (IllegalAccessException e) http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif e.printStackTrace();
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif // map中key为formAttribute中描述该表单字段在数据库中的名称c_columnName
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif // 具体情况根据formAttribute而定
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif Map map = new HashMap();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif map.put("name", "limq");
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif // 调用insert方法插入数据
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif int c = (Integer) groovyObject.invokeMethod("insert", map);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif // 调用getAll方法获得所有动态表中的数据
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif Object o = groovyObject.invokeMethod("getAll", null);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif List list2 = (List) o;
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif Object obj = list2.get(0);
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif try http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif String tname = (String) BeanUtils.getDeclaredProperty(obj, "name");
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif System.out.println(tname);
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif } catch (IllegalAccessException e) http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif e.printStackTrace();
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif } catch (NoSuchFieldException e) http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif e.printStackTrace();
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif // 调用search方法查询动态表
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif List<Map> returnList = (List) groovyObject.invokeMethod("search", map);
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif for (Map map2 : returnList) http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif // 同理此处根据FromAttribute而定
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif System.out.println(map2.get("id"));
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif System.out.println(map2.get("name"));
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif System.out.println(map2.get("type"));
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif }
FormGenerator : 创建访问数据库Groovy代码
http://www.blogjava.net/Images/OutliningIndicators/None.gif
http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedBlock.gifpublic class FormGenerator http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif protectedMap root = new HashMap();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif private static Logger log = LoggerFactory.getLogger(FormGenerator.class);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif protected String path ;
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif protected String packageName ;
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif private Form form ;
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif protected Configuration getConfig(String resource) http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif Configuration cfg = new Configuration();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif cfg.setDefaultEncoding("UTF-8");
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif cfg.setClassForTemplateLoading(this.getClass(), resource);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif return cfg;
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif public FormGenerator(Form form)http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif this.form = form;
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif public String generator()http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif String returnstr = null;
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif Template t;
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif try http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif t = getConfig("/template").getTemplate("FormService.ftl");
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif //Writer out = new OutputStreamWriter(new FileOutputStream(new File(path)),"UTF-8");
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif Writer out = new StringWriter();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif t.process(getMapContext(), out);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif returnstr = out.toString();
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif log.debug(returnstr);
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif } catch (IOException e) http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif e.printStackTrace();
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif } catch (TemplateException e) http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif e.printStackTrace();
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif return returnstr;
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif @SuppressWarnings("unchecked")
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif Map getMapContext() http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif root.put("entity", form);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif root.put("insert", SqlHelper.buildInsertStatement(form));
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif root.put("update", SqlHelper.buildUpdateStatement(form));
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif root.put("insertParameter", SqlHelper.buildInsertparameter(form));
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif root.put("updateParameter", SqlHelper.buildUpdateparameter(form));
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif root.put("delete", SqlHelper.buildDeleteStatement(form));
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif root.put("query",SqlHelper.buildQueryStatement(form));
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif return root;
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif}
FormService.ftl
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport java.sql.ResultSet
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport java.sql.SQLException
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport java.sql.Types
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport org.springframework.jdbc.core.RowMapper
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport org.springframework.jdbc.core.RowMapperResultSetExtractor
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport com.glnpu.sige.core.dao.DataSourceFactory
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport org.apache.commons.lang.builder.ToStringBuilder;
http://www.blogjava.net/Images/OutliningIndicators/None.gifimport org.apache.commons.lang.builder.ToStringStyle;
http://www.blogjava.net/Images/OutliningIndicators/None.gif
http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedBlock.gifclass $http://www.blogjava.net/Images/dot.gif{entity.name?cap_first}Dao http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif def insert = '${insert}'
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif def delete = '${delete}'
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif def update = '${update}'
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif def int insert( entity)http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif def Object[] params = [$http://www.blogjava.net/Images/dot.gif{insertParameter}]
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif <#assign size = entity.formAttributeList?size/>
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif def int[] types=[<#list 1..size+1 as p>Types.VARCHAR,<#rt/></#list>]
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif return DataSourceFactory.getJdbcTemplate().update(insert, params, types)
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif def int update( entity)http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif def Object[] params = [$http://www.blogjava.net/Images/dot.gif{updateParameter}]
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif return DataSourceFactory.getJdbcTemplate().update(update, params)
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif def int delete(String entityId)http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif def Object[] params =
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif return DataSourceFactory.getJdbcTemplate().update(delete, params)
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif def search(entity)http://www.blogjava.net/Images/dot.gif{
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockStart.gifhttp://www.blogjava.net/Images/OutliningIndicators/ContractedSubBlock.gif $http://www.blogjava.net/Images/dot.gif{query}
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif println(query);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif return DataSourceFactory.getJdbcTemplate().queryForList(query);
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.blogjava.net/Images/OutliningIndicators/InBlock.gif
http://www.blogjava.net/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.blogjava.net/Images/OutliningIndicators/None.gif
以上代码示意了如何利用 freemarker 生成 Groovy 和 hibernate 相关代码,以及如何利用Groovy动态的对数据库进行创建和增删改查操作,了解以上的原理后就可以方便的在运行时利用freemarker生成表示层页面以及代码来进行展示。
http://www.blogjava.net/limq/archive/2009/09/19/295690.html
页:
[1]