1. 报错场景

邮件服务不需要连接数据库, 但是底层包中引入了


org.mybatis.spring.boot
mybatis-spring-boot-starter

2. 报错

  1. Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
  2. 2021-12-10 17:08:49.976 ERROR 12188 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
  3. ***************************
  4. APPLICATION FAILED TO START
  5. ***************************
  6. Description:
  7. Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
  8. Reason: Failed to determine a suitable driver class
  9. Action:
  10. Consider the following:
  11. If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
  12. If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
  13. Disconnected from the target VM, address: '127.0.0.1:61178', transport: 'socket'
  14. Process finished with exit code 1

3. 处理方案:

3.1 配置文件中设置数据库连接信息

  1. spring:
  2. application:
  3. name: ml-finance-service
  4. datasource:
  5. driver-class-name: com.mysql.cj.jdbc.Driver
  6. url: jdbc:mysql://****/**?allowMultiQueries=true&serverTimezone=GMT%2B8&characterEncoding=utf8
  7. username: ml
  8. password: Ml123456..

3.2 启动类设置 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

  1. /**
  2. * @author ml
  3. */
  4. @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
  5. @ComponentScan("com.ml")
  6. public class MailApplication extends SpringBootServletInitializer {
  7. public static void main(String[] args) {
  8. new SpringApplicationBuilder(MailApplication.class).run(args);
  9. }
  10. }