本文介绍了为什么mybatis将java.util.Date值插入到Mysql datetime列然后获取它们不相等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Mybatis将java.util.Date插入到mysql datetime列中,然后抓取它找到它们不匹配
表
$ b code codeREATE TABLE`t`(
`id` int(11)NOT NULL AUTO_INCREMENT,
`create_time` datetime DEFAULT NULL,
PRIMARY KEY(`id`)
)ENGINE = InnoDB AUTO_INCREMENT = 3 DEFAULT CHARSET = utf8
代码
@Insert(insert into t(create_time)values(#{0}))
int insertT (日期日期);
@Select(select id_time from t order by id desc limit 1)
Date getLatestCreateTime();
单元测试
code> Date date1 = new Date();
mapper.insertT(date1);
日期date2 = mapper.getLatestCreateTime();
Assert.assertEquals(date1,date2);
断言失败
code> java.lang.AssertionError:
预计:Tue Aug 02 22:10:35 CST 2016
实际:Tue Aug 02 22:10:36 CST 2016
为什么是这样?
解决方案
因为在 mysql-connector
中的 TimeUtil
将Timstamp转换为Date时,它将执行以下代码
Timestamp ts = new Timestamp(tsAsMillis1 +(long)offsetDiff);
ts.setNanos(secondsPart);
和 secondsPart
为0。 >
@Test
public void test_date_compare_date_from_timestamp(){
Date date1 = new Date();
Timestamp ts = new Timestamp(date1.getTime());
System.out.println(ts); // 2016-8-08 22:37:37.078
日期date2 =新日期(ts.getTime());
Assert.assertEquals(date1,date2);
ts.setNanos(0);
System.out.println(ts);日期date3 =新日期(ts.getTime());
Assert.assertNotEquals(date1,date3);
}
因为秒数多于原始秒, p>
insert into t select 1,'08-08-08 22:42:44.676';
select * from t;
+ ---- + --------------------- +
| id | create_time |
+ ---- + --------------------- +
| 1 | 2016-08-08 22:42:45 |
+ ---- + --------------------- +
Mybatis insert java.util.Date to mysql datetime column, then fetch it find they are not match
Table
CREATE TABLE `t` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`create_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
code
@Insert("insert into t(create_time) values(#{0})")
int insertT(Date date);
@Select("select create_time from t order by id desc limit 1")
Date getLatestCreateTime();
Unit test
Date date1 = new Date();
mapper.insertT(date1);
Date date2 = mapper.getLatestCreateTime();
Assert.assertEquals(date1, date2);
Assert fail
java.lang.AssertionError:
Expected :Tue Aug 02 22:10:35 CST 2016
Actual :Tue Aug 02 22:10:36 CST 2016
why is so?
解决方案
because when TimeUtil
in mysql-connector
convert Timstamp to Date, it executes below code
Timestamp ts = new Timestamp(tsAsMillis1 + (long)offsetDiff);
ts.setNanos(secondsPart);
and secondsPart
is 0.
@Test
public void test_date_compare_date_from_timestamp() {
Date date1 = new Date();
Timestamp ts = new Timestamp(date1.getTime());
System.out.println(ts); // 2016-08-08 22:37:37.078
Date date2 = new Date(ts.getTime());
Assert.assertEquals(date1, date2);
ts.setNanos(0);
System.out.println(ts); // 2016-08-08 22:37:37.0
Date date3 = new Date(ts.getTime());
Assert.assertNotEquals(date1, date3);
}
and as for the seconds is more then original seconds, it is because
insert into t select 1,'2016-08-08 22:42:44.676';
select * from t;
+----+---------------------+
| id | create_time |
+----+---------------------+
| 1 | 2016-08-08 22:42:45 |
+----+---------------------+
这篇关于为什么mybatis将java.util.Date值插入到Mysql datetime列然后获取它们不相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!