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
-
-
<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>
-
<packaging>pom</packaging>
-
-
<parent>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-parent</artifactId>
-
<version>2.7.6</version>
-
<relativePath/> <!-- lookup parent from repository -->
-
</parent>
-
-
<groupId>com.app.info</groupId>
-
<artifactId>app-info</artifactId>
-
<version>${app.info.version}</version>
-
<name>app-info</name>
-
-
<modules>
-
<module>app-info-commom</module>
-
<module>app-info-model</module>
-
<module>app-info-dao</module>
-
<module>app-info-service</module>
-
<module>app-info-web</module>
-
</modules>
-
-
<properties>
-
<app.info.version>1.0.0</app.info.version>
-
</properties>
-
-
<dependencies>
-
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter</artifactId>
-
</dependency>
-
-
</dependencies>
-
-
<build>
-
<plugins>
-
<plugin>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-maven-plugin</artifactId>
-
</plugin>
-
</plugins>
-
</build>
-
-
</project>
app-info-commom下pom.xml
-
-
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
<parent>
-
<artifactId>app-info</artifactId>
-
<groupId>com.app.info</groupId>
-
<version>1.0.0</version>
-
</parent>
-
<modelVersion>4.0.0</modelVersion>
-
-
<artifactId>app-info-commom</artifactId>
-
<version>${app.info.version}</version>
-
-
-
</project>
app-info-model下pom.xml
-
-
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
<parent>
-
<artifactId>app-info</artifactId>
-
<groupId>com.app.info</groupId>
-
<version>1.0.0</version>
-
</parent>
-
<modelVersion>4.0.0</modelVersion>
-
-
<artifactId>app-info-model</artifactId>
-
<version>${app.info.version}</version>
-
-
<dependencies>
-
-
<dependency>
-
<groupId>com.app.info</groupId>
-
<artifactId>app-info-commom</artifactId>
-
<version>${app.info.version}</version>
-
</dependency>
-
-
</dependencies>
-
-
</project>
app-info-dao下pom.xml
-
-
<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>
-
<artifactId>app-info</artifactId>
-
<groupId>com.app.info</groupId>
-
<version>1.0.0</version>
-
</parent>
-
-
<artifactId>app-info-dao</artifactId>
-
<version>${app.info.version}</version>
-
-
<dependencies>
-
<dependency>
-
<groupId>com.app.info</groupId>
-
<artifactId>app-info-model</artifactId>
-
<version>${app.info.version}</version>
-
</dependency>
-
-
<dependency>
-
<groupId>org.mybatis.spring.boot</groupId>
-
<artifactId>mybatis-spring-boot-starter</artifactId>
-
<version>3.0.0</version>
-
</dependency>
-
-
<dependency>
-
<groupId>com.mysql</groupId>
-
<artifactId>mysql-connector-j</artifactId>
-
<scope>runtime</scope>
-
</dependency>
-
-
</dependencies>
-
-
</project>
app-info-service下pom.xml
-
-
<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>
-
<artifactId>app-info</artifactId>
-
<groupId>com.app.info</groupId>
-
<version>1.0.0</version>
-
</parent>
-
-
<artifactId>app-info-service</artifactId>
-
<version>${app.info.version}</version>
-
-
<dependencies>
-
-
<dependency>
-
<groupId>com.app.info</groupId>
-
<artifactId>app-info-dao</artifactId>
-
<version>${app.info.version}</version>
-
</dependency>
-
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-data-redis</artifactId>
-
</dependency>
-
-
</dependencies>
-
-
-
</project>
app-info-web下pom.xml
-
-
<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>
-
<artifactId>app-info</artifactId>
-
<groupId>com.app.info</groupId>
-
<version>1.0.0</version>
-
</parent>
-
-
<artifactId>app-info-web</artifactId>
-
<version>${app.info.version}</version>
-
-
<dependencies>
-
<dependency>
-
<groupId>com.app.info</groupId>
-
<artifactId>app-info-service</artifactId>
-
<version>${app.info.version}</version>
-
</dependency>
-
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-web</artifactId>
-
</dependency>
-
-
</dependencies>
-
-
<build>
-
<plugins>
-
<plugin>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-maven-plugin</artifactId>
-
</plugin>
-
</plugins>
-
</build>
-
-
</project>
app-info-web下application.properties
-
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
-
spring.datasource.url=xxxxxx
-
spring.datasource.username=xxxxxxx
-
spring.datasource.password=xxxxxx
-
# 指定为HikariDataSource
-
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
-
# 连接池名
-
spring.datasource.hikari.pool-name=HikariCP
-
# 最小空闲连接数
-
spring.datasource.hikari.minimum-idle=5
-
# 连接池最大连接数
-
spring.datasource.hikari.maximum-pool-size=20
-
# 空闲连接存活最大时间,默认10分钟
-
spring.datasource.hikari.idle-timeout=600000
-
# 数据库连接超时时间
-
spring.datasource.hikari.connection-timeout=60000
-
-
spring.redis.database=xxxxxx
-
spring.redis.host=xxxxx
-
spring.redis.port=xxxx
-
spring.redis.password=xxxxxx
-
# 最大连接数
-
spring.redis.jedis.pool.max-active=200
-
# 最大空闲
-
spring.redis.jedis.pool.max-idle=100
-
# 最小空闲
-
spring.redis.jedis.pool.min-idle=100
-
# 最大阻塞等待时间(负数表示没限制)
-
spring.redis.jedis.pool.max-wait=-1
-
# 连接超时时间
-
spring.redis.timeout=2000
启动服务
如果启动报错:【java: 错误: 无效的源发行版:17】,大概率是因为建项目的时候,springboot选的是3.0.0以上的版本,3.0.0需要jdk17支持,可以把springboot版本降到2.7.10或者2.7.6