问题:
使用yml配置的数据源,在启动项目的时候,报错了。
Unsatisfied dependency expressed through bean property 'sqlSessionFactory';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'sqlSessionFactory' defined in class path resource [tk/mybatis/mapper/autoconfigure/MapperAutoConfiguration.class]:
Unsatisfied dependency expressed through method 'sqlSessionFactory' parameter 0;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]:
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception;
nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
看了下是没有读取到 datasource ,这说明没有找到对应的文件。 看下下编译后的 target 下 classes 文件,是没有 对应的yml 文件。 难怪找不到。
处理
可以直接复制对应yml到classes文件夹下,但这样如果一clean就废了。
找到pom.xml 搜索 resources
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.yml</include>
<include>**/*.properties</include>
</includes>
<filtering>false</filtering>
</resource>
添加: <include>**/*.yml</include>
clean下,然后重新编译。这样就可以看到yml文件了。 再启动项目就正常了。
总结:
找不到合适的驱动类,除了启动类加 @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//去掉数据源 。
还有可能是对应的配置文件没有生成,这个更容易出现,是常常默认以为是有的,就没注意到!!!