今天发现我的工程很多数据保存后到mysql,创建时间字段都少了8小时,看了下其他微服务兄弟项目的数据,都不会有问题,然后才发现别的项目在连接mysql的url上有加上时区+8小时的配置,如下:
spring:
datasource:
name: mysql_test
type: com.alibaba.druid.pool.DruidDataSource
#druid相关配置
druid:
#监控统计拦截的filters
filters: stat
#driver-class-name: com.mysql.cj.jdbc.Driver
driver-class-name: com.mysql.jdbc.Driver
#基本属性
url: jdbc:mysql://xxxxxx:3306/sysbase?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8
username: root
....我把连接配置改成:
jdbc:mysql://xxxx:3306/sysbase?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2b8
就发现保存数据到mysql时间字段正常了。
但是通过springmvc查询显示到jsp页面中时又少了8小时。。。
后来想想应该是要设置json转换时间的时区,配置如下:
spring:
profiles:
active: dev
jackson:
date-format: yyyy-MM-dd HH🇲🇲ss
joda-date-time-format: yyyy-MM-dd HH🇲🇲ss
time-zone: GMT+8然后都正常了