Springboot多模块项目的构建

参考视频

springboot-模块化开发-项目搭建流程

  1. 先创建好你的Springboot项目

  2. 然后把当前项目的”.mvn,src,.gitignore,HELP,mvnw,mvnw.cmd”等文件删除

    要删除的文件

    01

    删除后的效果

    02

  3. 右键当前项目,选择“新建模块” ,分别新建(common, pojo,dao,service,web模块,所有模块均按以下操作进行)

    03

    把当前模块里的不必要文件删除

    04

    把当前模块里的resource里的文件的后缀由properties改成yml

    一个模块最终的样子

    05

  4. 新建模块名为entrance

    删除以下文件(src里面的不要删!)

    06

    重命名application.properties为application.yml

    最终布局

    07

  5. pom文件的修改

    主项目模块的pom.xml(需要根据你的具体布局来修改)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    <?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>

    <!-- 说明这个项目是继承自springboot3.1.4的 不用改-->
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.1.4</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!-- 说明这个项目的属性,其他模块要引用导入这个模块时要用到下面的groupID和artifactID和version 不用改-->
    <groupId>fun.aino3hree</groupId>
    <artifactId>AIEditor</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>AIEditor</name>
    <description>AIEditor</description>
    <!-- 新增 项目的打包方式-->
    <packaging>pom</packaging>

    <!-- 新增 你有多少模块下面就填多少模块的名称 -->
    <modules>
    <module>common</module>
    <module>pojo</module>
    <module>dao</module>
    <module>service</module>
    <module>web</module>
    <module>entrance</module>
    </modules>

    <!-- 说明这个项目用的java版本的 不用改 -->
    <properties>
    <java.version>17</java.version>
    </properties>

    <!-- 项目要用到的公共依赖就放这里面去 -->
    <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>

    <!-- 新增 新增web依赖-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    </dependencies>

    <!-- 构建项目用到的maven插件 不用改 -->
    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>

    </project>

    common,pojo,dao,service,web,entrance模块(所有模块)的pom.xml文件按以下注释思路来修改即可

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    <?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>

    <!-- 修改 当前模块的父依赖 把项目的pom.xml文件中的项目groupID等复制过来即可 每个模块都要改!-->
    <parent>
    <groupId>fun.aino3hree</groupId>
    <artifactId>AIEditor</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    </parent>

    <!-- 当前模块的描述 不用动-->
    <groupId>fun.aino3hree</groupId>
    <artifactId>common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>common</name>
    <description>common</description>

    <!-- 各个模块中的properties可以删除 当前已删除-->

    <!-- 当前模块需要什么依赖就加进来 不需要可以留空 -->
    <dependencies>
    </dependencies>

    <!-- 不需要构建插件 当前已删除 -->

    </project>

    注:以下模块的pom.xml需要增加以下内容

    web模块和entrance模块

    在dependencies 上面新增

    1
    <packaging>jar</packaging>

    在dependencies 下面新增

    1
    2
    3
    4
    5
    6
    7
    8
    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>
  6. pom文件添加必要依赖

    entrance模块的pom.xml

    添加在里面

    1
    2
    3
    4
    5
    <dependency>
    <groupId>fun.aino3hree</groupId>
    <artifactId>web</artifactId>
    <version>${project.parent.version}</version>
    </dependency>

    service模块的pom.xml

    1
    2
    3
    4
    5
    <dependency>
    <groupId>fun.aino3hree</groupId>
    <artifactId>dao</artifactId>
    <version>${project.parent.version}</version>
    </dependency>

    dao模块的pom.xml

    1
    2
    3
    4
    5
    <dependency>
    <groupId>fun.aino3hree</groupId>
    <artifactId>pojo</artifactId>
    <version>${project.parent.version}</version>
    </dependency>

    web模块的pom.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <dependency>
    <groupId>fun.aino3hree</groupId>
    <artifactId>service</artifactId>
    <version>${project.parent.version}</version>
    </dependency>
    <dependency>
    <groupId>fun.aino3hree</groupId>
    <artifactId>common</artifactId>
    <version>${project.parent.version}</version>
    </dependency>
  7. 在entrance模块中的主程序文件中添加以下注解,用于扫描包内的各模块

    value值为你的项目的包名

    1
    @ComponentScan(value = "fun.aino3hree")
  8. 测试接口的创建

    在web模块中创建controller包,并创建一个Hello类

    08

    Hello类文件内容

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    package fun.aino3hree.web.controller;

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    /**
    * @Description
    * @Author: Ho Kin
    * @Date: 2023/10/12 10:26 周四
    */
    @RestController
    public class Hello {
    @RequestMapping("/hello")
    public String helloSpringboot() {
    System.out.println("hello springboot!");
    return "hello springboot";
    }
    }

    启动entrance中的主程序文件

    09

    打开浏览器即可测试

    10

  9. pom文件的常用依赖

    pojo模块

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.3.1</version>
    </dependency>

    dao模块

    1
    2
    3
    4
    5
    ```

    ***service模块***

    ```xml

    web模块

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    ```

    ***common模块***

    ```xml
    <dependency>
    <groupId>com.alibaba.fastjson2</groupId>
    <artifactId>fastjson2</artifactId>
    <version>2.0.40</version>
    </dependency>

    entrance模块

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

  10. 其他

在entrance模块中的Application.yml文件中添加

1
2
3
4
5
6
7
8
server:
port: 9090
servlet:
context-path: /666

spring:
profiles:
active: common, dao, service

11