Failed to auto-configure a DataSource

报错内容如下:

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-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).

引起原因:

springboot项目,引入mybaits依赖和mysql依赖后报错,引入的依赖如下:

         <!-- mybatis相关依赖 -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.1.1</version>
		</dependency>
		<!-- mysql 依赖 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>

原因分析:

springboot项目,引入mybaits依赖和mysql依赖之后,项目启动时会自动加载application.xml里面datasource配置,如果没有配置,则会以上错误

解决办法:

1、如果不需要数据源,启动类中排除DataSourceAutoConfiguration.class即可,配置如下

package com.it;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * 
 * @ClassName: AppPortalWeb 
 * @description: TODO(这里用一句话描述这个类的作用) 
 * @author 互联网架构师-liuxk  
 * @date 2019年4月12日 下午4:50:46 
 * @version V1.0
 */
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class,//此处排除DataSource的auto-configured配置
		DataSourceTransactionManagerAutoConfiguration.class, //此处排除DataSourceTransactionManager的auto-configured配置
		HibernateJpaAutoConfiguration.class })//此处排除HibernateJpa的auto-configured配置
@EnableEurekaClient
@EnableFeignClients
public class AppPortalWeb {

	public static void main(String[] args) {
		SpringApplication.run(AppPortalWeb.class, args);
	}

}

2、如果需要数据源,引入数据源配置即可

###数据库相关连接  
spring:    
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.1.81:3306/shop_member?useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: root

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值