光明圣火 2022-06-23 11:45 采纳率: 70.6%
浏览 104
已结题

springboot多模块项目,启动报数据源相关错误

尝试搭建多模块项目,项目本来一切就绪,编译上已经不存在错误了,但是在启动时还是报了一个错
错误详情

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

上网寻找解决方法,看到大概这个错误发生原因是数据源配置存在问题,但是我yml中的数据源应该没有啥问题,因为这在我另一个单模块项目中启动没有报错,这个是我yml的配置:


spring:
  #配置数据源
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/longcaiuserbook?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&useUnicode=true&useSSL=false
    username: root
    password: root

    #配置druid连接池
    type: com.alibaba.druid.pool.DruidDataSource
    filters: stat
    max-active: 20
    initial-size: 1
    min-idle: 3
    max-wait: 60000
    time-between-eviction-runs-millis: 60000
    min-evictable-idle-time-millis: 300000
    test-while-idle: true
    test-on-borrow: false
    test-on-return: false
    validationQuery: select 'x'
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20
  mvc:
    view:
      prefix: /views/
      suffix: .html

#配置mybatis
mybatis:
  #配置mapper.xml所在路径
  mapper-locations: classpath*:/mapping/*.xml,classpath*:pmp/mapper/*/*.xml
  # 配置映射类所在的包名:
  type-aliases-package: com.cjsg.property.**.entity

server:
  port: 62

这个是我的启动类代码

package com.cjsg.testmultiplemodule;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
@ServletComponentScan
@MapperScan("com.cjsg.**.dao")
public class TestMultipleModuleApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(TestMultipleModuleApplication.class, args);
    }

}


这是我的主模块的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>

    <groupId>com.cjsg</groupId>
    <artifactId>TestMultipleModule</artifactId>
    <version>1.0</version>
    <name>TestMultipleModule</name>
    <description>springboot多模块测试</description>

    <packaging>pom</packaging>

    <properties>
        <java.version>1.8</java.version>
        <druid.version>1.1.20</druid.version>
        <commons-io.version>2.6</commons-io.version>
        <commons-fileupload.version>1.3.1</commons-fileupload.version>
        <poi.version>3.17</poi.version>
        <spring-boot.version>2.6.8</spring-boot.version>
    </properties>

    <dependencyManagement>
    <!-- 依赖项定义 -->
    <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${spring-boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
    </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- SpringBoot的依赖配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>

        <!--阿里数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>

        <!--io常用工具类 -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons-io.version}</version>
        </dependency>

        <!--文件上传工具类 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>${commons-fileupload.version}</version>
        </dependency>

        <!-- excel工具 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>${poi.version}</version>
        </dependency>

        <!--MyBatis支持springboot的配置依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
        <!-- mybatis的jar包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.7</version>
        </dependency>

        <!--MySQL数据库配置-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- mysql的jdbc的jar包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>

<!--踩坑记录,不能在父模块引进子模块,避免循环依赖报错-->
<!--        <dependency>-->
<!--            <groupId>com.cjsg</groupId>-->
<!--            <artifactId>admin</artifactId>-->
<!--            <version>1.0</version>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>com.cjsg</groupId>-->
<!--            <artifactId>common</artifactId>-->
<!--            <version>1.0</version>-->
<!--        </dependency>-->
    </dependencies>

    <modules>
        <module>admin</module>
        <module>common</module>
    </modules>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>



这个是我的目录结构:

img

试了很多方法尝试解决,网上搜到的修改pom冲突依赖,给启动类
@SpringBootApplication加(exclude = DataSourceAutoConfiguration.class),给pom中的build引入yml路径之类的,但是都不好使,希望各位帮忙看一下,可能问题出在哪!感谢!

  • 写回答

1条回答 默认 最新

  • 皮蛋不吃粥 2022-06-23 13:12
    关注
    
    <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    

    这依赖在Mybatis的Starter里面已经有,项目package是否有问题

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    1人已打赏

报告相同问题?

问题事件

  • 系统已结题 7月1日
  • 已采纳回答 6月23日
  • 创建了问题 6月23日

悬赏问题

  • ¥15 h3.6m 人类行为预测论文复现
  • ¥50 wordpress项目注册报失败刷新后其实是成功状态,请求排查原因
  • ¥20 linxu服务器僵尸进程不释放,代码如何修改?
  • ¥15 pycharm激活不成功
  • ¥40 如果update 一个列名为参数的value
  • ¥15 基于51单片机的水位检测系统设计中LCD1602一直不显示
  • ¥15 OCS2安装出现问题,请大家给点意见
  • ¥15 ros小车启动launch文件报错
  • ¥15 vs2015到期想登陆但是登陆不上
  • ¥15 IPQ5018制作烧录固件,boot运行失败(操作系统-linux)(相关搜索:操作系统)(相关搜索:操作系统)