spring boot:構建多模組專案(spring boot 2.3.1)
阿新 • • 發佈:2020-08-03
一,為什麼要使用多模組?
1,結構更清晰,方便管理 如果只是一個小專案當然沒有問題, 但如果功能越增越多則管理越來越複雜, 多模組可以使專案中模組間的結構分離 2,把專案劃分成多個模組後, 能夠方便模組的複用 例如:web/api/管理後臺 都會用到一些資料表, 對資料表的封裝(mapper)會是都需要複用的模組 3,減少各個模組對不必要功能的依賴, 4,不同的模組可以由不同的工程師來維護, 避免重要的程式碼被經驗不足的工程師改動受影響說明:劉巨集締的架構森林是一個專注架構的部落格,地址:https://www.cnblogs.com/architectforest
對應的原始碼可以訪問這裡獲取: https://github.com/liuhongdi/
說明:作者:劉巨集締 郵箱: [email protected]
二,演示專案的相關資訊
1,專案地址:https://github.com/liuhongdi/multimodule2,專案說明: 我們建立兩個子模組: business模組: pojo/mapper這些功能都在這個模組中 web模組: controller/service等功能在這個模組中 3,專案結構:如圖:
三,建立一個空的父專案
1,新建一個空專案: 說明:專案group為:com.multimodule artifact用: demo 依賴保持為空,點next 指定位置後點 finish 2,修改pom.xml 增加一行: <packaging>pom</packaging> 說明:表示使用打包時使用maven的分模組管理打包 新增module<刪除pom.xml中的build這個tag 刪除pom.xml中的dependencies這個tag 說明:目的是各個module各自新增自己的依賴 附:最終的pom.xml:modules> <module>business</module> <module>web</module> </modules>
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"3,因為父專案不需要實現其他功能, 我們刪除不需要用到的檔案 (不刪除也不影響使用): 刪除src目錄 刪除mvnw 刪除mvnw.cmd 刪除help.md 刪除.mvnxmlns: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>
四,建立子專案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.javapublic 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)