将 ZonedDateTime 与 Grails 3 lastUpdated 和 dateCreated 字段一起使用

Using ZonedDateTime with Grails 3 lastUpdated and dateCreated fields

Grails 3 是否支持使用 class 除了 Date 之外的任何其他类型,例如 ZonedDateTime,用于 dateCreated 和 lastUpdated 字段?例如,

...
ZonedDateTime dateCreated
ZonedDateTime lastUpdated
...

我知道 MySQL 支持 ZonedDateTime,所以我很好奇 GORM 是否支持它。

自然地,没有。但是,如果您愿意,自己使用它们将是非常简单的。您可能希望首先关闭域对象上的自动时间戳:

static mapping = {
    autoTimestamp false
}

然后使用事件(beforeUpdate、beforeInsert)填充如下:

def beforeUpdate() {
    lastUpdated = // whatever you want it to be
} 

是的,确实如此。由于 Grails 3 仍然支持低至 java 7,因此您需要在 build.gradle 中添加一些依赖项以添加对它的支持:

// Hibernate java 8 java.time support 
compile "org.hibernate:hibernate-java8:$hibernateVersion" 
// Grails java 8 java.time support
compile "org.grails.plugins:grails-java8:1.2.3"

在此处将 java.time 与 Grails 一起使用时有很多好的提示:https://giri-tech.blogspot.com/2018/01/add-time-zone-sense.html