SpringBoot使用mybatis,发生:Failed to configure a DataSource: 'url' attribute is not specified an
Posted 飞鱼君
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot使用mybatis,发生:Failed to configure a DataSource: 'url' attribute is not specified an相关的知识,希望对你有一定的参考价值。
最近,配置项目,使用SpringBoot2.2.1,配置mybatis访问db,配好后,使用自定义的数据源。启动发生:
APPLICATION FAILED TO START *************************** 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).
虽然,我知道,是因为我没有在application.yml文件中配置:spring.datasource。但是因为我是自定义的数据源,配置项不在spring.datasource,所以希望,不配置spring.datasource,也能启动项目。单纯在@SpringBootApplication(exclude = {DataSourceAutoConfigure.class})中排除DataSourceAutoConfigure是不行的。
下面是解决方案:
方案1:
在自定义数据源中,添加自定义自动配置类。见下面的例子:
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; import javax.annotation.Resource; import javax.sql.DataSource; import java.util.Properties; /** * 自定义自动配置类 * 1. 自定义数据源,自定义数据源是为了,防止因为没有在配置文件中,配置spring.datasource, * 出现:Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could be configured. * 2. 切面类,用来切换主从数据源 * */ @EnableAspectJAutoProxy @Configuration public class CustomDataSourceAutoConfiguration { @Bean @ConditionalOnMissingBean public DataSource defaultDataSource() { return new CustomDataSource()); } } @Bean @ConditionalOnBean({DataSource.class}) public CustomDataSourceAspect CustomDsAspect() { return new CustomDataSourceAspect(); } }
别忘了,在META-INF/spring.factories文件中配置:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.xxx.framework.datasource.masterslave.MasterSlaveDataSourceAutoConfiguration
方案2:
在启动类上,添加如下代码:
import com.lzj.framework.datasource.multi.MultiDatabaseMapperFactoryBean; 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(exclude = {DataSourceAutoConfiguration.class}) @MapperScan(basePackages = {"com.xxx.test.dao"}, factoryBean = MultiDatabaseMapperFactoryBean.class) public class LzjFrameworkTestApplication { public static void main(String[] args) { SpringApplication.run(LzjFrameworkTestApplication.class, args); } }
第一要在@SpringBootApplication中排除DataSourceAutoConfiguration类的配置;第二,要配置@MapperScan,在参数中指出,扫描标准@Mapper注解的dao接口的包位置,和自定义factoryBean的实现类。factoryBean需要自己实现,需要继承MapperFactoryBean接口。
当然,你要是不使用数据库,只需把DataSourceAutoConfiguration排除就可以了。
项目,就可以正常启动了。
以上是关于SpringBoot使用mybatis,发生:Failed to configure a DataSource: 'url' attribute is not specified an的主要内容,如果未能解决你的问题,请参考以下文章
springboot(mybatis)多数据源:mysql+mysql
springboot(mybatis)多数据源:mysql+mysql
springboot(mybatis)多数据源:mysql+mysql
springboot(mybatis)多数据源:mysql+mysql
springboot(mybatis)多数据源:mysql+mysql
SpringBoot多模块项目ApplicationContext无法getBean,MybatisPlus二级缓存与放弃Mybatis(Plus)的二级缓存