使用idea完成多模块的搭建

使用idea完成多模块的搭建

一、实现父工程和子工程的连接

1.创建一个空项目

在这里插入图片描述

2.创建子模块

在这里插入图片描述

重复以上操作创建需要的子模块

在这里插入图片描述

3.删除多余启动项

在这里插入图片描述

4.在MainDemo中声明子模块

在这里插入图片描述

5.定义Maven项目的父工程

在五个子模块的pom文件空白出添加, 使用父项目来定义一组项目共同的配置信息,这样子项目就可以继承配置信息 ,具体的内容根据自己项目而定

 <parent>
        <groupId>com.example</groupId>
        <artifactId>MainDemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

例:

在这里插入图片描述

6.将父工程与子工程关联

  
    <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>


            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
                <version>2.5.3</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <version>2.5.3</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>3.1.6</version>
            </dependency>

            <!--            后台管理系统模块-->
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>demo-admin</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>

<!--            公共模块-->
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>demo-common</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>

<!--            入口-->
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>demo-server</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>

<!--            用户模块-->
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>demo-user</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>

<!--            web模块-->
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>demo-web</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

7.运行demo-server模块的启动项

出现springboot启动图标即完成

二、实现模块与模块之间的连接

1.子模块之间的联系

在这里插入图片描述

2.连接子模块

根据子模块的关系图实现连接,具体代码如下,以demo-admin为例子,与demo-common相关联

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

<!--            关联工具模块-->
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>demo-common</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
        </dependencies>

因为整个父工程的pom中依赖关系为声明,所以在子工程demo-service中需要额外添加以下代码

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</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-web</artifactId>
            <version>3.1.6</version>
        </dependency>

三.可能遇到的问题

在这里插入图片描述
解决方案
在这里插入图片描述

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
搭建一个多模块Spring Boot项目,可以按照以下步骤: 1. 创建一个Maven项目,并选择“Create a simple project(创建一个简单项目)”选项。 2. 在pom.xml文件中添加以下插件: ```xml <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> ``` 这个插件可以将Spring Boot项目打包成一个可执行的jar文件。 3. 在项目根目录下创建一个子模块,比如叫做“web”。可以使用以下Maven命令创建子模块: ``` mvn archetype:generate -DgroupId=com.example -DartifactId=web -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false ``` 这个命令会在根目录下创建一个名为“web”的子模块。 4. 在子模块的pom.xml文件中添加以下依赖: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> ``` 这个依赖会让子模块成为一个Spring Boot Web应用程序。 5. 在子模块中创建一个Spring Boot应用程序,比如叫做“WebApplication”。可以创建一个类似于以下的类: ```java @SpringBootApplication public class WebApplication { public static void main(String[] args) { SpringApplication.run(WebApplication.class, args); } } ``` 这个类使用Spring Boot的@SpringBootApplication注解,这个注解会自动配置Spring应用程序。 6. 在子模块中创建一个Controller,比如叫做“HelloController”。可以创建一个类似于以下的类: ```java @RestController public class HelloController { @GetMapping("/hello") public String hello() { return "Hello World!"; } } ``` 这个Controller定义了一个名为“/hello”的路由,并返回了一个字符串。 7. 运行项目。可以使用以下Maven命令运行项目: ``` mvn spring-boot:run ``` 这个命令会启动Spring Boot应用程序,并在控制台输出日志。可以在浏览器中访问“http://localhost:8080/hello”来测试应用程序。 8. 添加其他子模块。可以按照以上步骤添加其他子模块,比如一个数据库模块、一个服务模块等等。这样就可以将应用程序拆分成多个模块,每个模块负责不同的功能。 注意事项: - 每个子模块都应该有一个唯一的artifactId。 - 如果子模块之间有依赖关系,可以在pom.xml文件中添加相关的依赖。 - 可以在根目录的pom.xml文件中添加公共依赖,这些依赖会被所有子模块继承。 - 如果使用Spring Cloud等微服务框架,可以将每个子模块打包成一个独立的服务,然后使用注册中心来管理它们。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值