32位数据源中没有mysql_Spring boot 启动提示数据源错误

在启动 Spring Boot 的项目的时候提示数据源未配置的错误。

09:52:08.333 [main] DEBUG o.s.b.d.LoggingFailureAnalysisReporter - Application failed to start due to an exception
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:233)
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:174)

Spring 会提示你完整的导致启动错误的信息是:

***************************
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).


Process finished with exit code 1

错误分析

从上面的启动信息来看,已经说得非常清楚了,就是因为你配置了 Spring 的数据组件,但是你没有配置相应的数据源。

因为这个会导致你的启动失败。

解决办法

有下面的集中解决办法:

加入 H2 包

最简单的解决办法就是在依赖中添加 H2 的数据库,如果你使用 Spring Batch 的话,这个组件也是需要的,因为 Spring 会使用 H2 为数据源。

添加数据源配置

如果你已经添加了数据库驱动,例如你添加了 mysql 的数据库驱动。

那么你需要制定 Mysql 的数据库连接参数。

spring.datasource.url=jdbc:mysql://localhost:3306/myDb
spring.datasource.username=user1
spring.datasource.password=pass
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

在启动时候不载入数据源配置。

你可用在启动的时候不载入数据源配置。

可用在启动类上面,添加下面的注解。

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})

c1cfc4de8c717ea79fa9a64fa8d5b6cd.png

你也可以在启动配置文件上面,添加下面的内容,这样能够保证你在启动的时候不载入数据源配置类。

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAuto
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot应用使用多数据源可以实现读写分离,提高系统的性能和可用性,同时也可以满足不同业务场景下的需求。下面我们来介绍如何使用Spring Boot数据源实现读写分离。 1. 添加依赖 在pom.xml文件添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> ``` 其spring-boot-starter-jdbc是Spring Boot提供的JDBC Starter,用于连接数据库。druid是阿里巴巴开源的数据库连接池。 2. 配置数据源 在application.yml文件添加数据源配置: ``` spring: datasource: master: url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver slave: url: jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver ``` 其,我们配置了两个数据源:master和slave。分别连接了两个不同的数据库,用于实现读写分离。 3. 配置数据源和事务管理器 在Spring Boot,我们需要自己配置数据源和事务管理器。可以在代码通过@Primary和@Qualifier注解实现数据源的动态切换。 ``` @Configuration public class DataSourceConfig { @Bean(name = "masterDataSource") @ConfigurationProperties(prefix = "spring.datasource.master") public DataSource masterDataSource() { return DruidDataSourceBuilder.create().build(); } @Bean(name = "slaveDataSource") @ConfigurationProperties(prefix = "spring.datasource.slave") public DataSource slaveDataSource() { return DruidDataSourceBuilder.create().build(); } @Bean(name = "dynamicDataSource") @Primary public DynamicDataSource dynamicDataSource(@Qualifier("masterDataSource") DataSource masterDataSource, @Qualifier("slaveDataSource") DataSource slaveDataSource) { Map<Object, Object> targetDataSources = new HashMap<>(); targetDataSources.put(DynamicDataSource.DataSourceType.MASTER, masterDataSource); targetDataSources.put(DynamicDataSource.DataSourceType.SLAVE, slaveDataSource); return new DynamicDataSource(masterDataSource, targetDataSources); } @Bean(name = "transactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("dynamicDataSource") DynamicDataSource dynamicDataSource) { return new DataSourceTransactionManager(dynamicDataSource); } } ``` 其,我们定义了DataSourceConfig配置,用于配置数据源和事务管理器。我们通过@Bean注解创建数据源对象并读取application.yml配置文件数据源信息。然后通过@Primary注解指定默认的数据源为Master数据源。最后,我们创建了DynamicDataSource对象,用于动态切换数据源,同时创建了事务管理器对象,用于管理事务。 4. 定义数据源切换注解 我们可以通过自定义注解来实现动态切换数据源。 ``` @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface TargetDataSource { DynamicDataSource.DataSourceType value() default DynamicDataSource.DataSourceType.MASTER; } ``` 其,@TargetDataSource注解用于标记数据源型,我们通过value()方法指定数据源型,默认为Master数据源。 5. 定义动态数据源 我们可以通过继承AbstractRoutingDataSource来实现动态数据源的切换。 ``` public class DynamicDataSource extends AbstractRoutingDataSource { private DataSource masterDataSource; private Map<Object, Object> targetDataSources; public DynamicDataSource(DataSource masterDataSource, Map<Object, Object> targetDataSources) { this.masterDataSource = masterDataSource; this.targetDataSources = targetDataSources; super.setDefaultTargetDataSource(masterDataSource); super.setTargetDataSources(targetDataSources); super.afterPropertiesSet(); } @Override protected Object determineCurrentLookupKey() { return DataSourceContextHolder.getDataSourceType(); } public enum DataSourceType { MASTER, SLAVE } } ``` 其,我们定义了DynamicDataSource,继承了AbstractRoutingDataSource。在DynamicDataSource,我们重写了determineCurrentLookupKey()方法,根据当前线程的数据源型,返回对应的数据源。我们通过枚举DataSourceType定义了Master和Slave两种数据源型。 6. 实现数据源切换 我们可以通过AOP的方式,在方法执行前切换数据源。 ``` @Aspect @Component public class DataSourceAspect { @Pointcut("@annotation(com.example.demo.annotation.TargetDataSource)") public void dataSourcePointCut() { } @Around("dataSourcePointCut()") public Object around(ProceedingJoinPoint point) throws Throwable { MethodSignature signature = (MethodSignature) point.getSignature(); TargetDataSource dataSource = signature.getMethod().getAnnotation(TargetDataSource.class); if (dataSource == null) { DataSourceContextHolder.setDataSourceType(DynamicDataSource.DataSourceType.MASTER); } else { DataSourceContextHolder.setDataSourceType(dataSource.value()); } try { return point.proceed(); } finally { DataSourceContextHolder.clearDataSourceType(); } } } ``` 其,我们定义了DataSourceAspect切面,用于切换数据源。我们通过@Around注解标记了切点,在方法执行前,根据注解指定的数据源型,切换数据源。执行完毕后,再切换回默认的Master数据源。 7. 编写业务代码 最后,我们编写业务代码,通过@TargetDataSource注解指定数据源型。 ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override @TargetDataSource(DynamicDataSource.DataSourceType.MASTER) public void addUser(User user) { userMapper.addUser(user); } @Override @TargetDataSource(DynamicDataSource.DataSourceType.SLAVE) public User getUserById(int id) { return userMapper.getUserById(id); } } ``` 其,我们定义了UserServiceImpl,实现了UserService接口。在addUser()方法,我们指定数据源型为Master数据源,在getUserById()方法,我们指定数据源型为Slave数据源。 通过以上步骤,我们就可以实现Spring Boot数据源的读写分离了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值