欢迎您的访问
专注架构,Java,数据结构算法,Python技术分享

spring boot3多模块项目工程搭建-上(团队开发模板)

 

写在前面

本文介绍了springboot开发后端服务,多模块项目工程搭建。坚持看完相信对你有帮助。

同时欢迎订阅springboot系列专栏,持续分享spring boot的使用经验。

模块结构优缺点

多模块项目将代码分成多个子模块,每个模块可以单独构建和管理。通常适用于大型项目或团队,以及那些希望将不同的功能或服务进行解耦的场景。

优点

  • 模块化:代码解耦,每个模块负责特定的功能,易于维护。
  • 团队协作:不同的团队可以在不同的模块上工作,减少冲突。
  • 重用性:可以将模块打包成共享库,供其他项目使用。

缺点

  • 复杂性:项目结构更复杂,构建和部署过程可能需要更多配置。
  • 管理难度:需要更多的协调和沟通,以确保模块之间的兼容性。

模块介绍

  1. Common 模块

    • 职责:Common 模块通常包含各种通用的工具类、常量定义、异常类等。
    • 功能:
      • 存放通用的工具方法,如日期处理、字符串处理等。
      • 存放常量定义,如错误码、常量字符串、数据对象等。
      • 定义通用的异常类,如自定义的业务异常。
  2. API 模块

    •  职责:API 模块用于定义应用程序的外部接口,通常是 RESTful API 或 GraphQL 接口。
    • 功能:
      • 定义接口的请求和响应对象。
      • 定义接口的路径、请求方法和参数。
      • 提供接口文档和描述。
  3. Web 模块

    • 职责:Web 模块负责处理 HTTP 请求,将请求转发给 Service 模块处理,并返回响应给客户端。
    • 功能:
      • 处理请求参数的验证和转换。
      • 调用 Service 模块提供的服务进行业务逻辑处理。
      • 构建响应并返回给客户端。
  4. Service 模块

    • 职责:Service 模块包含应用程序的业务逻辑,负责处理业务规则和数据处理。
    • 功能:
      • 实现应用程序的业务逻辑。
      • 调用 DAO 模块提供的数据访问接口进行数据库操作。
      • 处理事务管理。
  5. DAO 模块

    • 职责:DAO 模块负责与数据库进行交互,执行数据访问操作。
    • 功能:
      • 提供数据访问对象(DAO)接口,定义对数据库的增删改查操作。
      • 实现 DAO 接口,执行 SQL 查询和更新操作。
      • 处理数据库连接和资源管理。

搭建步骤

1.创建 父Maven 项目

父工程在 Maven 项目中扮演着组织和管理子模块的角色。它通常是一个空的 POM(Project Object Model)文件,用于定义项目的基本信息和管理子模块的依赖关系。父工程可以统一管理子模块的版本号、依赖项和插件配置,从而简化项目的构建和维护过程。同时,父工程也可以提供一些公共的配置和资源,供子模块共享使用。

父项目不放任何代码用不到的文件全部删除

2.添加各模块

在项目中创建各个模块的子目录,如Common、API、Web、Service和DAO。

Common模块

任何依赖都先不用选,后续统一在pom.xml导入

删除无用文件:只保留src、.gitignore、pom.xml

src/main/java/·····内的启动类删除,src/test/java/····内的测试类删除

API模块、Web模块、Service模块和DAO模块

添加方法跟Common模块一样、依次添加即可。

仅web模块要保留启动类和测试类

添加完成如图:

删除无用文件如图:

3.配置父项目构建

在项目的根目录下的 pom.xml 文件中配置 Maven 构建,指定各个模块的依赖关系和构建顺序。

父项目pom.xml:

需要清理无用配置

设置打包方式为pom

导入子模块

完整pom.xml如下:

  1. <?xml version=”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.0</modelVersion>

  5. <groupId>org.springframework.boot</groupId>

  6. <artifactId>spring-boot-starter-parent</artifactId>

  7. <version>3.2.5</version>

  8. <groupId>com.mijiu</groupId>

  9. <artifactId>springboot-modules-template</artifactId>

  10. <version>0.0.1-SNAPSHOT</version>

  11. <name>springboot-modules-template</name>

  12. <description>springboot-modules-template</description>

  13. <java.version>17</java.version>

  14. <packaging>pom</packaging>

  15. <module>common</module>

  16. <module>api</module>

  17. <module>service</module>

  18. <module>dao</module>

  19. <module>web</module>

  20. <dependencyManagement>

  21. <groupId>com.mijiu</groupId>

  22. <artifactId>common</artifactId>

  23. <version>0.0.1-SNAPSHOT</version>

  24. </dependency>

  25. <groupId>com.mijiu</groupId>

  26. <artifactId>api</artifactId>

  27. <version>0.0.1-SNAPSHOT</version>

  28. </dependency>

  29. <groupId>com.mijiu</groupId>

  30. <artifactId>service</artifactId>

  31. <version>0.0.1-SNAPSHOT</version>

  32. </dependency>

  33. <groupId>com.mijiu</groupId>

  34. <artifactId>dao</artifactId>

  35. <version>0.0.1-SNAPSHOT</version>

  36. </dependency>

  37. <groupId>com.mijiu</groupId>

  38. <artifactId>web</artifactId>

  39. <version>0.0.1-SNAPSHOT</version>

  40. </dependency>

  41. </dependencyManagement>

 

