spring boot项目时间字段少8小时问题

原创 2019-10-10 20:00 阅读(1382)次

今天发现我的工程很多数据保存后到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:mm:ss
    joda-date-time-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
然后都正常了