用Spring Boot Starter 向導生成了一個很簡單SpringBoot程序, 用到了 MySQL, 總是下面不能加載 Mysql driver class 錯誤.
Cannot load driver class: com.mysql.jdbc.Driver
剛開始以為是 application.properties 中相關的配置項寫法不合規, 發現很正常. 檢查了 MySQL jar 文件也很正常. 突然想起之前好像也碰到過同樣的問題, 又想了很久, 終於想起上次是因為用了 spring-boot 2.0.5.RELEASE, 換成了 2.0.4.RELEASE 就好了. 這次 Spring Boot Starter 向導生成的是 2.0.6.RELEASE, 改回2.0.4.RELEASE 也好了.
記錄一下, 以免將來又碰到同樣的問題.
附: application.properties 文件
spring.datasource.url=jdbc:mysql://localhost:3306/world?useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver
附 pom.xml 片段
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>provided</scope> </dependency>