springboot引入了mybatis-spring-boot-starter不配置数据库参数报错

一、报错

2022-04-20 14:06:45.453  WARN 34556 --- [main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[module.log]' package. Please check your configuration.
2022-04-20 14:06:47.286  WARN 34556 --- [main] onfigReactiveWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: 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
2022-04-20 14:06:47.363  INFO 34556 --- [main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-04-20 14:06:47.380 ERROR 34556 --- [main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
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 (the profiles dev are currently active).

Disconnected from the target VM, address: '127.0.0.1:4445', transport: 'socket'

Process finished with exit code 1

原因

引入了mybatis-spring-boot-starter依赖
Spring boot默认会加载org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration类,
因为没有配置dataSource,当spring配置dataSource因缺少相关的参数就会报错。

二、解决方法

1、去掉相关的mybatis依赖
2、配置数据库参数datasource

spring:
  datasource:
    url: jdbc:mariadb://127.0.0.1:3306/my_db?characterEncoding=UTF-8&allowMultiQueries=true&autoReconnect=true
    driver-class-name: org.mariadb.jdbc.Driver
    password: password
    username: username
    hikari:
      maximum-pool-size: 50

3、阻止springboot自动配置数据源,看情况在启动类添加以下注解

//1、直接在@SpringBootApplication注解中排除数据源自动配置类
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
//2、在自动配置类注解中添加排除
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
//因为@SpringBootApplication注解已经包含了@EnableAutoConfiguration注解,所以有@SpringBootApplication注解的话直接在这个注解上加

你可能感兴趣的:(Java,java)