将项目由SpringBoot 1.5升级到2.0时要修改数据源配置

SpringBoot 1.5时的数据源配置

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false

升级到SpringBoot 2后,启动出现问题

The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone

原因

SpringBoot2.0中使用的jdbc驱动是mysql-connector-java 8,需要指定url的时区serverTimezone。
设置为

jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC

设定serverTimezone=UTC,会比中国时间早8个小时,如果在中国,可以选择Asia/Shanghai或者Asia/Hongkong, 或者GMT+8

## 设置为地区名称
jdbc:mysql://localhost:3306/test&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai

## 设置GMT+8
jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8

再次启动,可以运行,但是会有提示

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

这时,只要将驱动由com.mysql.jdbc.Driver改为com.mysql.cj.jdbc.Driver即可。

spring:
  datasource:
    ## 修改这里
    driver-class-name: com.mysql.cj.jdbc.Driver
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容