创建springboot多模块项目
前言
对于业务不复杂的项目,各层写在一起比较方便,结构也很清晰,如同这样
对于业务结构稍微复杂的项目,可以考虑进行模块拆分,抽取出公共的模块,模块与模块之间通过继承和聚合,相互关联,实现代码的复用,比如我们需要给客户做一个订票系统,这个系统包括了管理端、客户端、移动端,我们就可以搭建多模块项目,将entity、mapper、service拆分成独立的模块(如果有其他的业务组件也可以单独拆开成对应的模块),这样我们的管理端、移动端和客户端都可以依赖这些模块,话不多说,让我们根据这个思路来进行项目的搭建:
1,新建一个springboot的父级工程,这里以IDEA为例:
选择spring Initializr
定义父工程的名称
生成的父工程结构如下,保留.idea 文件夹 、 pom.xml 文件、以及一个 *.iml 文件,其他的全部删掉:
修改父工程的打包类型:
2,创建子模块
创建子模块weige-domain、weige-dao、weige-service
1,weige-domain
最后点击完成
service模块和domain生成方法一样,注意weige-dao模块需要添加数据库连接的依赖,如下:
模块生成后项目结构如下图:
删掉子模块中的启动类和配置文件,weige-dao和weige-service与weige-domain相同,都删掉这两个文件:
3.构建模块之间的依赖关系;
(1)在父工程的pom.xml文件中添加子模块的声明:
<!--在父工程中声明子模块-->
<modules>
<module>weige-domain</module>
<module>weige-dao</module>
<module>weige-service</module>
<module>weige-web</module>
</modules>
(2).修改各个子模块的父级依赖为weige-parent
<!--在子模块中声明父级依赖-->
<parent>
<groupId>com</groupId>
<artifactId>weige-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
(3),模块之间添加依赖:
weige-dao模块依赖weige-domaim、weige-service依赖weige-dao;
weige-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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--在子模块中声明父级依赖-->
<parent>
<groupId>com</groupId>
<artifactId>weige-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com</groupId>
<artifactId>weige-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>weige-dao</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 添加 weige-domain 的依赖 -->
<dependency>
<groupId>com</groupId>
<artifactId>weige-domain</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
weige-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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--在子模块中声明父级依赖-->
<parent>
<groupId>com</groupId>
<artifactId>weige-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com</groupId>
<artifactId>weige-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>weige-service</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 添加 weige-dao 的依赖 -->
<dependency>
<groupId>com</groupId>
<artifactId>weige-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
父工程weige-parent工程的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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com</groupId>
<artifactId>weige-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>weige-parent</name>
<!--声明父模块打包类型为pom-->
<packaging>pom</packaging>
<description>Demo project for Spring Boot</description>
<!--在父工程中声明子模块-->
<modules>
<module>weige-domain</module>
<module>weige-dao</module>
<module>weige-service</module>
</modules>
<!--在父工程中声明各版本类型-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
到这里项目的基本骨架算是搭完了,下一节我们将模拟具体的场景,通过不同业务模块之间的相互协作,来体会多模块项目的优势。