我想抽象出这个顶部的代码块看起来像底部的代码块。如何创建一个返回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 –