Failed to configure a DataSource: 'url' ... no embedded datasource could be configured
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured
项目application.yml配置
#开发配置
spring:
profiles: devdatasource:
url: jdbc:mysql://localhost:3306/oms?useUnicode=true&characterEncoding=UTF-8&useUnicode=true&useSSL=false&allowMultiQueries=true
#driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
#db-name: oms
#filters: log4j,wall,mergeStatmybatis:
type-aliases-package: com.example.demo.models
mapper-locations: classpath*:com/example/demo/**/mapper/*.xml
启动类
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication
public class Demo8Application {
public static void main(String[] args) {
SpringApplication.run(Demo8Application.class, args);
}
}
运行程序的时候报错 :
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
解决过程
1、从网上搜索 说是 程序 没加载到 配置 于是乎 在启动类加了个
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
加上后然后好用了 , 然后 写 demo 程序 一直就是 在启动类加以上 注解参数
2、application.yml配置 中 加了 profiles: dev 然后 没写 active: dev 导致的 找不到数据库的配置
话说今天 又写了个demo学习, 还是用的 上述方法 然后 发现被人写的一个 增删改查的 demo 没用 @SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}) 也没出错 , 我就很纳闷 , 他是怎么设置的 咋好用呢 , 于是乎我跟他的demo对比了一下 , 发现配置文不同 他的 配置文件里 多了个 active: dev ,然后把 active: dev 写上 把 @SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}) 删了, ok好用
(。・v・。)# 默认使用开发配置
spring:
profiles:
active: dev
mybatis:
type-aliases-package: com.example.demo.models
mapper-locations: classpath*:com/example/demo/**/mapper/*.xml
#这个 --- 是必须的, 否则会报错
---
#开发配置
spring:
profiles: devdatasource:
url: jdbc:mysql://localhost:3306/oms?useUnicode=true&characterEncoding=UTF-8&useUnicode=true&useSSL=false&allowMultiQueries=true
#driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
#db-name: oms
#filters: log4j,wall,mergeStat