4.配置Web模块构建

  • web模块继承父项目
  • web模块依赖service模块
  • web模块需要spring boot web组件

pom.xml:

  1. <?xml version=”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.0</modelVersion>

  5. <groupId>com.mijiu</groupId>

  6. <artifactId>springboot-modules-template</artifactId>

  7. <version>0.0.1-SNAPSHOT</version>

  8. <artifactId>web</artifactId>

  9. <version>0.0.1-SNAPSHOT</version>

  10. <description>web</description>

  11. <java.version>17</java.version>

  12. <groupId>org.springframework.boot</groupId>

  13. <artifactId>spring-boot-starter-web</artifactId>

  14. <groupId>org.springframework.boot</groupId>

  15. <artifactId>spring-boot-starter-test</artifactId>

  16. <scope>test</scope>

  17. <!– 引入service模块 –>

  18. <groupId>com.mijiu</groupId>

  19. <artifactId>service</artifactId>

  20. <groupId>org.springframework.boot</groupId>

  21. <artifactId>spring-boot-maven-plugin</artifactId>

 

5.配置Service模块构建

  • service模块继承父项目
  • service模块依赖dao模块

pom.xml:

  1. <?xml version=”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.0</modelVersion>

  5. <groupId>com.mijiu</groupId>

  6. <artifactId>springboot-modules-template</artifactId>

  7. <version>0.0.1-SNAPSHOT</version>

  8. <artifactId>service</artifactId>

  9. <version>0.0.1-SNAPSHOT</version>

  10. <description>service</description>

  11. <java.version>17</java.version>

  12. <groupId>com.mijiu</groupId>

  13. <artifactId>dao</artifactId>

 

6.配置DAO模块构建

  • dao模块继承父项目
  • dao模块依赖common模块
  1. <?xml version=”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.0</modelVersion>

  5. <groupId>com.mijiu</groupId>

  6. <artifactId>springboot-modules-template</artifactId>

  7. <version>0.0.1-SNAPSHOT</version>

  8. <artifactId>dao</artifactId>

  9. <version>0.0.1-SNAPSHOT</version>

  10. <description>dao</description>

  11. <java.version>17</java.version>

  12. <!– 引入common模块 –>

  13. <groupId>com.mijiu</groupId>

  14. <artifactId>common</artifactId>

 

7.配置API模块构建

  • api模块继承父项目
  • api模块依赖common模块
  1. <?xml version=”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.0</modelVersion>

  5. <groupId>com.mijiu</groupId>

  6. <artifactId>springboot-modules-template</artifactId>

  7. <version>0.0.1-SNAPSHOT</version>

  8. <artifactId>api</artifactId>

  9. <version>0.0.1-SNAPSHOT</version>

  10. <description>API</description>

  11. <java.version>17</java.version>

  12. <!– 引入common模块 –>

  13. <groupId>com.mijiu</groupId>

  14. <artifactId>common</artifactId>

 

8.配置Common模块构建

  • common模块继承父项目
  1. <?xml version=”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.0</modelVersion>

  5. <groupId>com.mijiu</groupId>

  6. <artifactId>springboot-modules-template</artifactId>

  7. <version>0.0.1-SNAPSHOT</version>

  8. <artifactId>common</artifactId>

  9. <version>0.0.1-SNAPSHOT</version>

  10. <description>common</description>

  11. <java.version>17</java.version>

 

9.启动类位置修改

再次强调只有web模块保留启动类和测试类!

原位置

修改后

10.编写测试接口

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

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

  3. public class TestController {

  4. @RequestMapping(“/hello”)

  5. public String hello() {

  6. return “Hello World!”;

 

11.打包测试

在此之前一定保证仅web模块保留启动类和测试类,其他模块一律删除。

打包成功

 12.启动项目测试接口

在web模块的启动类启动项目

启动成功

测试接口

写在最后

spring boot3多模块项目工程搭建基础搭建到这里就结束了。任何问题评论区或私信讨论,欢迎指正。

赞(0) 打赏
版权归原创作者所有,任何形式转载请联系作者;码农code之路 » spring boot3多模块项目工程搭建-上(团队开发模板)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