0

嗨,我如何将数据库存储的 GString 定义用于动态生成的数据。如果在代码中定义了格式,我可以使用 GString 来选择行属性

code_format = "${-> row.ACCOUNT} ${-> row.ACCOUNT_OWNER}" 

但是,如果从数据库中提取相同的定义,我的代码将无法正常工作。

Sql sql = Sql.newInstance(url, login, password, driver);

sql.eachRow(SQL) { row ->
    code_format = "${-> row.ACCOUNT} ${-> row.ACCOUNT_OWNER}" 
    database_format = "${-> row.REPORT_ATTRIBUTES}"

    println "1-  " + code_format
    println "2-  " + database_format         
    println "CODE : " + code_format.dump()
    println "DB   : " + database_format.dump()

}   

当我运行此代码时,我得到以下输出;

1-  FlowerHouse Joe
2-  ${-> row.ACCOUNT} ${-> row.ACCOUNT_OWNER}
CODE : <org.codehaus.groovy.runtime.GStringImpl@463cf024 strings=[,  , ] values=[GString$_run_closure1_closure2@44f289ee, GString$_run_closure1_closure3@f3d8b9f]>
DB   : org.codehaus.groovy.runtime.GStringImpl@4f5e9da9 strings=[, ] values=[GString$_run_closure1_closure4@11997b8a]
4

1 回答 1

1

row.REPORT_ATTRIBUTES返回 String 因为数据库不知道 groovy stings 格式。

GString 是模板,可以从字符串创建。

因此,您可以执行以下操作:

def engine = new groovy.text.SimpleTemplateEngine()
println engine.createTemplate(row.REPORT_ATTRIBUTES).make([row:row]).toString()
于 2018-01-30T07:20:54.197 回答