当使用Spring Boot 2.0 整合MySQL的时候配置可能会出现这个故障

spring.datasource.driver-class-name= com.mysql.jdbc.Driver

Cannot resolve class or package ‘jdbc’ less… (Ctrl+F1) Inspection
info: Checks Spring Boot application .properties configuration files.
Highlights unresolved and deprecated configuration keys and invalid
values.

这里写图片描述

故障分析:

其实这个问题是由于MySQL 这个jar 包依赖类型默认是runtime ,
也就是说只有运行时生效,所以虽然这里报错,但是不影响你代码运行。
这里写图片描述

解决方案一:

将runtime 修改为Compile 即可

  1. 选中项目—> 右键-----> Open Module Settings
    这里写图片描述

  2. Modules ------> *****App -------> Dependencies

这里写图片描述

3.将runtime 修改为Compile即可

这里写图片描述

解决方案二

这里更新下第二种解决方法,既然由于依赖的范围引起的,那么我们其实只需要修改pom.xml 中的依赖配置,将scope 范围修改为compiler也可以达到解决方案一的目的。

<dependency>
	  <groupId>mysql</groupId>
	  <artifactId>mysql-connector-java</artifactId>
	  <version>8.0.19</version>
	  <scope>compile</scope>
</dependency>

除此之外,还有个注意事项,

  • MySQL 8 配置是
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  • MySQL 5.7 配置应该是
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

解决方案三

如果是Spring Boot 项目,那么引入MySQL 驱动的最佳方式是

         <!-- added MySQL support -->
         <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
             <scope>runtime</scope>
         </dependency>

目前Intellij Idea 最新版貌似已经修复这个Bug,更新升级Intellij Idea 即可。

Logo

更多推荐