IDEA 搭建springboot多模块项目

首先说一下为什么要建多模块项目,其实很多项目在刚开始的时候,都是单结构应用,常见的几个分层(web层、service层、dao层)直接通过建不同的包名即可,但是随着业务发展,项目参与人员变多,业务变复杂,所有的代码都在一个结构下,就会变得不直观,同时耦合度可能比较高。另外一个问题就是,在多服务的场景下,要给外部服务提供接口了(比如要提供对外的dubbo接口),如果是单体结构,只能整个模块打个jar出去,不优雅,不然还得重新做多模块拆分,麻烦。还有一个问题,可能一些通用的类在好几个工程里都有,多模块结构可以把通用的类放到一起,打包出去给其它服务用。所以,对于可预见未来的中大型项目,最好是刚开始就直接多模块搭建,对于小型项目,单结构即可。

下面简单举个例子,在idea里建一个多模块的项目:

首先说一下例子的结构:

app-info
    └ app-info-commom
    └ app-info-model
    └ app-info-dao
    └ app-info-service
    └ app-info-web

各module依赖关系:

app-info-commom
        ↓
app-info-model
        ↓
app-info-dao
        ↓
app-info-service
        ↓
app-info-web

新建项目,packaging选择jar

下一步,这里不选任何依赖,因为这是最外层的父 module

建好的工程,只保留画红线的部分,其它的文件删掉

 

 

 这一步开始新建子module,首先建最底层的app-info-commom,选择maven即可

 groupId、artifactId填一下

app-info-commom下的pom.xml里<parent>应该是父module的信息

 ↑↑↑↑↑↑  app-info-model参考app-info-commom操作   ↑↑↑↑↑↑

 

下面新建app-info-dao,因为这里要导入mysql、mybatis相关的包,所以选择spring initializr

建好后,只保留红色部分的文件,其它都可以删掉

↑↑↑↑↑↑  app-info-service、app-info-web参考app-info-dao操作   ↑↑↑↑↑↑

 

 5个子module建好后,还需要处理的文件:

1、把app-info-service、app-info-dao里src下的初始化启动类删掉,只保留app-info-web的启动类

2、只保留app-info-dao、app-info-web模块里src下的resources,其它模块删掉

3、按照各module依赖关系,在对应子module的pom.xml里需要引入相关依赖(下面会把完整的pom.xml贴出来)

4、如果在建app-info-dao、app-info-service引入了redis或mysql的依赖,需要在app-info-web的application.properties加上mysql或redis的相关配置,不然启动不会成功

 

最终项目结构

 

下面把每个module的pom.xml贴出来

app-info下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. <packaging>pom</packaging>
  6.  
  7. <parent>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-parent</artifactId>
  10. <version>2.7.6</version>
  11. <relativePath/> <!-- lookup parent from repository -->
  12. </parent>
  13.  
  14. <groupId>com.app.info</groupId>
  15. <artifactId>app-info</artifactId>
  16. <version>${app.info.version}</version>
  17. <name>app-info</name>
  18.  
  19. <modules>
  20. <module>app-info-commom</module>
  21. <module>app-info-model</module>
  22. <module>app-info-dao</module>
  23. <module>app-info-service</module>
  24. <module>app-info-web</module>
  25. </modules>
  26.  
  27. <properties>
  28. <app.info.version>1.0.0</app.info.version>
  29. </properties>
  30.  
  31. <dependencies>
  32.  
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter</artifactId>
  36. </dependency>
  37.  
  38. </dependencies>
  39.  
  40. <build>
  41. <plugins>
  42. <plugin>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-maven-plugin</artifactId>
  45. </plugin>
  46. </plugins>
  47. </build>
  48.  
  49. </project>
 

app-info-commom下pom.xml

 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>app-info</artifactId>
  7. <groupId>com.app.info</groupId>
  8. <version>1.0.0</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11.  
  12. <artifactId>app-info-commom</artifactId>
  13. <version>${app.info.version}</version>
  14.  
  15.  
  16. </project>
 

app-info-model下pom.xml

 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>app-info</artifactId>
  7. <groupId>com.app.info</groupId>
  8. <version>1.0.0</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11.  
  12. <artifactId>app-info-model</artifactId>
  13. <version>${app.info.version}</version>
  14.  
  15. <dependencies>
  16.  
  17. <dependency>
  18. <groupId>com.app.info</groupId>
  19. <artifactId>app-info-commom</artifactId>
  20. <version>${app.info.version}</version>
  21. </dependency>
  22.  
  23. </dependencies>
  24.  
  25. </project>
 

