简体   繁体   English

关于 grails “createCriteria”的新手问题

[英]Newbie question on grails “createCriteria”

I am new to Grails criteria builder, can someone please explain what does the following mean?我是 Grails 标准构建器的新手,有人可以解释以下是什么意思吗?

def c = Account.createCriteria()
         def results = c {

             like("holderFirstName", "Fred%")
             and {
                 between("balance", 500, 1000)
                 eq("branch", "London")
             }
             maxResults(10)
             order("holderLastName", "desc")
         }

Does it mean是不是意味着

  • Select * from account where holderFirstName like 'fred%' and (balance between 500 and 1000 **and** branch='london ') Select * from account where holderFirstName like 'fred%' and (balance between 500 and 1000 **and** branch='london ')
  • Select * from account where holderFirstName like 'fred%' and (balance between 500 and 1000 **or** branch='london ') Select * from account where holderFirstName like 'fred%' and (balance between 500 and 1000 **or** branch='london ')

If i want to use "or" and "and" together how do I do that?如果我想同时使用“或”和“和”,我该怎么做?

Your example would execute as:您的示例将执行为:

select * from account 
where holderFirstName like 'Fred%' 
and balance between 500 and 1000 
and branch = 'London'

All top level conditions are implied to be AND'ed together.所有顶级条件都被暗示为“与”在一起。 You could create the same Criteria as:您可以创建与以下相同的条件:

def c = Account.createCriteria()
def results = c {
    like("holderFirstName", "Fred%")
    between("balance", 500, 1000)
    eq("branch", "London")
    maxResults(10)
    order("holderLastName", "desc")
}

To get your second query use this Criteria:要获得第二个查询,请使用此标准:

def c = Account.createCriteria()
def results = c {
    like("holderFirstName", "Fred%")
    or {
        between("balance", 500, 1000)
        eq("branch", London")
    }
    maxResults(10)
    order("holderLastName", "desc")
}

Nest your and / or closures for more complex Criteria.嵌套您的and / or闭包以获得更复杂的标准。

Your current criteria means "and" in the two conditions of balance and branch.您当前的标准在平衡和分支两个条件中意味着“和”。 so所以

Select * from account where holderFirstName like 'fred%' and (balance between 500 and 1000 and branch='london') is correct, just that it will hold maximum 10 results and will be sorted on the basis of "holderLastName" in descending order. Select * from account where holderFirstName like 'fred%' and (balance between 500 and 1000 and branch='london') 是正确的,只是它将容纳最多 10 个结果,并将根据 "holderLastName" 降序排序.

To use and and or together you also need to specify an or block in criteria, so your criteria will look something like要同时使用andor ,您还需要在标准中指定or块,因此您的标准看起来像

Account.createCriteria()
         def results = c {

             like("holderFirstName", "Fred%")
             and {
                 between("balance", 500, 1000)
                 eq("branch", "London")
             }
             or{
                 eq("prop1", prop1)
                 eq("prop2",prop2)
             }
             maxResults(10)
             order("holderLastName", "desc")
         }

in this criteria there is an and between balance and branch conditions.在此标准中,平衡条件和分支条件之间存在and and also an or between prop1 and prop2也是 prop1 和 prop2 之间的or

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM