Bootstrap

SpringBoot多模块

SpringBoot多模块项目搭建

一.模块分层(我知晓的两种)

重点:理清依赖关系

1.按系统功能模块划分

在这里插入图片描述

2.按entity层、service层、web(controller)层、common层划分

在这里插入图片描述

二.项目搭建(按第二种方法)

1.先建一个普通的SpringBoot项目,作为父模块

在这里插入图片描述

2.子模块创建

父项目名称->右键->new->moudle

在这里插入图片描述

依次创建entity层、service层、 web层、 common层子模块

在这里插入图片描述

3.项目创建完成整体结构图

在这里插入图片描述

删除多余的文件,启动类只留下web层的

在这里插入图片描述

rescource包创建在common层里,创一个index.html,以便验证启动成功还是失败

<html>
<body>
<h1>hello word!!!</h1>
<p>this is a html page</p>
</body>
</html>

4.调整pom.xml

1)在父项目中要确保声明了所有的子模块
<?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.example</groupId>
    <artifactId>multi-module</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>multi-module</name>
    <description>multi-module</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.13</spring-boot.version>
    </properties>
    <!-- 添加内容   -->
    <packaging>pom</packaging>
    <modules>
        <module>multi-common</module>
        <module>multi-entity</module>
        <module>multi-service</module>
        <module>multi-web</module>
    </modules>

    <dependencies>
        <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>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <!-- 添加内容   -->
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>multi-common</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>multi-entity</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>multi-service</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>multi-web</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
           
            <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.example.MultiModuleApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
2)调整子模块pom.xml

按照依赖关系调整

(1)multi-common包

<?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>com.example</groupId>
        <artifactId>multi-module</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>multi-common</artifactId>
    <description>通用工具类模块</description>
    <dependencies>
        <!--所有模块都需要使用的依赖-->
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <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>
    </dependencies>
</project>

(2)multi-entity包

<?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>com.example</groupId>
        <artifactId>multi-module</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>multi-entity</artifactId>
    <description>entity层</description>
    <dependencies>
        <!--依赖公共模块-->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>multi-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!--所有模块都需要使用的依赖-->
    </dependencies>
</project>

(3)multi-service包

<?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>com.example</groupId>
        <artifactId>multi-module</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>multi-service</artifactId>
    <description>service层</description>
    <dependencies>
        <!--依赖公共模块和entity层-->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>multi-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>multi-entity</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!--所有模块都需要使用的依赖-->
    </dependencies>

</project>

(4)multi-web包

<?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>com.example</groupId>
        <artifactId>multi-module</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>multi-web</artifactId>
    <description>web层</description>
    <dependencies>
        <!--依赖公共模块和service层-->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>multi-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>multi-service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!--所有模块都需要使用的依赖-->
    </dependencies>

</project>

5.启动MultiWebApplication启动类,访问http://localhost:8080/
在这里插入图片描述

启动成功,SpringBoot多模块项目搭建完成

在这里插入图片描述

;