MySQL-Connector-Java升级到8.0.11更改从DB检索的所有日期值



对于春季项目,mysql-connector-java已从6.0.6迁移到8.0.11

因此,使用8.0.11,问题是:

Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: 
The server time zone value 'PET' is unrecognized or represents more than one time zone. 
You must configure either the server or JDBC driver (via the serverTimezone configuration property) 
to use a more specifc time zone value if you want to utilize time zone support.

进行研究

  • 服务器时区值" Aest"未被认可或代表一个以上的时区

解决方案是更改 URL(我不想返回上一个版本(

  • 来自:mysql.jdbcUrl = jdbc:mysql://localhost:3306/web_v01?useSSL=false
  • to: mysql.jdbcUrl = jdbc:mysql://localhost:3306/web_v01?useSSL=false&serverTimezone=UTC

观察添加&serverTimezone=UTC

在我的数据库中,我有以下内容:

mysql> select * from persona;
+-----+--------------+-------------+------------+
| id  | nombre       | apellido    | fecha      |
+-----+--------------+-------------+------------+
...
| 088 | Something    | Something   | 1981-07-06 |
...
+-----+--------------+-------------+------------+

Spring应用程序通过RowMapper<Persona>从DB检索时,我可以确认rs.getDate("fecha")返回1981-07-05(观察一天已减少了一天,它是 note recke(

(

如果mysql-connector-java返回到6.0.6,因此mysql.jdbcUrl = jdbc:mysql://localhost:3306/web_v01?useSSL=false( no serverTimezone=UTC(rs.getDate("fecha")返回1981-07-06(预期如何(

因此,如何解决此方法与8.0.11

serverTimezone从一开始就从未声明时,我想要具有相同的行为,当然避免例外。

因此,如果考虑到serverTimezone的价值无关紧要,则解决方案会更好。

有几个与时区有关的属性:

usetimezone:将客户端和服务器时区之间的时间/日期类型转换(true/fals,默认为" false"(?这是旧版日期时间代码的一部分,因此,仅当" uselegacydatetimecode = true"时,属性才会产生效果。默认值:false

uselegacyDateTimeCode:在结果集和语句中使用代码进行日期/时间/dateTime/Timestamp处理,这些语句始终如一地处理从客户端到服务器的时区转换,或者对这些驱动程序中的这些数据类型使用旧的代码?将此属性设置为" false",可以使" USETIMEZONE"," USETIMEZONE"," USEJDBCCOMPIANT TIMENESHIFT"," use usegmtmillisfordateTimes"one_answers" usefastDateParsing"的效果。默认:true

Servertimezone:覆盖时区检测/映射。当服务器的时区未映射到Java时区域

时使用

如果mysql-connector-java为5.1,则应指定三个属性,例如:jdbc:mySQL://主机:port/dbname?usetimezone = true&amp; uselegacydateTimeCode = true&amp; servertimeZone = gmt%2B08:00

如果mysql-connector-java为8.0,则应指定一个属性,例如:JDBC:mysql://主机:port/dbname?servertimezone = gmt%2B08:00

尝试使用

jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=CST

对于我的情况,最不可能的怀疑是 Jackson

如果您使用的Spring Boot将以下属性添加到 application.properties

spring.jackson.time-zone=Asia/Colombo

我在这里详细回答了这一点 - &gt;https://stackoverflow.com/a/68016006/9183199

最新更新