V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
anzu
V2EX  ›  MySQL

MySQL Timestamp 不能直接接收整型

  •  
  •   anzu · Apr 27, 2021 · 1530 views
    This topic created in 1826 days ago, the information mentioned may be changed or developed.

    字段:begin_at timestamp;

    JPA:private Timestamp beginAt;

    程序运行环境本地时间与数据库本地时间不同。

    begin_at 是前端传来的时间戳,但是在保存的时候 JPA 将它转换成了本地时间(+0 时区)字符串表示,插入数据库的时候,数据库将该字符串再次转换成时间戳,然而使用的是+8 时区进行转换,导致最终保存在数据库的时间戳少了 8 小时。

    Java 日志类似如下:

    org.hibernate.SQL                        : insert into table (begin_at) values (?)
    o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [TIMESTAMP] - [2021-04-27 16:33:07.645]
    

    我以为是 JPA 捣乱,试着直接在 MySQL 敲命令:

    insert into table (begin_at) values (1619514153)

    报错:

    Incorrect datetime value: '1619514153' for column 'begin_at'

    原本想用时间戳统一表示时间,消除因不同时区导致的时间问题,结果 MySQL 的 timestamp 字段不能直接接收整型,坑啊啊啊啊

    brader
        1
    brader  
       Apr 27, 2021
    timestamp 接受一个时间字符串。
    建议用 int 吧,我做了这么多项目,都是一直用的 int
    yogogo
        2
    yogogo  
       Apr 27, 2021
    我都是用 int
    F281M6Dh8DXpD1g2
        3
    F281M6Dh8DXpD1g2  
       Apr 27, 2021
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5604 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 07:08 · PVG 15:08 · LAX 00:08 · JFK 03:08
    ♥ Do have faith in what you're doing.