Today using MyBatis query MySQL data, all the time is wrong, all the time than the database time more than 14 hours, considered a bit, the initial decision is the system time zone problem. Because the MySQL time zone setting defaults to the operating system time zone, looking at the next CentOS time zone, East 8 is not wrong, so it can be decided that the code is set up a wrong time zone.
Now start debugging MyBatis source code, When debugging the Createfromtimestamp method to the com.mysql.cj.jdbc.io.JdbcTimestampValueFactory of the Mysql-connector-java-6.0.4.jar package, the underlying driver for MySQL was found to be Database query time with a calender do type conversion, calender record contains the time zone is CST, and China's time zone Asia/shanghai just 14 hours.
So why does the MyBatis connection database use CST's American time? Continue to view source discovery
When MySQL connects to the database, it reads the system's time zone settings from MySQL, Debug Com.mysql.cj.mysqla.MysqlaSession.java Configuretimezone method found, this.getservervariable ("System_time_zone") The time zone setting that is read from the system is CST
At this point, it is clear that the time zone is incorrect for MySQL, log on to Linux, execute Mysql-uroot-p, and then Run command show variables like '%time_zone% ', and find that the System_time_zone item is CST.
Now, to troubleshoot the problem, modify the MySQL time zone settings.
Huvushun
Links: https://www.jianshu.com/p/ea7ef2d29940
Source: Pinterest
The copyright of the book is owned by the author, and any form of reprint should be contacted by the author for authorization and attribution.
MyBatis querying MySQL for datetime type data time difference 14 hours