Springboot + hibernate 运行正常,打成jar包后启动报错:Failed to configure a DataSource

***************************
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,将下图中标红的删除即可。

附上完整的pom.xml

<?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 http://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>3.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
   
    <groupId>com.test</groupId>
    <version>0.0.1-SNAPSHOT</version>
 	<artifactId>mservice-sql</artifactId>
  	<name>mservice-sql</name>
  	<description>mservice-sql</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>21</java.version>
        <spring-cloud.version>2024.0.0</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
   

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
        	<groupId>org.springframework.boot</groupId>
        	<artifactId>spring-boot-starter-data-jpa</artifactId>
    	</dependency>
    	   
    	<dependency>
			<groupId>com.mysql</groupId>
			<artifactId>mysql-connector-j</artifactId>		
		</dependency>

  

   		<dependency>
     		<groupId>commons-codec</groupId>
     		<artifactId>commons-codec</artifactId>
 		</dependency>

  
    	
    	
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>              
            </plugin>
        </plugins>
        
        <resources>
    
            <resource>
                <directory>src/main/resources</directory>   
                         
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
      
        </resources>
        
    </build>

</project>

### 解决方案 当Spring Boot应用程序启动时遇到“Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured”的错误,表明框架未能成功配置数据源。这可能是由多种原因引起的。 #### 原因分析 1. **缺少必要的依赖项** Spring Boot尝试自动配置嵌入式数据库(如H2、HSQL或Derby),但如果这些库不在类路径上,则会失败[^2]。 2. **外部数据库设置缺失** 若应用期望连接到外部关系型数据库而非内置数据库,那么`application.properties`或`application.yml`文件中的相应属性应被正确定义以提供URL、用户名和密码等必要参数[^1]。 3. **激活特定环境下的配置文件** 应用程序可能设计成根据不同活动的profile加载不同的数据库设定;如果当前没有任何active profile,则默认情况下不会加载任何特殊的数据源配置。 4. **排除DataSourceAutoConfiguration** 对于某些不打算立即建立数据库链接的应用场景,可以通过在主类上添加`@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})`来阻止Spring Boot自动生成数据源实例[^3]。 #### 实施建议 为了修复上述提到的问题,可以采取以下措施之一: - 将所需的JDBC驱动加入项目的构建描述符(`pom.xml`)中; ```xml <!-- MySQL Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> ``` - 在`src/main/resources/application.properties`内定义完整的数据源详情: ``` spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=mypassword spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` - 或者,在多环境部署的情况下,通过命令行参数指定要使用的profile,并确保对应profile下有适当的数据源配置: `java -jar myapp.jar --spring.profiles.active=prod` 对于确实不需要即时创建数据源的情况,可以在启动类上方加上注解防止其发生: ```java @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ``` 请注意,最后一种方式适用于那些暂时不想处理数据库逻辑或是计划稍后再引入持久层组件的情形。而对于大多数实际项目而言,前两种解决方案更为常见也更推荐采用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值