日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

idea 多模块build_[史上最详细]springboot创建基于maven的多模块项目

發(fā)布時(shí)間:2023/12/10 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 idea 多模块build_[史上最详细]springboot创建基于maven的多模块项目 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
點(diǎn)擊藍(lán)字 關(guān)注我們?

背景

項(xiàng)目為什么需要用多模塊?springmvc難道還不夠我們平常使用嗎?

  • 設(shè)計(jì)模式真言:“高內(nèi)聚、低耦合”,springmvc項(xiàng)目,一般會(huì)把項(xiàng)目分成多個(gè)包:controller、service、dao、util等,但是隨著項(xiàng)目的復(fù)雜性提高,想復(fù)用其他一個(gè)模塊的話,因?yàn)槭前男问?#xff0c;剝離出來會(huì)比較困難,耦合性有點(diǎn)強(qiáng),常用的方法就是復(fù)制代碼修改,但是這樣會(huì)做很多無用功與增加出錯(cuò)幾率。

  • springboot多模塊簡單來說,就是把按包分模塊的模式,借助maven升級到j(luò)ar的方式,抽象性更加強(qiáng)了,假如jar再升級到到war或者多個(gè)集合jar,就成微服務(wù)了,在多模塊jar模式下可以將某個(gè)jar拿出來對外共用,能大大提高代碼復(fù)用率與開發(fā)效率。

  • 話不多說開搞

    springboot多模塊創(chuàng)建

    父模塊創(chuàng)建

    • 打開idea:選擇Create New Project

    或者去官網(wǎng)創(chuàng)建(跟idea創(chuàng)建項(xiàng)目是一樣的,我這里用的是idea)

    • 然后選擇Spring Initializr

    • 點(diǎn)擊next之后—>基本設(shè)置

    • 點(diǎn)擊next之后->添加依賴

    • 點(diǎn)擊next之后->選擇項(xiàng)目地址

    • 點(diǎn)擊finish之后

    子模塊創(chuàng)建

    • 父項(xiàng)目名稱->右鍵->new->moudle

    • 點(diǎn)擊Spring Initializr(選擇合適jdk版本)->next

    • 點(diǎn)擊next之后->設(shè)置Maven Project 而不是跟父項(xiàng)目相同的Maven Pom

    • 點(diǎn)擊next之后->添加依賴

    • 點(diǎn)擊next->選擇確認(rèn)項(xiàng)目地址

    • 點(diǎn)擊finish完成

    然后再創(chuàng)建一個(gè)子模塊multi-core 過程跟multi-controller一樣,我這里就省略了。我們這里就暫時(shí)創(chuàng)建兩個(gè)子模塊。

    • 項(xiàng)目創(chuàng)建完成整體結(jié)構(gòu)圖

    注:由于程序的主入口是multi-controller 所有 multi-core里面 application.properties MultiCoreApplication.java 文件都刪除了

    修改pom文件

    1.修改父項(xiàng)目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.0modelVersion>
    ????<parent>
    ????????<groupId>org.springframework.bootgroupId>
    ????????<artifactId>spring-boot-starter-parentartifactId>
    ????????<version>2.4.1version>
    ????????<relativePath/>?
    ????parent>
    ????<groupId>com.tinygraygroupId>
    ????<artifactId>multi-parentartifactId>
    ????<version>0.0.1-SNAPSHOTversion>
    ????<name>multi-parentname>
    ????<description>Demo?project?for?Spring?Bootdescription>
    ????
    ????<packaging>pompackaging>
    ????
    ????<modules>
    ????????<module>multi-controllermodule>
    ????????<module>multi-coremodule>
    ????modules>
    ????
    ????<properties>
    ????????<java.version>1.8java.version>
    ????properties>

    ????<dependencies>
    ????????
    ????????<dependency>
    ????????????<groupId>org.springframework.bootgroupId>
    ????????????<artifactId>spring-boot-starter-webartifactId>
    ????????dependency>
    ????????<dependency>
    ????????????<groupId>org.springframework.bootgroupId>
    ????????????<artifactId>spring-boot-starterartifactId>
    ????????dependency>
    ????????<dependency>
    ????????????<groupId>org.projectlombokgroupId>
    ????????????<artifactId>lombokartifactId>
    ????????????<optional>trueoptional>
    ????????dependency>
    ????????<dependency>
    ????????????<groupId>org.springframework.bootgroupId>
    ????????????<artifactId>spring-boot-starter-testartifactId>
    ????????????<scope>testscope>
    ????????dependency>
    ????????<dependency>
    ????????????
    ????????????<groupId>org.springframework.bootgroupId>
    ????????????<artifactId>spring-boot-testartifactId>
    ????????????<version>2.0.1.RELEASEversion>
    ????????????<scope>testscope>
    ????????dependency>
    ????dependencies>

    project>

    2.修改子項(xiàng)目pom

    • multi-controller

    <?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.0modelVersion>

    ????<artifactId>multi-controllerartifactId>
    ????<version>0.0.1-SNAPSHOTversion>
    ????<name>multi-controllername>
    ????<description>Demo?project?for?Spring?Bootdescription>
    ????
    ????<parent>
    ????????<artifactId>multi-parentartifactId>
    ????????<groupId>com.tinygraygroupId>
    ????????<version>0.0.1-SNAPSHOTversion>
    ????parent>
    ???

    ????<dependencies>
    ????????
    ????????<dependency>
    ????????????<groupId>com.tinygraygroupId>
    ????????????<artifactId>multi-coreartifactId>
    ????????????<version>0.0.1-SNAPSHOTversion>
    ????????dependency>
    ????dependencies>

    ????<build>
    ????????<plugins>
    ????????????<plugin>
    ????????????????<groupId>org.springframework.bootgroupId>
    ????????????????<artifactId>spring-boot-maven-pluginartifactId>
    ????????????????<configuration>
    ????????????????????<mainClass>com.tinygray.multicontroller.MultiControllerApplicationmainClass>
    ????????????????????<layout>JARlayout>
    ????????????????configuration>
    ????????????plugin>
    ????????plugins>
    ????build>

    project>
    • multi-core

    <?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.0modelVersion>

    ????<artifactId>multi-coreartifactId>
    ????<version>0.0.1-SNAPSHOTversion>
    ????<name>multi-corename>
    ????<description>Demo?project?for?Spring?Bootdescription>
    ????
    ????<parent>
    ????????<artifactId>multi-parentartifactId>
    ????????<groupId>com.tinygraygroupId>
    ????????<version>0.0.1-SNAPSHOTversion>
    ????parent>
    ????

    ????<dependencies>
    ????????
    ????dependencies>

    project>

    springboot多模塊創(chuàng)建完成之后驗(yàn)證啟動(dòng)

    驗(yàn)證

    你如何知道你的多模塊項(xiàng)目搭建完成并能成功啟動(dòng)了呢?看下圖:

    出現(xiàn)以上圖片結(jié)果就是你的多模塊項(xiàng)目創(chuàng)建完成了并可以啟動(dòng)了

    啟動(dòng)

    • 找到啟動(dòng)類

    • 執(zhí)行啟動(dòng)類

    • 執(zhí)行成功

    • 瀏覽器打開?http://localhost:8080

    • 出現(xiàn)以上結(jié)果表示多模塊項(xiàng)目已經(jīng)搭建完成了

    寫一個(gè)測試接口訪問

    • 創(chuàng)建兩個(gè)java文件(一個(gè)實(shí)體類User一個(gè)UserController)

    • User.java、UserController.java文件內(nèi)容-很簡單一個(gè)測試接口

    • 瀏覽器輸入http://localhost:8080/user/getUserInfo

    • 出現(xiàn)以上結(jié)果就表示測試成功了。

    • 項(xiàng)目地址:

      • 搜索公眾號:Madison龍少

      • 回復(fù)?springboot-multi-module?獲取資源

    結(jié)束語

    掃碼關(guān)注我們

    公眾號|Madison龍少

    微信號|公眾號點(diǎn)擊'聯(lián)系方式'菜單獲取

    期待你的

    分享

    點(diǎn)贊

    在看

    總結(jié)

    以上是生活随笔為你收集整理的idea 多模块build_[史上最详细]springboot创建基于maven的多模块项目的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。