关于mybatis处理Java.util.lang.date多一秒的问题

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

关于mybatis处理Java.util.lang.date多一秒的问题

liqiankun508
各位好:
现在我在Java程序中设置了一个date对象,通过mybatisinsert进去到MySQL,但是很多时候会出现MySQL中的时间比在Java中设置的时间多一秒,如Tue May 23 22:55:47 CST 2017 进入数据库变成Tue May 23 22:55:48 CST 2017
我现在是在同一个事务中去做一个save,然后就进行查询,根据save返回的结果数据,其中有这个时间字段,由于多一秒,所以查询是败。这个问题不是每次都出现

我想问下,这个有可能是什么原因?
非常感谢能回复我的问题!

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 关于mybatis处理Java.util.lang.date多一秒的问题

Martin Kuen
Hi,

this sounds like you have a column using mysql's e.g. DATETIME datatype. This datatype does not support milliseconds in mysql versions < 5.6. In newer mysql versions you may use milliseconds (and microseconds as well) if you explicitly define it. See https://dev.mysql.com/doc/refman/5.7/en/fractional-seconds.html

java.util.Date does support milliseconds. If you send a Date object to the database, which has milliseconds you'll find that the milliseconds part is *rounded*. So if you have in java Tue May 23 22:55:47.509 CST 2017 (i.e. 509 milliseconds), then this value is stored as Tue May 23 22:55:48.

To work around this issue you can 
(1) set the millisecond part of your date object to 0, before doing the insert
(2) define the e.g. DATETIME column as DATETIME(3) (if you use mysql 5.6 or greater). See the link above

Maybe this helps ...


Best Regards,

Martin


2017-05-23 17:06 GMT+02:00 <[hidden email]>:
各位好:
现在我在Java程序中设置了一个date对象,通过mybatisinsert进去到MySQL,但是很多时候会出现MySQL中的时间比在Java中设置的时间多一秒,如Tue May 23 22:55:47 CST 2017 进入数据库变成Tue May 23 22:55:48 CST 2017
我现在是在同一个事务中去做一个save,然后就进行查询,根据save返回的结果数据,其中有这个时间字段,由于多一秒,所以查询是败。这个问题不是每次都出现

我想问下,这个有可能是什么原因?
非常感谢能回复我的问题!

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: 关于mybatis处理Java.util.lang.date多一秒的问题

liqiankun508
是的,您说的很对,我现在尝试在insert之前将毫秒数置为0,ok了,没问题。尝试第二种方式,将MySQL(我的版本是5.6.16)中column的datetime类型改为datetime(3),save之后的时间也是不会变了,但是在查询的方法执行之后,仍然查询不到数据,看起来数据都是没有问题的。您有什么好的建议吗?

在 2017年5月24日星期三 UTC+8下午10:47:29,mk81写道:
Hi,

this sounds like you have a column using mysql's e.g. DATETIME datatype. This datatype does not support milliseconds in mysql versions < 5.6. In newer mysql versions you may use milliseconds (and microseconds as well) if you explicitly define it. See <a href="https://dev.mysql.com/doc/refman/5.7/en/fractional-seconds.html" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F5.7%2Fen%2Ffractional-seconds.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFqMHO9HYbYS9839rtUJ7DjG_eHBw&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F5.7%2Fen%2Ffractional-seconds.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFqMHO9HYbYS9839rtUJ7DjG_eHBw&#39;;return true;">https://dev.mysql.com/doc/refman/5.7/en/fractional-seconds.html

java.util.Date does support milliseconds. If you send a Date object to the database, which has milliseconds you'll find that the milliseconds part is *rounded*. So if you have in java Tue May 23 22:55:47.509 CST 2017 (i.e. 509 milliseconds), then this value is stored as Tue May 23 22:55:48.

To work around this issue you can 
(1) set the millisecond part of your date object to 0, before doing the insert
(2) define the e.g. DATETIME column as DATETIME(3) (if you use mysql 5.6 or greater). See the link above

Maybe this helps ...


Best Regards,

Martin


2017-05-23 17:06 GMT+02:00 <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="OBqOHMDSAQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">liqian...@...>:
各位好:
现在我在Java程序中设置了一个date对象,通过mybatisinsert进去到MySQL,但是很多时候会出现MySQL中的时间比在Java中设置的时间多一秒,如Tue May 23 22:55:47 CST 2017 进入数据库变成Tue May 23 22:55:48 CST 2017
我现在是在同一个事务中去做一个save,然后就进行查询,根据save返回的结果数据,其中有这个时间字段,由于多一秒,所以查询是败。这个问题不是每次都出现

我想问下,这个有可能是什么原因?
非常感谢能回复我的问题!

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="OBqOHMDSAQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">mybatis-user...@googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...