SpringBoot mysql驅動問題

F嘉陽發表於2019-03-30

問題描述

問題發生時間(2018-12-27)

環境版本:

  • SpringBoot 2.1.1
  • Mybatis 1.3.2

原始配置

spring:
  datasource:
    driver-class-name:  com.mysql.jdbc.Driver
    username: hikaricp
    password: hikaricp
    url: jdbc:mysql://localhost:3306/db_test?characterEncoding=utf-8&useSSL=false
複製程式碼

報錯資訊

Registered driver with driverClassName=com.mysql.jdbc.Driver was not found,
複製程式碼

報錯提示

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.
複製程式碼

解決辦法

從提示中可看出,驅動名稱有變,更正驅動名稱即可

更正後配置

spring:
  datasource:
    driver-class-name:  com.mysql.cj.jdbc.Driver
    username: hikaricp
    password: hikaricp
    url: jdbc:mysql://localhost:3306/db_test?characterEncoding=utf-8&useSSL=false
複製程式碼

同時若使用IDEA自帶提示也可看出問題,有兩種驅動都存在的問題,希望後續修復

mybatis1.jpg

另一種解決方案,從SpringBoot2.0之後連線池使用hikari,可根據url自動識別驅動,即如下配置也可成功執行

spring:
  datasource:
    username: hikaricp
    password: hikaricp
    url: jdbc:mysql://localhost:3306/db_test?characterEncoding=utf-8&useSSL=false
複製程式碼

相關文章