grailsgorm:选择为

grailsgorm:选择为,grails,gorm,Grails,Gorm,我试图获取所有今天出生于GORM的用户,但我无法在Grails中编写此查询: SELECT DAY(dateOfBirth) AS 'day', MONTH(dateOfBirth) AS 'month' FROM Users WHERE day = '...' AND month = '...'; …将替换为今天的值 最小用户域类 class User { Date dateOfBirth ... } @Transactional class UserService {

我试图获取所有今天出生于GORM的用户,但我无法在Grails中编写此查询:

SELECT
DAY(dateOfBirth) AS 'day',
MONTH(dateOfBirth) AS 'month'
FROM Users
WHERE day = '...' AND month = '...';
将替换为今天的值

最小
用户
域类

class User {

  Date dateOfBirth

  ...

}
@Transactional
class UserService {

  def getTodayBirthdays() {

    def bornTodayQuery = User.where{
      /* I'm thinking here I must
       * select the DAY and MONTH from the DB
       * and compare it with the today ones.
       */
    }       

    User usersBornToday = bornTodayQuery.findAll()      

    usersBornToday      

  }

}
最小
UserService
class

class User {

  Date dateOfBirth

  ...

}
@Transactional
class UserService {

  def getTodayBirthdays() {

    def bornTodayQuery = User.where{
      /* I'm thinking here I must
       * select the DAY and MONTH from the DB
       * and compare it with the today ones.
       */
    }       

    User usersBornToday = bornTodayQuery.findAll()      

    usersBornToday      

  }

}
有没有办法用GORM做一个别名(选择字段作为别名)

我正在使用:

  • Grails2.4.4

谢谢

您可以在服务中使用where查询

@Transactional(readOnly = true)
def listBirthday(int _month, int _day) {
  // Calendar.JANUARY equals to zero!
  def m = _month + 1
  // Run a where query on the users
  User.where {
    month(dateOfBirth) == m && day(dateOfBirth) == _day
  }
}

@Transactional(readOnly = true)
def listBirthdayToday() {
  def cal = Calendar.getInstance()
  listBirthday(cal.get(cal.MONTH), cal.get(cal.DAY_OF_MONTH))
}

除了
还有一些其他功能,文档(查找“其他功能”)是否回答了您的问题@有点像sebnukem,但我正在寻找一种不使用
executeQuery
的方法。“当然,如果它存在的话。我不认为你可以和戈姆在一起,但我可能错了…”塞布努金显然你可以,同意铍的回答。谢谢