com.mysql.jdbc.Driver
是 mysql-connector-java 5中的,com.mysql.cj.jdbc.Driver
是 mysql-connector-java 6中的
mysql5用的驱动url是com.mysql.jdbc.Driver,mysql6以后用的是com.mysql.cj.jdbc.Driver。版本不匹配便会报驱动类已过时的错误。
1、JDBC连接Mysql6 (com.mysql.cj.jdbc.Driver), 需要指定时区serverTimezone,否则会报如下错误,
org.mybatis.spring.MyBatisSystemException:
nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database.
Cause: org.springframework.jdbc.CannotGetJdbcConnectionException:
Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException:
Cannot create PoolableConnectionFactory (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.)
指定时区serverTimezone:
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
2、JDBC连接Mysql5 com.mysql.jdbc.Driver
:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=
3、如果mysql-connector-java用的6.0以上的,依赖如下:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
但你的driver用的还是com.mysql.jdbc.Driver
,就会报错:
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.