我想抽象出這個頂部的代碼塊看起來像底部的代碼塊。如何創建一個返回grails標準方法的閉包的自定義函數
if (params.xId) {
and {
'in'(aggregateClassReference, hierarchy['x'])
eq(aggregateIdReference, params.xId as Long)
}
}
if (params.yId) {
and {
'in'(aggregateReference, hierarchy['y'])
eq(aggregateIdReference, params.yId as Long)
}
}
...
if (params.xId) { belongsToHierarchy('x', params.xId as Long) }
if (params.yId) { belongsToHierarchy('y', params.yId as Long) }
我使用GORM標準查詢,但我不希望這些代碼大塊。有沒有辦法在自定義函數中返回這些條件查詢的關閉?現在的問題是我把代碼以下塊在
def criteria = DetachedCriteria.build(...)
後來我做了
criteria.list(...)
執行。在某種程度上返回一個封閉的結構,但是我還沒有弄清楚這個問題。 Grails有點新鮮。任何洞察力指導我將不勝感激:)
也許命名查詢可以幫助?你看過他們嗎? http://docs.grails.org/latest/ref/Domain%20Classes/namedQueries.html –