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 . ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
[32m :: Spring Boot :: [39m [2m (v2.7.9)[0;39m
[2m2023-03-17 08:58:12.220[0;39m [32m INFO[0;39m [35m13896[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mc.e.redisdemo.RedisDemoApplication [0;39m [2m:[0;39m 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)
[2m2023-03-17 08:58:12.223[0;39m [32m INFO[0;39m [35m13896[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mc.e.redisdemo.RedisDemoApplication [0;39m [2m:[0;39m No active profile set, falling back to 1 default profile: "default"
[2m2023-03-17 08:58:12.335[0;39m [32m INFO[0;39m [35m13896[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36m.e.DevToolsPropertyDefaultsPostProcessor[0;39m [2m:[0;39m Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
[2m2023-03-17 08:58:12.336[0;39m [32m INFO[0;39m [35m13896[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36m.e.DevToolsPropertyDefaultsPostProcessor[0;39m [2m:[0;39m For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
[2m2023-03-17 08:58:13.990[0;39m [32m INFO[0;39m [35m13896[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port(s): 8080 (http)
[2m2023-03-17 08:58:13.992[0;39m [32m INFO[0;39m [35m13896[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.a.catalina.core.AprLifecycleListener [0;39m [2m:[0;39m Loaded Apache Tomcat Native library [1.2.36] using APR version [1.7.2].
[2m2023-03-17 08:58:13.993[0;39m [32m INFO[0;39m [35m13896[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.a.catalina.core.AprLifecycleListener [0;39m [2m:[0;39m APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true], UDS [true].
[2m2023-03-17 08:58:13.993[0;39m [32m INFO[0;39m [35m13896[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.a.catalina.core.AprLifecycleListener [0;39m [2m:[0;39m APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
[2m2023-03-17 08:58:13.997[0;39m [32m INFO[0;39m [35m13896[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.a.catalina.core.AprLifecycleListener [0;39m [2m:[0;39m OpenSSL successfully initialized [OpenSSL 1.1.1t 7 Feb 2023]
[2m2023-03-17 08:58:14.013[0;39m [32m INFO[0;39m [35m13896[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.apache.catalina.core.StandardService [0;39m [2m:[0;39m Starting service [Tomcat]
[2m2023-03-17 08:58:14.013[0;39m [32m INFO[0;39m [35m13896[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36morg.apache.catalina.core.StandardEngine [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/9.0.71]
[2m2023-03-17 08:58:14.131[0;39m [32m INFO[0;39m [35m13896[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/] [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext
[2m2023-03-17 08:58:14.131[0;39m [32m INFO[0;39m [35m13896[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 1794 ms
[2m2023-03-17 08:58:14.301[0;39m [33m WARN[0;39m [35m13896[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mConfigServletWebServerApplicationContext[0;39m [2m:[0;39m 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
[2m2023-03-17 08:58:14.308[0;39m [32m INFO[0;39m [35m13896[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.apache.catalina.core.StandardService [0;39m [2m:[0;39m Stopping service [Tomcat]
[2m2023-03-17 08:58:14.342[0;39m [32m INFO[0;39m [35m13896[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mConditionEvaluationReportLoggingListener[0;39m [2m:[0;39m
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
[2m2023-03-17 08:58:14.392[0;39m [31mERROR[0;39m [35m13896[0;39m [2m---[0;39m [2m[ restartedMain][0;39m [36mo.s.b.d.LoggingFailureAnalysisReporter [0;39m [2m:[0;39m
***************************
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.Driverspring.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: 'url' attribute is not specified如何解决? 的全部内容, 来源链接: utcz.com/p/945044.html