SpringBoot项目无法启动,提示Failed to configure a DataSource: 'url' attribute is not specified如何解决?

Eclipse+SpringBoot+MyBatis,死活无法启动。百度了很久,尝试各种办法误解解决。console提示如下:

08:58:11.576 [Thread-2] DEBUG org.springframework.boot.devtools.restart.classloader.RestartClassLoader - Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@4177cebd

. ____ _ __ _ _

/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \

( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

\\/ ___)| |_)| | | | | || (_| | ) ) ) )

' |____| .__|_| |_|_| |_\__, | / / / /

=========|_|==============|___/=/_/_/_/

 :: Spring Boot ::   (v2.7.9)

2023-03-17 08:58:12.220  INFO 13896 --- [ restartedMain] c.e.redisdemo.RedisDemoApplication  : Starting RedisDemoApplication using Java 1.8.0_351 on DESKTOP-OQ55J3D with PID 13896 (D:\MyData\JavaData\WorkSpace\SpringToolSuit\redis-demo\target\classes started by luqing in D:\MyData\JavaData\WorkSpace\SpringToolSuit\redis-demo)

2023-03-17 08:58:12.223  INFO 13896 --- [ restartedMain] c.e.redisdemo.RedisDemoApplication  : No active profile set, falling back to 1 default profile: "default"

2023-03-17 08:58:12.335  INFO 13896 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable

2023-03-17 08:58:12.336  INFO 13896 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'

2023-03-17 08:58:13.990  INFO 13896 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)

2023-03-17 08:58:13.992  INFO 13896 --- [ restartedMain] o.a.catalina.core.AprLifecycleListener  : Loaded Apache Tomcat Native library [1.2.36] using APR version [1.7.2].

2023-03-17 08:58:13.993  INFO 13896 --- [ restartedMain] o.a.catalina.core.AprLifecycleListener  : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true], UDS [true].

2023-03-17 08:58:13.993  INFO 13896 --- [ restartedMain] o.a.catalina.core.AprLifecycleListener  : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]

2023-03-17 08:58:13.997  INFO 13896 --- [ restartedMain] o.a.catalina.core.AprLifecycleListener  : OpenSSL successfully initialized [OpenSSL 1.1.1t 7 Feb 2023]

2023-03-17 08:58:14.013  INFO 13896 --- [ restartedMain] o.apache.catalina.core.StandardService  : Starting service [Tomcat]

2023-03-17 08:58:14.013  INFO 13896 --- [ restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.71]

2023-03-17 08:58:14.131  INFO 13896 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]  : Initializing Spring embedded WebApplicationContext

2023-03-17 08:58:14.131  INFO 13896 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1794 ms

2023-03-17 08:58:14.301  WARN 13896 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cityController': Unsatisfied dependency expressed through field 'cityMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cityMapper' defined in file [D:\MyData\JavaData\WorkSpace\SpringToolSuit\redis-demo\target\classes\com\example\redisdemo\dao\CityMapper.class]: 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 [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.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

2023-03-17 08:58:14.308  INFO 13896 --- [ restartedMain] o.apache.catalina.core.StandardService  : Stopping service [Tomcat]

2023-03-17 08:58:14.342  INFO 13896 --- [ restartedMain] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.

2023-03-17 08:58:14.392 ERROR 13896 --- [ restartedMain] 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 (no profiles are currently active).

POM文件如下

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.7.9</version>

<relativePath /> <!-- lookup parent from repository -->

</parent>

<groupId>com.example</groupId>

<artifactId>redis-demo</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>redis-demo</name>

<description>Demo project for Spring Boot</description>

<properties>

<java.version>1.8</java.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>2.3.0</version>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-devtools</artifactId>

<scope>runtime</scope>

<optional>true</optional>

</dependency>

<dependency>

<groupId>com.mysql</groupId>

<artifactId>mysql-connector-j</artifactId>

<scope>runtime</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

<resources>

<resource>

<directory>src/main/java</directory>

<includes>

<include>**/*.xml</include>

</includes>

<filtering>true</filtering>

</resource>

<resource>

<directory>src/main/resources</directory>

<includes>

<include>**/*.xml</include>

</includes>

<filtering>true</filtering>

</resource>

</resources>

</build>

</project>

application.properties文件如下

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/world

spring.datasource.username=root

spring.datasource.password=123456

mybatis.mapper-locations=classpath:/mapper/*.xml


回答:

原因在于

<resource>

<directory>src/main/resources</directory>

<includes>

<include>**/*.xml</include>

<!-- 加上这个就行了 -->

<include>*.properties</include>

</includes>

<filtering>true</filtering>

</resource>

application.properties也位于src/main/resources下面。但是你这个配置仅处理*.xml结尾的文件,将配置文件忽略了。

或者你也可以直接删除resources相关的配置,理论上不用处理dao.xml的。


本文参与了SegmentFault 思否面试闯关挑战赛,欢迎正在阅读的你也加入。

以上是 SpringBoot项目无法启动,提示Failed to configure a DataSource: &#x27;url&#x27; attribute is not specified如何解决? 的全部内容, 来源链接: utcz.com/p/945044.html

回到顶部