<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # SpringBoot+Maven多模塊項目(創建、依賴、打包可執行jar包部署測試)完整流程 原文鏈接: https://zhuanlan.zhihu.com/p/471117796 開發環境:IDEA,SprngBoot 2.0.4,Maven 2.19.1 工程結構: 父工程father 子模塊 dao (用于持久化數據跟數據庫交互) 子模塊 entity (實體類) 子模塊 service (處理業務邏輯) 子模塊 web (頁面交互接收、傳遞數據,唯一有啟動類的模塊) 關系: web依賴 service、dao、entity service依賴 dao、entity dao依賴 entity entity誰都不依賴,獨立的 這里我用比較常見的工程結構舉例說明,有些公司的項目可能會把模塊分的很細,或者會有兩個程序入口,也就是兩個可以啟動的模塊!這個我在文章最后會做說明!縷清了思路其實沒那么復雜! ### 一、創建Maven多模塊項目 1.先建立外層父工程 File →new →project 選擇Spring Initializr Next下一步到以下頁面 2.接下來,把src整個刪掉,父工程不需要,因為父工程你就當它只有一個外殼就完了 3.接下來創建子模塊 工程上右鍵 → new → Module 選擇Spring Initaializr 下一步 4.重復以上動作,創建dao模塊,service模塊,web模塊 5.service模塊和entity模塊一樣什么都不需要引入 6.dao模塊和web模塊可以根據實際需求選擇引入mysql,mybatis,redis,web這些, 7.刪除每個子模塊中沒用的文件,.mvn、.gitignore、daoiml、mvnw、mvnw.cmd文件只留下pom.xml 8.刪除除了web模塊以外其它模塊中的Applicatin啟動項,和resources目錄下的application.properties配置文件 以上動作操作完成以后如果你發現你的子模塊變成了文件夾,沒關系,找到Maven Projects刷新一下就好了 整理過后的項目結構是這樣的 ![](https://img.kancloud.cn/38/00/3800167a63c6e29056fc026b4597aac1_701x809.png) 以上項目的基本結構就完成了,接下來建立各自依賴 ### 二、依賴關系 打開父pom.xml修改打包方式jar為pom,注意:build內容也需要做替換,因為默認的spring-boot-maven-plugin這種方式,等到后期打包的時候他會一直提示你,你引入的依賴不存在!代碼如下 ~~~text <?xml version="1.0" encoding="UTF-8"?> <project xmlns="Maven - Page Not Found" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="Maven - Page Not Found http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!--父pom.xml--> <groupId>com.miu</groupId> <artifactId>father</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>father</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <!--聲明你有四個兒子 --> <modules> <module>entity</module> <module>dao</module> <module>service</module> <module>web</module> </modules> <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.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <configuration> <skipTests>true</skipTests> <!--默認關掉單元測試 --> </configuration> </plugin> </plugins> </build> </project> ~~~ **這里有個坑需要注意,dao、service、entity這三個模塊的pom.xml文件中不需要build 內容,直接干掉** entity 的 pom.xml 內容 ~~~text <?xml version="1.0" encoding="UTF-8"?> <project xmlns="Maven - Page Not Found" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="Maven - Page Not Found http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.miu</groupId> <artifactId>entity</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>entity</name> <description>Demo project for Spring Boot</description> <!--聲明父模塊--> <parent> <groupId>com.miu</groupId> <artifactId>father</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <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> </project> ~~~ dao 的 pom.xml 內容 ~~~text <?xml version="1.0" encoding="UTF-8"?> <project xmlns="Maven - Page Not Found" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="Maven - Page Not Found http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!--dao 模塊 pom.xml--> <groupId>com.miu</groupId> <artifactId>dao</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>dao</name> <description>Demo project for Spring Boot</description> <!--聲明父模塊--> <parent> <groupId>com.miu</groupId> <artifactId>father</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <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-data-redis</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</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> <!--dao 模塊 引入entity模塊--> <dependency> <groupId>com.miu</groupId> <artifactId>entity</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project> ~~~ service 模塊的 pom.xml 內容 ~~~text <?xml version="1.0" encoding="UTF-8"?> <project xmlns="Maven - Page Not Found" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="Maven - Page Not Found http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.miu</groupId> <artifactId>service</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>service</name> <description>Demo project for Spring Boot</description> <!--聲明父模塊--> <parent> <groupId>com.miu</groupId> <artifactId>father</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <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> <!--service模塊 引入entity模塊--> <dependency> <groupId>com.miu</groupId> <artifactId>entity</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!--service模塊 引入dao模塊--> <dependency> <groupId>com.miu</groupId> <artifactId>dao</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project> ~~~ web模塊的 pom.xml 內容 **注意build部分,因為web模塊作為程序的入口啟動,所以它需要打包,并且要指定Main Class** ~~~text <?xml version="1.0" encoding="UTF-8"?> <project xmlns="Maven - Page Not Found" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="Maven - Page Not Found http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.miu</groupId> <artifactId>web</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>web</name> <description>Demo project for Spring Boot</description> <!--聲明父模塊--> <parent> <groupId>com.miu</groupId> <artifactId>father</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <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-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</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> <!--web模塊 引入entity模塊--> <dependency> <groupId>com.miu</groupId> <artifactId>entity</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!--web模塊 引入service模塊--> <dependency> <groupId>com.miu</groupId> <artifactId>service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!--web模塊 引入dao模塊--> <dependency> <groupId>com.miu</groupId> <artifactId>dao</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!-- 指定該Main Class為全局的唯一入口 --> <mainClass>com.miu.web.WebApplication</mainClass> <layout>ZIP</layout> </configuration> <executions> <execution> <goals> <!--可以把依賴的包都打包到生成的Jar包中--> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> ~~~ 到此為止所有的依賴全部完成!接下來就是測試!這里只用簡單的測試來實驗! 最后把web模塊中的application.properties文件補充一下就OK了,因為引入了mysql,redis等配置,所以數據源是要配的,不然運行起來會報錯找不到數據源! ~~~text server.port=8080 #-----------------------------------數據庫配置---------------------------------------- spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=123 #------------------------------------redis配置--------------------------------------- spring.redis.database=0 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password= spring.redis.jedis.pool.max-active=8 spring.redis.jedis.pool.max-idle=8 spring.redis.jedis.pool.max-wait=-1ms spring.redis.jedis.pool.min-idle=0 spring.redis.timeout=10000ms ~~~ 一切準備就緒,開始運行web模塊下的啟動類進行測試 ### 三、打包可執行jar 看到上面的頁面就證明模塊之間的依賴沒有問題,調用正常,我這里是用簡單的創建對象的這種方式來操作的,實際開發并不是這種操作,大部分都是通過 @Autowired 注解 來實現的注入,這里我就不做演示了,只要模塊之間調用沒問題,剩下的就是鋪代碼的事了,接下來還有最后一個打包問題,為什么要啰嗦那么多還要說打包問題呢,因為我建議在項目架構之初,除了搭框架以外,最好是在最開始的時候就測試一下打包,尤其是這種多模塊項目之間各種依賴的這種工程的打包,如果等你代碼寫的鋪天蓋地的時候你在去想怎么打包,到時候有你頭疼的!如果你是按照我本章的流程一步步下來的話,那么你完全不用擔心打包問題,因為所有的pom.xml有已經配置好了,只需要動手運行 package打包動作就行了,第一次打包不需要clean,記住以后每次打包之前clean一下,關于為什么打jar包,不打war包這個問題,還有其它會遇到的問題,在文章最后會做說明! ![](https://img.kancloud.cn/d0/b7/d0b78900039d11c05c8c7bfb3aacb6ef_689x545.png) 雙擊運行package,看到BUILD SUCCESS 就證明打包成功了,如此簡單?告訴你就是這么簡單,前提是你的每一個模塊下的pom.xml要配置好,誰需要打包,誰不需要打包,誰依賴誰,父工程是否聲明了子模塊,子模塊是否聲明了父工程是誰,這些是重點! ![](https://img.kancloud.cn/1d/e0/1de070a53a31f715940c3e2a214d0228_676x169.png) 接下來去找你工程目錄,web文件夾下的target文件夾,剛才打包好的jar文件,就放在這里了 然后我把這個jar文件上傳到我的測試服務器,使用 java -jar web-0.0.1-SNAPSHOT.jar 命令來測試運行打包的可執行jar文件到底行不行! 運行成功,輸入我測試服務器地址測試也沒問題,到此為止全部搞定 ## 聚合工程舉一個簡單的例子: 整個工程你就當作一個公司,父工程(退休了什么也不干)只需要聲明有幾個兒子(子模塊)就完事了, 子模塊web聲明父工程是誰,就當他是大兒子,公司他管事,pom.xml文件需要打包,需要build配置,需要其它三個兄弟幫助 其它子模塊聲明父工程是誰,之間關系都是兄弟,不需要打包,哪里需要去哪里! 在此我說一下重點和需要注意的地方! 1.父pom.xml 打包方式,jar要更改為pom,build 需要更改 2.不需要打包的模塊pom.xml文件中不要寫,全刪掉,例如有些工程中的common模塊,utils模塊,entity模塊,service模 塊都不需要打包 3.聲明父工程時,填寫父工程位置../pom.xml 4.關于applicatin.properties配置文件,只需要在啟動的模塊中配置就可以了, 5.關于打包為什么打包jar包,不打war包,打war包目的是war包可以運行在tomcat下,但是SpringBoot是內置tomcat,如果你打war包,前提是干掉內置的tomcat,然后才能打包,各種麻煩,直接打包可執行jar包,使用java -jar 命令就可以完美的運行起來很方便! 6.真實開發中使用@Autowired 注解 來實現注入,而不是new對象這種方式,所以可能會產生注入以后報錯,是因為你的啟動類上沒有配置掃描,使用 @ComponentScan(basePackages = "你的路徑")注解來解決,如果你使用的持久層是Mybatis,那么你的mapper也需要掃描,在啟動類上使用 @MapperScan("你的mapper文件地址")注解來解決,算了還是貼個圖片吧 ![](https://img.kancloud.cn/ab/d4/abd45ff3439eb61bc324620deba07be2_547x257.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看