发布时间:2026-07-21阅读(1)
线上java代码
/** * 示例代码,忽略部分实现 */ @Autowired AMapper aMapper; @Test public Response triggerCollectAllocate() { DispatchInfoUpdateDto dispatchInfoUpdateDto= new DispatchInfoUpdateDto(); dispatchInfoUpdateDto.setAllocateTime(new Date()); dispatchInfoUpdateDto.setId(123); aMapper.testA(dispatchInfoUpdateDto); }/** * 简单例子,不建议使用注解 **/ @Insert(" INSERT INTO table_a " " ( allocate_time, id) " " VALUES ( #{allocateTime,jdbcType=TIMESTAMP},#{id}) " " ON DUPLICATE KEY UPDATE " " allocate_time = VALUES(allocate_time)") int testA(DispatchInfoUpdateDto dispatchInfoUpdateDto);
插入结果

更新时间,大于allocate_time 10几个小时左右。
初步怀疑时区问题。
问题原因首先登录服务器,查询服务器时区 date -R 命令


服务器时区就是 0800 就是Asia/Shanghai
之后登录 数据库,查询当前数据库的时区。

这个CST 居然能够表示四种时区,分别为:
1.美国中部时间 Central Standard Time (USA) UTC-06:00
2.澳大利亚中部时间 Central Standard Time (Australia) UTC 09:30
3.中国标准是 China Standard Time UTC 08:00
4.古巴标准时 Cuba Standard Time UTC-04:00
默认是美国中部时区
这个问题是Mysql时区设置不对造成的。
解决办法一般有经验的op(运维)都会设置为中国标准时 China Standard Time UTC 08:00,
这里给出,代码修改方式,优点修改少,不需要重启启动数据库。
就是在数据库连接上加入: serverTimezone=Asia/Shanghai,例子如下:
url : jdbc:mysql://XXXX:3306/XXdatabase?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false

最后,我想说,运维靠点谱的话,大概率大家都不会遇到这个问题。
上一篇:五阶魔方怎么还原
下一篇:如何注册qq号 怎样注册qq号
Copyright © 2024 有趣生活 All Rights Reserve吉ICP备19000289号-5 TXT地图HTML地图XML地图