一,為什么要使用多模塊?
1,結構更清晰,方便管理
如果只是一個小項目當然沒有問題,
但如果功能越增越多則管理越來越復雜,
多模塊可以使項目中模塊間的結構分離
2,把項目划分成多個模塊后,
能夠方便模塊的復用
例如:web/api/管理后台
都會用到一些數據表,
對數據表的封裝(mapper)會是都需要復用的模塊
3,減少各個模塊對不必要功能的依賴,
4,不同的模塊可以由不同的工程師來維護,
避免重要的代碼被經驗不足的工程師改動受影響
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,演示項目的相關信息
1,項目地址:
https://github.com/liuhongdi/multimodule
2,項目說明:
我們創建兩個子模塊:
business模塊: pojo/mapper這些功能都在這個模塊中
web模塊: controller/service等功能在這個模塊中
3,項目結構:如圖:


三,創建一個空的父項目
1,新建一個空項目:


說明:項目group為:com.multimodule
artifact用: demo

依賴保持為空,點next

指定位置后點 finish
2,修改pom.xml
增加一行:
<packaging>pom</packaging>
說明:表示使用打包時使用maven的分模塊管理打包
新增module
<modules> <module>business</module> <module>web</module> </modules>
刪除pom.xml中的build這個tag
刪除pom.xml中的dependencies這個tag
說明:目的是各個module各自添加自己的依賴
附:最終的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.3.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.multimodule</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <packaging>pom</packaging> <modules> <module>business</module> <module>web</module> </modules> <properties> <java.version>11</java.version> </properties> </project>
3,因為父項目不需要實現其他功能,
我們刪除不需要用到的文件
(不刪除也不影響使用):
刪除src目錄
刪除mvnw
刪除mvnw.cmd
刪除help.md
刪除.mvn
四,創建子項目business:
1,創建模塊
在父項目上右擊->new->module


artifact命名為business

依賴選擇頁面,保留為空,點next

模塊名字和目錄,使用business,點finish
2,配置pom.xml
因為business不會被直接運行,
所以我們刪除它的 build這個tag
修改parent的值:
<parent> <groupId>com.multimodule</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent>
groupId,artifactId,version這三項信息,就用父項目的信息即可
3,刪除啟動文件
這個模塊不需要單獨啟動
所以刪除:src/main/java/com.multimodule.business/BusinessApplication.java
五,創建子模塊web:
1,創建web模塊
在父項目上右擊->new->module


group命名為 com.multimodule,
artifact命名為:web

依賴選中spring web,因為此模塊需要獨立運行

名字和位置命名為web,點finish
2,配置pom.xml
這個模塊會直接運行,所以不刪除build項
修改parent的值,值用父項目的相關信息即可:
<parent> <groupId>com.multimodule</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent>
增加對business模塊的依賴
<dependency> <groupId>com.multimodule</groupId> <artifactId>business</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>compile</scope> </dependency>
3,啟動文件不可刪除,
因為此模塊需要直接運行
六,配置文件說明:
1,web模塊/application.properties
#error server.error.include-stacktrace=always #errorlog logging.level.org.springframework.web=trace #mysql spring.datasource.url=jdbc:mysql://localhost:3306/store?characterEncoding=utf8&useSSL=false spring.datasource.username=root spring.datasource.password=lhddemo spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #mybatis mybatis.mapper-locations=classpath:/mapper/*Mapper.xml mybatis.type-aliases-package=com.example.demo.mapper mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
2,創建數據表的sql:
CREATE TABLE `goods` ( `goodsId` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id', `goodsName` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '' COMMENT 'name', `subject` varchar(200) NOT NULL DEFAULT '' COMMENT '標題', `price` decimal(15,2) NOT NULL DEFAULT '0.00' COMMENT '價格', `stock` int(11) NOT NULL DEFAULT '0' COMMENT 'stock', PRIMARY KEY (`goodsId`) ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='商品表'
七,java代碼說明:
1,business模塊/Goods.java
public class Goods { //商品id Long goodsId; public Long getGoodsId() { return this.goodsId; } public void setGoodsId(Long goodsId) { this.goodsId = goodsId; } //商品名稱 private String goodsName; public String getGoodsName() { return this.goodsName; } public void setGoodsName(String goodsName) { this.goodsName = goodsName; } //商品標題 private String subject; public String getSubject() { return this.subject; } public void setSubject(String subject) { this.subject = subject; } //商品價格 private BigDecimal price; public BigDecimal getPrice() { return this.price; } public void setPrice(BigDecimal price) { this.price = price; } //庫存 int stock; public int getStock() { return this.stock; } public void setStock(int stock) { this.stock = stock; } public String toString(){ return " Goods:goodsId=" + goodsId +" goodsName=" + goodsName+" subject=" + subject+" price=" + price+" stock=" + stock; } }
2,business模塊/GoodsMapper.java
@Repository @Mapper public interface GoodsMapper { //get all goods List<Goods> selectAllGoods(); }
3,business模塊/GoodsMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.multimodule.business.mapper.GoodsMapper"> <select id="selectAllGoods" resultType="com.multimodule.business.pojo.Goods"> select * from goods order by goodsId desc </select> </mapper>
4,web模塊/HomeController.java
@RestController @RequestMapping("/home") public class HomeController { @Resource private GoodsMapper goodsMapper;
//打印數據庫中所有的商品 @GetMapping("/home") public String all() { List<Goods> goodsList = goodsMapper.selectAllGoods(); String retStr = ""; for (Goods goodsOne : goodsList) { String oneStr = goodsOne.toString()+"<br/>"; //i++; retStr += oneStr; } return retStr; } }
八,測試運行
1,啟動:
注意此時需要在web模塊的啟動文件:WebApplication上右鍵->選擇: Run WebApplication
2,訪問:
http://127.0.0.1:8080/home/home
輸出如圖:
有數據返回,表示正常運行
九,查看spring boot版本:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.2.RELEASE)