【解决方案】【springboot不同版本数据库连接驱动版本不一致】 导致的两种不同数据库连接信息配置方式

前景概要

因为使用的是springboot2.2.1,默认MYSQL连接驱动是8版本
在这里插入图片描述
但是配置数据库连接信息是驱动5的老版本的配置方式,

导致如下警告
Loading classcom.mysql.jdbc.Driver’. This is deprecated. The new driver class iscom.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”。通过SPI自动注册驱动程序,通常不需要手动加载驱动程序类。
报错

java.sql.SQLException: The server time zone value '�й���׼ʱ��' 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.
···
······
·········
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '�й���׼ʱ��' 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.
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_191]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_191]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_191]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_191]

springboot不同版本下的配置方式

在这里插入图片描述
application.properties 配置文件中添加 MySQL 数据库的相关配置:

mysql5 (springboot2.1以下版本使用这种配置)

在这里插入图片描述

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus
spring.datasource.username=root
spring.datasource.password=123456

mysql8(spring boot 2.1以上)

在这里插入图片描述
注意:driver和url的变化

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456

注意:

1、这里的 url 使用了 ?serverTimezone=GMT%2B8 后缀,因为Spring Boot 2.1 集成了 8.0版本的jdbc驱动,这个版本的 jdbc 驱动需要添加这个后缀,否则运行测试用例报告如下错误:

java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more

2、这里的 driver-class-name 使用了 com.mysql.cj.jdbc.Driver ,在 jdbc 8 中 建议使用这个驱动,之前的 com.mysql.jdbc.Driver 已经被废弃,否则运行测试用例的时候会有 WARN 信息

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot应用中配置多个数据库连接池可以使用多种方法。其中两种常用的方法是使用Spring Boot官方支持的多数据源配置和使用HikariCP连接池。 首先,使用Spring Boot官方支持的多数据源配置可以简单地配置和管理多个数据源。你可以在application.properties或application.yml文件中为每个数据源配置相关属性,例如数据库URL、用户名、密码等。然后,在你的代码中使用@Primary和@Qualifier注解来指定使用哪个数据源。这样,你就可以在应用中同时连接多个数据库了。\[1\] 另外,你还可以使用HikariCP连接池来配置多个数据库连接。HikariCP是一个高性能的连接池,它提供了很多配置选项。你可以在application.properties或application.yml文件中配置HikariCP的属性,例如最大连接数、最小空闲连接数等。同时,你可以使用GenericObjectPool类来创建对象池,通过传入一个对象池的配置和一个对象的工厂来快速创建对象池。\[2\]\[3\] 总之,以上是两种常用的方法来配置多个数据库连接池。你可以根据具体的需求选择适合你的方法来配置多种数据库连接池。 #### 引用[.reference_title] - *1* [SpringBoot配置连接两个或多个数据库](https://blog.csdn.net/lps12345666/article/details/130530125)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [SpringBoot 官方强烈推荐,连接池,太快了!](https://blog.csdn.net/FanZaiYo/article/details/126567587)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值