[英]How to capture milliseconds in grails domain
I have a grails domain that stores data in MySQL. 我有一个grails域,用于在MySQL中存储数据。
In it I have a Date
property which creates a DateTime
field in the DB. 在其中我有一个
Date
属性,它在DB中创建一个DateTime
字段。 However, I also wish to capture the milliseconds in addition to the date. 但是,除了日期之外,我还希望捕获毫秒数。
Is there a way to capture milliseconds? 有没有办法捕获毫秒?
Example: 例:
Class MyClass {
Date dateCreated
}
stores data in MySQL as 2014-10-06 16:21:57
but I'd like to capture milliseconds as well. 在MySQL中存储数据为
2014-10-06 16:21:57
但我也想捕获几毫秒。
MySQL 5.6.4 and up can save milliseconds in the database for datetime objects by defining the amount of fractional points for the object. 通过定义对象的小数点数,MySQL 5.6.4及更高版本可以在数据库中为datetime对象节省毫秒数。 (Read more here: MySQL reference manual ).
(在这里阅读更多: MySQL参考手册 )。 By default Grails/Gorm doesn't do this.
默认情况下,Grails / Gorm不会这样做。 Fortunately, this is easily solved by defining a sqlType in the domain like this:
幸运的是,通过在域中定义sqlType可以很容易地解决这个问题,如下所示:
class ExampleDomain {
Date date
static mapping = {
date sqlType: 'datetime(3)'
}
}
You could use bigint(20). 你可以使用bigint(20)。 Your groovy/grails domain could look like that if you want to store the nanoseconds since unix epoch automatically when a instance will be saved:
如果要在保存实例时自动存储unix epoch,那么你的groovy / grails域可能看起来像那样:
class myClass {
Integer id
Long timestamp = System.nanoTime()
static mapping = {
timestamp sqlType: "bigint"
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.