四种方案解决报错:Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
方案一
在@SpringBootApplication后添加exclude = { DataSourceAutoConfiguration.class }来排除自动配置 :
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
方案二
在application.yml文件中添加排除自动配置
spring:
autoconfigure:
exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
方案三
检查pom依赖有没有直接或者间接的导入了下面的依赖
比如A模块依赖了leyou-item-interface模块:
在leyou-item-interface模块中依赖了
在mapper-spring-boot-starter中依赖了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
所以会加载自动配置类,如果项目不想连接数据库的话可以选择导入更小的依赖,因为我的leyou-item-interface模块用到了persistence所以直接将
改为
这样就不会导入spring-boot-starter-jdbc的模块了,也就不会自动配置了,完美解决。
方案四
前面三种方案都是项目不用数据库的情况下,如果用数据库还报这个错基本就是四大参数没配置:
spring:
datasource:
url: jdbc:mysql://localhost:3306/xxxx
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
结语
产生这种错误的原因是:
导入了spring-boot-starter-jdbc模块但是没有配置四大参数
解决起来也不难,要么配置,要么不用。
第一次写博客,请各位大佬多多指教~~~