现象:使用Mybatis查询数据库记录的时间和查询出来的时间不一致

数据库记录的时间

Mybatis查询出来的时间

createTime=2019-06-02 17:39:02.0, lastUpdatedTime=2019-06-02 18:49:05.0

差了8小时。。。

原因:Mybatis连接Mysql的URL使用的时区与Mysql使用的不一致

URL用的是serverTimezone=GMT
spring.datasource.url=jdbc:mysql://localhost:3306/flashsale?useSSL=false&characterEncoding=utf8&serverTimezone=GMT

Mysql使用的是与操作系统一致的

解决方法:修改spring.datasource.url后面的serverTimezone=GMTserverTimezone=Asia/Shanghai就可以了。

修改后的URL
spring.datasource.url=jdbc:mysql://localhost:3306/flashsale?useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai

再次查询就与数据库的一致了

createTime=2019-06-02 09:39:02.0, lastUpdatedTime=2019-06-02 10:49:05.0

更多推荐

关于Mysql数据库时间和查询出来的时间不一致问题