报错内容如下:
***************************
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