Failed to determine a suitable driver class

启动就报这个错误:

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)

看字面意思是没找到数据源。网上说是在应用中没有用到数据源但是添加了mybatis的依赖:

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>

很可惜我不是这种情况,我的这个模块是服务模块,里面用到mybatis-plus的相关包,而且application.yml文件中也配置了数据源正常:

datasource:
  driver-class-name: com.mysql.cj.jdbc.Driver
  username: root
  password: 123456
  url: jdbc:mysql://localhost:3306/seckill?serverTimezone=GMT%2B8

说什么要在启动类上面排除数据源的自动注入:

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//去掉数据源

然后就报这个错:

Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

然后居然让写一个基础dao:

可能是为了解决多数据源的问题吧,取消了自动注入。没用到多数据源,不太关心这个。

解决方案:因为我们dao层是继承于一个dao基类,所以只要在这个基类中注入任意一个属性即可。SqlSessionFactory在spring配置文件中已经配置。

public class CommonDao extends SqlSessionDaoSupport {
     @Resource
     public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
         super.setSqlSessionFactory(sqlSessionFactory);
     }
}

然后我所有的mapper都要继承这个。。。

果断放弃,最终找到如下方案。

解决办法

亲测有效,在pom.xml文件添加如下:

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.yml</include>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.yml</include>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>

Star_陈
23 声望2 粉丝

人的内心好像一直有一片荒芜的夜地,留给那个幽暗又寂寞的自我。