问题
- 时间获取当前时间写入数据库时和当前时间相差8小时
- 明显相差
原因
1. Java 数据库连接使用 UTC 时区(世界标准时间),即 serverTimezone=UTC,而北京时间比 UTC 时间早8小时,即 UTC+08:00,如果我们直接使用 serverTimezone=UTC,写入数据库中的数据会提前 8 个小时。
使用数据库MySQL8.0就必须需要时区这些配置
spring:
application:
name: fileupload
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/csdw?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
username: root
password: 11111
注意这一句中的serverTimezone=UTC
url: jdbc:mysql://localhost:3306/csdw?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
解决
url: jdbc:mysql://localhost:3306/csdw?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8