Grails中,我可以执行以下操作来查询数据库:Grails的FindWhere和BetweenDate
...
channel.findAllByTitle("CNN")
..
and
channel.findAllByDateBetween(new Date(), new Date() + 2)
..
,但我怎样才能在findWhere方法结合了?
channel.findAllWhere(title: "CNN", ...)
Grails中,我可以执行以下操作来查询数据库:Grails的FindWhere和BetweenDate
...
channel.findAllByTitle("CNN")
..
and
channel.findAllByDateBetween(new Date(), new Date() + 2)
..
,但我怎样才能在findWhere方法结合了?
channel.findAllWhere(title: "CNN", ...)
我不认为你可以做到这一点一个动态取景器,但你可以用这样的标准查询来做到这一点
Channel.withCriteria {
eq('title', 'CNN')
'between'('date', new Date(), new Date() +1)
}
我不是在我的工作站,(并不能确认这是正确的),但是从文档,它看起来像你应该能够通过使用布尔“与”为你动态的一部分来完成这个发现者。
检查此页面的底部:http://grails.org/doc/1.3.7/ref/Domain%20Classes/findAllBy.html 这里,在 '布尔逻辑':http://grails.org/doc/latest/guide/single.html#5.4.1 Dynamic Finders
像这样的东西可能工作:
channel.findAllByTitleAndDateBetween("CNN", new Date(), new Date() + 2)
否则,审核规定应该做你想要什么。我注意到你提到想使用findAllWhere(而不是findAllBy),但我假设你关心的是结果集。随时纠正我。
假设你正在使用Grails 2(目前为RC3),你可以尝试一些新'where' method:
def titleQuery = channel.where {
title = "CNN"
}
Channel channel = titleQuery.find()
或
def betweenQuery = channel.where {
(date >= new Date() && date <= new Date() + 2)
}
def results = betweenQuery.list(max: 10)