項目中往往需要動態的創建一個表單
Hibernate + Spring + Groovy +Freemarker
Hibernate 作用很簡單負責創建數據庫表這樣可以避免我們自己去寫復雜的sql和判斷
Spring 作為橋梁起到連接紐帶的作用
Groovy做為動態語言
Freamker 可以根據提前定義好的模板生成 hibernate配置文件
首先創建Form 和 FromAttribute 兩張表關系一對多
測試代碼
public void testGenerator(){
Form form = formService
List list = formAttributeService
}
DbGenerator
import java
import javax
import org
import freemarker
public class DbGenerator {
private DataSource dataSource
protected Map root = new HashMap()
private static Logger log = LoggerFactory
protected String path
protected String packageName
private Form form
protected Configuration getConfig(String resource){
Configuration cfg = new Configuration()
cfg
return cfg
}
public DbGenerator(Form form
}
public void generator(){
if(null == form
return
}
Template t
try {
t = getConfig(
Writer out = new StringWriter()
t
createTable(xml)
log
} catch(IOException e){
e
} catch(TemplateException e){
e
}
@SuppressWarnings(
Map getMapContext(){
root
return root
}
public void createTable(String xml){
org
Properties extraProperties = new Properties()
extraProperties
conf
SchemaExport dbExport
try {
dbExport = new SchemaExport(conf
} catch(SQLException e){
// TODO Auto
e
}
}
class hibernateGenerator {
}hibernate
hibernate
org
net
jdbc
sa
sa
true
update
——>
創建好數據庫後就要利用groovy動態創建訪問代碼了
public void testGroovy(){
Form form = formService
List list = formAttributeService
FormGenerator fg = new FormGenerator(form)
String groovycode = fg
GroovyClassLoader loader = new GroovyClassLoader(parent)
Class groovyClass = loader
GroovyObject groovyObject = null
try {
groovyObject = (GroovyObject) groovyClass
} catch(InstantiationException e){
e
} catch(IllegalAccessException e){
e
}
// map中key為formAttribute中描述該表單字段在數據庫中的名稱c_columnName
//具體情況根據formAttribute而定
Map map = new HashMap()
map
//調用insert方法插入數據
int c = (Integer) groovyObject
//調用getAll方法獲得所有動態表中的數據
Object o = groovyObject
List list
Object obj = list
try {
String tname = (String) BeanUtils
} catch(IllegalAccessException e){
e
} catch(NoSuchFieldException e){
e
}
//調用search方法查詢動態表
List returnList = (List) groovyObject
for(Map map
//同理此處根據FromAttribute而定
System
}
}FormGenerator
public class FormGenerator {
protected Map root = new HashMap()
private static Logger log = LoggerFactory
protected String path
protected String packageName
private Form form
protected Configuration getConfig(String resource){
Configuration cfg = new Configuration()
cfg
return cfg
}
public FormGenerator(Form form){
this
}
public String generator(){
String returnstr = null
Template t
try {
t = getConfig(
Writer out = new StringWriter()
t
} catch(IOException e){
e
} catch(TemplateException e){
e
}
return returnstr
}
@SuppressWarnings(
Map getMapContext(){
root
root
root
return root
}
}FormService
class ${entity
def insert =
def delete =
def update =
def int insert(entity){
def Object[] params = [${insertParameter}]
def int[] types=[Types
}
def int update(entity){
def Object[] params = [${updateParameter}]
return DataSourceFactory
}
def int delete(String entityId){
def Object[] params =[entityId]
return DataSourceFactory
}
def search(entity){
${query}
println(query)
return DataSourceFactory
}
}
以上代碼示意了如何利用 freemarker 生成 Groovy 和 hibernate 相關代碼
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25599.html