简体   繁体   English

MySql和MyBatis日期时间小数秒

[英]MySql and MyBatis datetime fractional seconds

I use mysql 5.6.14. 我使用mysql 5.6.14。 Firsly I created 首先,我创建了

CREATE TABLE `TEST` (
  `id` tinyint(3) unsigned zerofill NOT NULL AUTO_INCREMENT,
  `dateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

From mysql documentation 从mysql文档

To define a column that includes a fractional seconds part, use the syntax type_name(fsp), where type_name is TIME, DATETIME, or TIMESTAMP, and fsp is the fractional seconds precision. 要定义包含小数秒部分的列,请使用语法type_name(fsp),其中type_name是TIME,DATETIME或TIMESTAMP,fsp是小数秒精度。 For example: 例如:

CREATE TABLE t1 (t TIME(3), dt DATETIME(6)); 创建表t1(t TIME(3),dt DATETIME(6));

The fsp value, if given, must be in the range 0 to 6. A value of 0 signifies that there is no fractional part. fsp值(如果给出)必须在0到6的范围内。值0表示没有小数部分。 If omitted, the default precision is 0. (This differs from the standard SQL default of 6, for compatibility with previous MySQL versions.) 如果省略,则默认精度为0。(这与标准SQL默认值6不同,以便与以前的MySQL版本兼容。)

As I dont define fsp it means that fsp is zero - no fractional part. 由于我没有定义fsp,这意味着fsp为零-没有小数部分。

This is my mybatis result mapper: 这是我的mybatis结果映射器:

 <resultMap id="readItemsRM" type="com.Foo">
        <id property="id" column="id"/>
        <result property="dateTime" column="dateTime"/>
  </resultMap>

This is my Foo class 这是我的富班

class Foo{
 private String id;
 private String dateTime
 //getters+setters
}

This is the result when I do foo.getDateTime() 这是我执行foo.getDateTime()时的结果

2015-01-21 16:46:03.0 2015-01-21 16:46:03.0

Why does this ".0" appear and how to avoid it? 为什么会出现“ .0”,以及如何避免呢?

The reason of .0 is that we told MySQL there is no fractional part. .0的原因是我们告诉MySQL没有小数部分。 Specifying java variable as a String we make MyBatis do a getString() for that parameter, and that's how the MySQL JDBC driver formats the value. 将java变量指定为String,我们使MyBatis为该参数执行getString(),这就是MySQL JDBC驱动程序格式化该值的方式。

Instead, it's necessary to set Java variable as java.util.Date, and format it for example using SimpleDateFormatter. 相反,必须将Java变量设置为java.util.Date,并使用例如SimpleDateFormatter对其进行格式化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM