Mybatis inserted record time and to save time after the database is inconsistent, how to set?

Question:
Mybatis inserted record time and to save time after the database is inconsistent.
Java for 2020-01-02 14:28:55

After it is saved to the database 2020-01-01 14:28:55

Analysis: The
two time difference of 14 hours, it is clear that the time zone issue.

Mysql view the current time, the current time zone:

CURTIME select ();
± ---------- +
| CURTIME () |
± ---------- +
| 15:18:10 |
---------- + ±

Variables like Show "% TIME_ZONE%";
± ± ----------------- ------- +
| variable_name | the Value |
± -------- ± ------- + ---------
| system_time_zone | CST |
| time_zone | the SYSTEM |
± ± ----------------- --- + ----
2 in rows SET (0.00 sec)
#time_zone mysql described using system time zone, system_time_zone Description system using CST region

Solution:
Method one: Modify the mysql command-line mode by

set global time_zone = '+8: 00 '; ## when mysql modify global area to Beijing, the East Area 8 that is where we
set time_zone = '+8: 00' ; ## modify the current conversation area
flush privileges; # effective immediately
method two: to modify the time zone by modifying the configuration file my.cnf

vim /etc/my.cnf ## plus [mysqld] area default-time_zone = '+8: 00'

/etc/init.d/mysqld restart ## Restart mysql make the new time zone to take effect

Method 3: setting jdbcUrl, added serverTimezone = UTC parameter

jdbcUrl=jdbc:mysql://localhost:3306/wos_refer?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC

Published 48 original articles · won praise 15 · views 1352

Guess you like

Origin blog.csdn.net/Bzbtyhydcxy/article/details/103804604