今天跑 shenyu 項(xiàng)目的時(shí)候出現(xiàn)一個(gè)十分顯眼的報(bào)錯(cuò)
2022-05-03 21:55:08 [main] INFO org.apache.shenyu.admin.spring.LocalDataSourceLoader - execute shenyu schema sql: sql-script/mysql/schema.sql
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.
2022-05-03 21:55:09 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
著重拎出來(lái)看看
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.
大概意思就是警告你要換個(gè)類名,但是項(xiàng)目代碼運(yùn)行并沒(méi)有錯(cuò)誤
我這里 mysql-server
版本是 8.0.29
, mysql-connector-java
版本是 8.0.16
解決這個(gè)報(bào)錯(cuò)的問(wèn)題很簡(jiǎn)單就是替換一下配置的 driver class 名字
spring:
datasource:
url: jdbc:mysql://localhost:3306/shenyu25?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: febsA123
#替換前 driver-class-name: com.mysql.jdbc.Driver
driver-class-name: com.mysql.cj.jdbc.Driver
好奇查下原因
官網(wǎng)文檔在 8.0 的連接介紹里確實(shí)有介紹要這么用
https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-connect-drivermanager.html
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
// Notice, do not import com.mysql.cj.jdbc.*
// or you will have problems!
public class LoadDriver {
public static void main(String[] args) {
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
}
}
}
切換到 5.1 的頁(yè)面名字就不一樣了
https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-usagenotes-connect-drivermanager.html
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
// Notice, do not import com.mysql.jdbc.*
// or you will have problems!
public class LoadDriver {
public static void main(String[] args) {
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
}
}
}
翻了下依賴包下面的源碼就發(fā)現(xiàn)警告信息出處了
這里也可以看到為了兼容原來(lái)的使用方式把
com.mysql.jdbc.Driver
實(shí)現(xiàn)成了 com.mysql.cj.jdbc.Driver
的子類
好家伙