• IDEA创建Springboot多模块项目


    一、创建父模块

            File --> New --> Project ,选择 “ Spring Initalizr ” ,点击 Next         

    Next

    Next --> Finish

    二、创建子模块

    右键根目录,New --> Module

    选择 “ Spring Initializr ”,点击Next

    此处注意Type选择Maven,Artifact根据自己的分模块的标准进行命名,可以按功能分模块(比如:订单、支付、登陆等模块分,就可以命名为order、pay、login等),此处我按照公共模块,业务模块、工具模块等模块进行划分的。

    Next

    Next --> Finish

    重复上述步骤,根据自身需要再创建其他子模块

    三、修改pom文件

          子模块

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.7.5version>
    9. <relativePath/>
    10. parent>
    11. <groupId>com.hnggroupId>
    12. <artifactId>businessartifactId>
    13. <version>0.0.1-SNAPSHOTversion>
    14. <name>businessname>
    15. <description>业务模块description>
    16. <properties>
    17. <java.version>8java.version>
    18. properties>
    19. <dependencies>
    20. <dependency>
    21. <groupId>org.springframework.bootgroupId>
    22. <artifactId>spring-boot-starter-webartifactId>
    23. dependency>
    24. <dependency>
    25. <groupId>org.springframework.bootgroupId>
    26. <artifactId>spring-boot-starter-testartifactId>
    27. <scope>testscope>
    28. dependency>
    29. dependencies>
    30. <build>
    31. <plugins>
    32. <plugin>
    33. <groupId>org.springframework.bootgroupId>
    34. <artifactId>spring-boot-maven-pluginartifactId>
    35. plugin>
    36. plugins>
    37. build>
    38. project>

     父模块

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <groupId>com.hnggroupId>
    6. <artifactId>serverartifactId>
    7. <version>0.0.1-SNAPSHOTversion>
    8. <name>servername>
    9. <description>父模块description>
    10. <packaging>pompackaging>
    11. <parent>
    12. <groupId>org.springframework.bootgroupId>
    13. <artifactId>spring-boot-starter-parentartifactId>
    14. <version>2.7.5version>
    15. <relativePath/>
    16. parent>
    17. <properties>
    18. <java.version>8java.version>
    19. properties>
    20. <modules>
    21. <module>commonmodule>
    22. <module>businessmodule>
    23. <module>systemmodule>
    24. <module>toolsmodule>
    25. modules>
    26. <dependencies>
    27. <dependency>
    28. <groupId>org.springframework.bootgroupId>
    29. <artifactId>spring-boot-starter-webartifactId>
    30. dependency>
    31. <dependency>
    32. <groupId>org.springframework.bootgroupId>
    33. <artifactId>spring-boot-starter-testartifactId>
    34. <scope>testscope>
    35. dependency>
    36. dependencies>
    37. <build>
    38. <plugins>
    39. <plugin>
    40. <groupId>org.springframework.bootgroupId>
    41. <artifactId>spring-boot-maven-pluginartifactId>
    42. plugin>
    43. plugins>
    44. build>
    45. project>

    四、删除多余不必要的文件及目录

            1.删除所有子模块的 mvnwmvnw.cmd、xxx.iml 文件及 .mvn 文件夹

                    

             2.删除所有子模块resources文件夹

                    

            3.删除所有子模块的的启动类

                    

    五、在主模块中添加测试代码,启动项目测试

            在主模块business中创建对应的包controller、service等,在controller创建测试代码

    1. package com.hng.business.controller;
    2. import org.springframework.web.bind.annotation.RequestMapping;
    3. import org.springframework.web.bind.annotation.RestController;
    4. /**
    5. * @Author 郝南过
    6. * @Date 2023/11/3 12:21
    7. * @Version 1.0
    8. */
    9. @RestController
    10. public class TestController {
    11. @RequestMapping("/test")
    12. public String hello(){
    13. return "Hello";
    14. }
    15. }

    使用主模块business的启动文件启动项目,访问localhost:8080/test,可以正常看到响应(默认端口为8080,如需要修改可在主模块business下的resources下的application.properties文件中进行配置修改)

  • 相关阅读:
    MIT 6.S081 Operating System Lecture4 (随意的笔记)
    IDEA取消git对项目的版本控制
    Java-集合
    攻防世界web篇-get_post
    springboot下添加全局异常处理和自定义异常处理
    程序员笔记本电脑选 windows 还是 MAC
    【语音识别入门】My-Voice-Analysis
    操作系统的基本特征
    携手同行,共赴未来......
    深度学习|如何确定 CUDA+PyTorch 版本
  • 原文地址:https://blog.csdn.net/shaogaiyue9745602/article/details/134186782