app-info-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. <parent>
  6. <artifactId>app-info</artifactId>
  7. <groupId>com.app.info</groupId>
  8. <version>1.0.0</version>
  9. </parent>
  10.  
  11. <artifactId>app-info-dao</artifactId>
  12. <version>${app.info.version}</version>
  13.  
  14. <dependencies>
  15. <dependency>
  16. <groupId>com.app.info</groupId>
  17. <artifactId>app-info-model</artifactId>
  18. <version>${app.info.version}</version>
  19. </dependency>
  20.  
  21. <dependency>
  22. <groupId>org.mybatis.spring.boot</groupId>
  23. <artifactId>mybatis-spring-boot-starter</artifactId>
  24. <version>3.0.0</version>
  25. </dependency>
  26.  
  27. <dependency>
  28. <groupId>com.mysql</groupId>
  29. <artifactId>mysql-connector-j</artifactId>
  30. <scope>runtime</scope>
  31. </dependency>
  32.  
  33. </dependencies>
  34.  
  35. </project>
 

app-info-service下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. <parent>
  6. <artifactId>app-info</artifactId>
  7. <groupId>com.app.info</groupId>
  8. <version>1.0.0</version>
  9. </parent>
  10.  
  11. <artifactId>app-info-service</artifactId>
  12. <version>${app.info.version}</version>
  13.  
  14. <dependencies>
  15.  
  16. <dependency>
  17. <groupId>com.app.info</groupId>
  18. <artifactId>app-info-dao</artifactId>
  19. <version>${app.info.version}</version>
  20. </dependency>
  21.  
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-data-redis</artifactId>
  25. </dependency>
  26.  
  27. </dependencies>
  28.  
  29.  
  30. </project>
 

app-info-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. <parent>
  6. <artifactId>app-info</artifactId>
  7. <groupId>com.app.info</groupId>
  8. <version>1.0.0</version>
  9. </parent>
  10.  
  11. <artifactId>app-info-web</artifactId>
  12. <version>${app.info.version}</version>
  13.  
  14. <dependencies>
  15. <dependency>
  16. <groupId>com.app.info</groupId>
  17. <artifactId>app-info-service</artifactId>
  18. <version>${app.info.version}</version>
  19. </dependency>
  20.  
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25.  
  26. </dependencies>
  27.  
  28. <build>
  29. <plugins>
  30. <plugin>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-maven-plugin</artifactId>
  33. </plugin>
  34. </plugins>
  35. </build>
  36.  
  37. </project>
 

app-info-web下application.properties

 
  1. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  2. spring.datasource.url=xxxxxx
  3. spring.datasource.username=xxxxxxx
  4. spring.datasource.password=xxxxxx
  5. # 指定为HikariDataSource
  6. spring.datasource.type=com.zaxxer.hikari.HikariDataSource
  7. # 连接池名
  8. spring.datasource.hikari.pool-name=HikariCP
  9. # 最小空闲连接数
  10. spring.datasource.hikari.minimum-idle=5
  11. # 连接池最大连接数
  12. spring.datasource.hikari.maximum-pool-size=20
  13. # 空闲连接存活最大时间,默认10分钟
  14. spring.datasource.hikari.idle-timeout=600000
  15. # 数据库连接超时时间
  16. spring.datasource.hikari.connection-timeout=60000
  17.  
  18. spring.redis.database=xxxxxx
  19. spring.redis.host=xxxxx
  20. spring.redis.port=xxxx
  21. spring.redis.password=xxxxxx
  22. # 最大连接数
  23. spring.redis.jedis.pool.max-active=200
  24. # 最大空闲
  25. spring.redis.jedis.pool.max-idle=100
  26. # 最小空闲
  27. spring.redis.jedis.pool.min-idle=100
  28. # 最大阻塞等待时间(负数表示没限制)
  29. spring.redis.jedis.pool.max-wait=-1
  30. # 连接超时时间
  31. spring.redis.timeout=2000
 

启动服务

 

如果启动报错:【java: 错误: 无效的源发行版:17】,大概率是因为建项目的时候,springboot选的是3.0.0以上的版本,3.0.0需要jdk17支持,可以把springboot版本降到2.7.10或者2.7.6

IDEA 搭建springboot多模块项目_idea创建多模块springboot项目-CSDN博客

posted @ 2024-03-26 10:01  CharyGao  阅读(60)  评论(0编辑  收藏  举报