Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grails 日期(月份和年份)的命名查询_Grails_Date_Named Query - Fatal编程技术网

Grails 日期(月份和年份)的命名查询

Grails 日期(月份和年份)的命名查询,grails,date,named-query,Grails,Date,Named Query,我有以下域类: class Car{ int hp Date registrationDate } 现在我要做的是创建一个命名查询,使我能够执行以下操作: Car.getCarsByYear(2011) 及 我不确定第二个是否可行,或者唯一的方法是使用两个命名查询,一个用于年,一个用于月 如何在命名查询中指定日期的月/年?我做了一些疯狂的拍摄,并尝试用谷歌搜索,但没有结果。您可以将参数传递给您的namedquery: static namedQueries = { g

我有以下域类:

class Car{

 int hp
 Date registrationDate

}
现在我要做的是创建一个命名查询,使我能够执行以下操作:

Car.getCarsByYear(2011)

我不确定第二个是否可行,或者唯一的方法是使用两个命名查询,一个用于年,一个用于月


如何在命名查询中指定日期的月/年?我做了一些疯狂的拍摄,并尝试用谷歌搜索,但没有结果。

您可以将参数传递给您的namedquery:

    static namedQueries = {
    getCarsByYearMonth { int year, int month ->
        println "year:"+year
        println  "month:"+month
        def now = Date.parse("yyyy-MM", "${year}-${month}")
        eq 'registrationDate', now
    }
}
这样称呼它:

 def search() {
    render Car.getCarsByYearMonth(2012,2).list()
}

注意:您可能希望使用marko问题中的字段,
registrationDate
,而不是通用的
日期,因为这有点让人费解。我假设他想要的是月/年等于参数,不大于
 def search() {
    render Car.getCarsByYearMonth(2012,2).list()
}