前言
整篇文章包含了、基础搭建、项目打包、项目拆分运行、项目联合运行,着重查看
基础搭建
1.创建一个spring boot项目(保证创建项目能够运行)
![](https://img.haomeiwen.com/i15473174/ea37b772b3e56659.png)
![](https://img.haomeiwen.com/i15473174/d7d43c44a2b29f32.png)
![](https://img.haomeiwen.com/i15473174/becf3a7574d34bf3.png)
2.新建Spring boot module
![](https://img.haomeiwen.com/i15473174/3d3137cf3e22b501.png)
3.新建module重复上述步骤(步骤三不勾选),建三个module演示(称为子级项目)
![](https://img.haomeiwen.com/i15473174/09046e0c3b10c422.png)
4.删除多余文件
![](https://img.haomeiwen.com/i15473174/1cf3513a407f64ff.png)
5.重点。配置pom文件(我们接下来以module1作为入口项目,module2/3作为依赖项目,如何依赖看项目架构是什么样的,接下来叙述)
![](https://img.haomeiwen.com/i15473174/717f430e99bbae28.png)
6.先修改父级项目pom
- 修改前pom
<?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.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</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>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 修改后pom
<?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.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</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>
<scope>test</scope>
</dependency>
</dependencies>
<!--删除了build,添加了packaging并且改为pom-->
<packaging>pom</packaging>
<!--添加module-->
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>
</project>
- 主要
<!--删除了build,添加了packaging并且改为pom-->
<packaging>pom</packaging>
<!--添加module-->
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>
7.修改module1(注意、此处比较特殊,因为是作为入口)
- 修改前
<?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.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>module1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>module1</name>
<description>Demo project for Spring Boot</description>
<properties>
<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>
- 修改后
<?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.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>module1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>module1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--删除了多余依赖、由父级pom管理-->
<!--添加了module1依赖于module2依赖(此处注意,如果是个人框架mvc模式区分,module2可以看做service层,)-->
<!--module3可以看做dao层,那么此处仅有依赖module2,而module2依赖于module3。我们此处案列是需要拆分的所以看成是多人聚合项目)-->
<dependency>
<groupId>com.example</groupId>
<artifactId>module2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>module3</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 主要
<!--修改为父级-->
<parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!--删除了多余依赖、由父级pom管理-->
<!--添加了module1依赖于module2依赖(此处注意,如果是个人框架mvc模式区分,module2可以看做service层,)-->
<!--module3可以看做dao层,那么此处仅有依赖module2,而module2依赖于module3。我们此处案列是需要拆分的所以看成是多人聚合项目)-->
<dependency>
<groupId>com.example</groupId>
<artifactId>module2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>module3</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
8.修改module2(多人子项目2)
- 修改前pom
<?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.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>module2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>module2</name>
<description>Demo project for Spring Boot</description>
<properties>
<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>
- 修改后pom
<?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.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>module2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>module2</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--删除了多余依赖、由父级pom管理-->
</dependencies>
<!--删除build、或者改成不可执行jar包、二选一-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--此处修改-->
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 主要
<!--修改为父级-->
<parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!--删除了多余依赖、由父级pom管理-->
</dependencies>
<!--删除build、或者改成不可执行jar包、二选一-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--此处修改-->
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
9.修改module3,步骤与module2相同。
10.运行项目,删除其他入口(个人洁癖)
![](https://img.haomeiwen.com/i15473174/c560d93e4a34a0fa.png)
![](https://img.haomeiwen.com/i15473174/624882b4b1dee978.png)
11.增加一个基础接口,在入口子项目添加一个接口
![](https://img.haomeiwen.com/i15473174/b47d1cdc08fe1d74.png)
@RestController
public class Main1 {
@RequestMapping(value = "/")
public String _Start() {
return "项目已成功运行";
}
}
12.添加端口(端口是在入口项目添加)
![](https://img.haomeiwen.com/i15473174/4480877a49722a0c.png)
13.启动(端口被占用了改到了8001就不重新截图了)
![](https://img.haomeiwen.com/i15473174/643fd176f8c130b6.png)
14.这一步、基础搭建就已经完成了
项目打包
1.为了保证项目能够正常打包,请按照基础搭建一步一步来。(网上很多文章搭建不一样,最终很少有能一次成功就打包的,所以避免踩坑)
2.依次完成搭建后,项目右侧maven
![](https://img.haomeiwen.com/i15473174/a7ec60591c616464.png)
3.一堆测试后、项目成功打包
![](https://img.haomeiwen.com/i15473174/6091c30a398cc5ea.png)
4.因为module1是入口,所以jar包在module1子目录下
![](https://img.haomeiwen.com/i15473174/a435d7813dc901cf.png)
5.cmd命令运行
![](https://img.haomeiwen.com/i15473174/2a8dbf140cf9e565.png)
6.打包测试这里就结束了
项目拆分
1.既然是多人聚合项目(这里先不谈mvc这些关联模式),解耦性是非常高的,每一个子项目就是一个系统,比如登录系统,数据库管理系统等等,基本就是跟微服务大体一致。
2.既然没有很高的耦合性,那么这些module就是独立的jar包,以module2作为实列
3.在module2中新建一个接口
![](https://img.haomeiwen.com/i15473174/18c5fedf3b82277b.png)
4.运行项目,发现访问http://localhost:8001/module
并不能获取结果。
5.返回module1(入口项目)的启动模块中,替换@SpringBootApplication,将依赖模块也加入托管容器
@SpringBootApplication(scanBasePackages = "com.example")
6.再次运行项目,可以进行正常访问
![](https://img.haomeiwen.com/i15473174/ad91b1e2517fe339.png)
7.既然要进行项目拆分,那么我们必然会对module2进行打包,进入module2的pom文件。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--此处修改-->
<!-- <configuration>-->
<!-- <classifier>exec</classifier>-->
<!-- </configuration>-->
</plugin>
</plugins>
</build>
8.将build恢复原状,这时候我们在使用maven进行打包
![](https://img.haomeiwen.com/i15473174/b9719bff1d8dcada.png)
![](https://img.haomeiwen.com/i15473174/3c89697b3c13694b.png)
9.这里不再演示,(上述步骤记得在module2中配置新端口),该module2如果是但是系统模块,是非常方便单独拆分的,已经直接作为上线项目也可以直接引用到下一个项目(比如工具模块)
10.项目拆分也到此结束
单人项目,设计模式MVC
1.既然是单人项目(当然这里也可以不是),将项目拆分成server、dao、controller是不可避免的。
2.基础搭建上注释了依赖关系,这里重新讲解一下controller连接server,则server是依赖对象。dao被server所依赖。
3.修改基础搭建中的依赖关系(不做演示)
4.每个模块中写好逻辑(dao层处理数据库,server处理数据库逻辑,已经下发的数据等,controller处理下发)
5.跟一个项目一样@Autowired
注入,注意: 每个启动入口都要添加依赖的名称,不然@Autowired注入是找不到的
//演示
@SpringBootApplication(scanBasePackages = "com.example")
6.正常逻辑写项目(这里已经不能被拆分了,但是可以部分逻辑可以抽离用于下一个项目)
7.打包、上线、部署
结尾
- 有些不足之处希望指正
- 项目演示Demo地址
网友评论