什么是多模块管理?
多模块管理简单地理解就是一个 Java 工程项目中不止有一个 pom.xml 文件,会在不同的目录中有多个这样的文件,进而实现 Maven 的多模块管理
为什么要使用多模块管理?
随着业务的增长,代码会出现以下问题:
- 不同业务之间的代码互相耦合,难以区分且快速定位问题
- 增加开发成本,入手难度增高
- 开发界线模糊,不易定位到具体负责人
- 对于有特殊需求的模块无法拆解,比如:上传 maven 仓库只需要部分代码即可,但由于只有 1 个模块,不得不全部上传
故而拆分模块之后,可以避免上述问题
模块拆分方案
一般有以下两种拆分方案
按照结构进行拆分
- project
- project-service
- project-controller
- project-dao
- project
按照业务进行拆分
- project
- project-order
- project-account
- project-pay
- project
实际项目结构
这里我们使用按照结构拆分的方法进行拆分
help-student-common:放一些所有模块都会用到的共用的部分 help-student-dao:放主要的dao层代码 help-student-entity:放实体类代码 help-student-service:放主要的业务逻辑代码 help-student-web:放web端的代码
需要注意的是父工程中pom.xml
文件的配置,其决定了父子模块之间的依赖关系
1、help-student的pom.xml
<?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>
<packaging>pom</packaging>
<modules>
<module>help-student-web</module>
<module>help-student-entity</module>
<module>help-student-dao</module>
<module>help-student-service</module>
<module>help-student-common</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ctguyxr</groupId>
<artifactId>help-student</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>help-student</name>
<description>help-student</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.ctguyxr</groupId>
<artifactId>help-student-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.ctguyxr</groupId>
<artifactId>help-student-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.ctguyxr</groupId>
<artifactId>help-student-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
</plugins>
</build>
</project>
2、help-student-web的pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>help-student</artifactId>
<groupId>com.ctguyxr</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>help-student-web</artifactId>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.ctguyxr</groupId>
<artifactId>help-student-entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.ctguyxr</groupId>
<artifactId>help-student-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.ctguyxr</groupId>
<artifactId>help-student-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<!--因为主启动类在这个包下,所以需要加上打包插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.2</version>
</plugin>
</plugins>
</build>
</project>
3、模块之间的依赖关系
4、help-student-dao的pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>help-student</artifactId>
<groupId>com.ctguyxr</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>help-student-dao</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.ctguyxr</groupId>
<artifactId>help-student-entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
</plugins>
</build>
</project>
5、help-student-service的pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>help-student</artifactId>
<groupId>com.ctguyxr</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>help-student-service</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.ctguyxr</groupId>
<artifactId>help-student-entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.ctguyxr</groupId>
<artifactId>help-student-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
</plugins>
</build>
</project>
6、help-student-entity的pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>help-student</artifactId>
<groupId>com.ctguyxr</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>help-student-entity</artifactId>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
</plugins>
</build>
</project>
项目如何启动
http://www.javashuo.com/article/p-momaobrc-hc.html
springboot项目打包时提示“程序包xxx不存在,找不到符号” - 夏冬青 - 博客园
SpringBoot默认只会扫描当前包以及当前包下的所有组件,我们多模块的编写的时候就需要配置扫描包的路径,指明需要去哪些包下面扫描我们的组件
需要用到以下几个注解:
@EnableJpaRepositorie
用来扫描和发现指定包以及其子包中Repository
组件的定义
Springboot应用中@EntityScan和@EnableJpaRepositories的用法 - 星朝 - 博客园
@EntityScan
配置实体类的扫描路径,扫描指定包和其子包中所有Entity
的定义@ComponentScan
配置组件的扫描路径
由于是多模块开发,启动类和我们的其他组件并不在一个模块中,所以需要进行配置
package com.ctguyxr.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
/**
* Created By Intellij IDEA
*
* @author Xinrui Yu
* @date 2021/12/23 20:03 星期四
*/
@EnableJpaRepositories(basePackages = {"com.ctguyxr.dao"})
@EntityScan(basePackages = {"com.ctguyxr.entity"})
@ComponentScan(basePackages = {"com.ctguyxr.service.impl","com.ctguyxr.web.controller"})
@SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class,args);
}
}
在启动类对应的模块下编写SpringBoot
的配置文件application.properties
,配置好数据库和JPA
的相关配置信息。创建好测试用的controller
运行,测试:
测试接口,接口也能正常响应,说明多模块项目已经搭建成功!!
打包的时候需要指定启动主类,在pom.xml
中配置我们的mainClass
即可
maven打包,配置中指定主类 - lshan - 博客园