[英]Grails include closure in createCriteria
I have a grails application in which I now need to do a search. 我有一个grails应用程序,我现在需要进行搜索。 I have created a closure which contains the common conditions. 我创建了一个包含常见条件的闭包。
def criteria = {
if(pageSize){
delegate.maxResults( pageSize )
}
if(offset){
delegate.firstResult( offset )
}
........
}
And then i want to use this inside create criteria alone with some specific conditions 然后我想在一些特定条件下单独使用内部创建标准
objects = DomainObkect.createCriteria().list {
ne "booleanCheck", true
eq "value", value
criteria
}
Is there a way for this to take effect? 有没有办法让这个生效? Now the criteria options are not being executed 现在没有执行标准选项
Thanks in Advance 提前致谢
Sure. 当然。 Something like: 就像是:
Closure searchRestriction = { builder ->
if(pageSize){
builder.maxResults( pageSize )
}
if(offset){
builder.firstResult( offset )
}
}
def criteria = DomainObkect.createCriteria()
objects = criteria.list {
ne "booleanCheck", true
eq "value", value
searchRestriction(criteria)
}
This question is asked earlier as well Reusing part of Grails criteria closure 之前也会问这个问题, 重复使用Grails标准的一部分
You can use namedqueries and you delegate criterias as well. 您可以使用namedqueries并委派标准。 Some of the blogs that you can check http://www.tothenew.com/blog/grails-data-binding-criteria-reuse/ http://www.tothenew.com/blog/reusing-grails-criteria-for-multiple-domains-using-closure-delegate/ 您可以查看一些博客http://www.tothenew.com/blog/grails-data-binding-criteria-reuse/ http://www.tothenew.com/blog/reusing-grails-criteria-for-多域-使用-闭合代表/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.