有天上飛的概念,就要有落地的實現java
- 概念十遍不如代碼一遍,朋友,但願你把文中全部的代碼案例都敲一遍
- 先贊後看,養成習慣
SpringBoot 圖文教程系列文章目錄mysql
本文已經收錄碼雲倉庫: https://gitee.com/bingqilinpe...
本文涉及源碼下載地址: https://gitee.com/bingqilinpe...
什麼是多模塊開發?如圖所示,項目中每個包對應都是一個完整的項目,在IDEA中稱之爲模塊,每個模塊都有完整的項目結構:獨立的pom文件,獨立的配置文件,獨立的編譯文件輸出模塊等等。git
那麼這樣項目結構的項目是如何設計出來的呢?web
技術選型:面試
- SpringBoot
- MybatisPlus MybatisPlus教程見:SpringBoot圖文教程11—今後不寫mapper文件「SpringBoot集成MybatisPlus」
- Mysql
多模塊開發效果圖以下:
spring
![]()
父級工程能夠用來統一管理全部項目的依賴,如圖,若是在父級項目中有一個mysql依賴,那麼全部繼承這個父級項目的子項目中也會繼承到mysql的依賴sql
1.建立一個Project數據庫
2.對IDEA作一些項目基本的配置apache
3.寫父級項目的pom文件mybatis
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- 配置SpringBoot的父級項目--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.lby</groupId> <artifactId>multi-module-demo</artifactId> <version>1.0-SNAPSHOT</version> <!-- packaging 父級項目的類型是pom --> <packaging>pom</packaging> <!-- properties 定義pom的全局變量 通常用於定義依賴的版本號 --> <properties> <java.version>1.8</java.version> <!-- lombok-version 這個標籤是自定義的能夠隨便寫 本質上就是一個變量 --> <lombok-version>1.18.4</lombok-version> </properties> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <!-- 版本號經過$獲取properties中定義的版本號--> <version>${lombok-version}</version> <scope>provided</scope> </dependency> </dependencies> <!-- dependencyManagement 在 dependencyManagement 中配置的依賴 只是一種聲明 聲明瞭版本號 不會被項目繼承下來【相關的jar包不會被子項目下載到項目中】 子項目若是想要繼承到dependencyManagement中的依賴 須要單獨在配置 只不過子項目若是繼承 dependencyManagement 中的依賴 能夠不寫版本號【子項目中依賴版本 按照父項目中dependencyManagement 中配置下載】 --> <dependencyManagement> <dependencies> <!--若是是Mybatis寫成Mybatis便可--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.1.tmp</version> </dependency> <!--數據源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.19</version> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> </dependencies> </dependencyManagement> </project>
注意:
dependencies 和 DependencyManagement 的區別
DependencyManagement 只是聲明依賴,並不實際引入,所以子項目須要顯式聲明須要用到的依賴
子項目開發的步驟以下:
- 基於Project建立module
- 修改pom
- 寫配置,沒有能夠不寫
- 寫代碼
1.基於Project建立module
建立完multi-entity後打開pom能夠看到
![]()
此時打開父級項目的pom 會看到
2.修改pom
完整的multi-entity項目的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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <!-- 子項目繼承父項目的配置 parent --> <parent> <artifactId>multi-module-demo</artifactId> <groupId>com.lby</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>multi-entity</artifactId> <dependencies> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency> </dependencies> </project>
3.在項目中寫入實體類
package com.lby.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @TableName("cmfz_admin") @Data public class Admin { /** * 主鍵屬性 @TableId * * value 該屬性對應的數據庫表中的字段名 * type 主鍵自增的類型 AUTO 表明自動遞增 */ @TableId(value = "id",type = IdType.AUTO) private Integer id; /** * 非主鍵屬性 @TableField * @TableField("username") 參數爲該屬性對應的數據庫表中的字段名 * */ private String username; private String password; private String salt; }
對於dao模塊而言,不一樣的地方在於,在multi-dao中須要使用到 multi-entity中的實體類,可是怎麼在dao模塊中使用到另外一個項目中的實體類呢?
將multi-entity像依賴同樣導入 multi-dao中
multi-dao完整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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>multi-module-demo</artifactId> <groupId>com.lby</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>multi-dao</artifactId> <dependencies> <!-- 在dao中引用entity 能夠在dao的代碼中直接導入entity中的類和方法 --> <dependency> <groupId>com.lby</groupId> <artifactId>multi-entity</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- 若是配置依賴在父項目的 dependencyManagement 有--> <!-- 能夠不寫版本號 版本號繼承父項目中的--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> </dependencies> </project>
在multi-dao寫入dao接口
package com.lby.dao; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.lby.entity.Admin; public interface AdminDao extends BaseMapper<Admin> { }
完整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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>multi-module-demo</artifactId> <groupId>com.lby</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>multi-service</artifactId> <dependencies> <!-- multi-service中須要引入 dao模塊--> <dependency> <groupId>com.lby</groupId> <artifactId>multi-dao</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
寫入業務類
package com.lby.service; import com.lby.dao.AdminDao; import com.lby.entity.Admin; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * 之因此可以直接使用@Service註解 * 是由於 multi-service 模塊 依賴了 multi-dao * multi-dao 依賴了 multi-entity * multi-entity 中的 MybatisPlus 依賴在項目中導入了Spring的jar包 */ @Service public class AdminService { @Autowired private AdminDao adminDao; public List<Admin> adminList(){ return adminDao.selectList(null); } }
multi-controller 模塊是啓動類所在的模塊
因此咱們把配置文件 啓動類以及SpringBoot集成maven的插件都放在這個項目中
1.完整的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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>multi-module-demo</artifactId> <groupId>com.lby</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>multi-controller</artifactId> <dependencies> <!-- controller中須要引入service--> <dependency> <groupId>com.lby</groupId> <artifactId>multi-service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- web--> <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> <!-- spring-boot-maven-plugin 插件配置在啓動類所在的模塊--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.application配置文件配置
#配置端口號 server: port: 8802 #數據源的配置 spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/cmfz?useUnicode=true&characterEncoding=UTF-8 username: root password: 123456 type: com.alibaba.druid.pool.DruidDataSource #mybatis-plus mybatis-plus: mapper-locations: classpath:mapper/*Mapper.xml # root 全局日誌等級 默認是info 能夠修改 debug warn error logging: level: root: info com.baizhi: debug
3.啓動類代碼
package com.lby; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @MapperScan("com.lby.dao") @SpringBootApplication public class AppRun { public static void main(String[] args) { SpringApplication.run(AppRun.class,args); } }
4.controller代碼
package com.lby.controller; import com.lby.entity.Admin; import com.lby.service.AdminService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class AdminController { @Autowired private AdminService adminService; @RequestMapping("adminList") public List<Admin> adminList(){ return adminService.adminList(); } }
經過啓動類啓動項目
訪問地址:http://localhost:8802/adminList 能夠看到以下效果
若是要使用插件啓動 須要先對父項目進行 clean 和 install操做
![]()
編寫測試類
運行測試方法 效果以下
注意:啓動類在哪一個模塊,就經過哪一個模塊打包
經過maven打包項目
找到打包好的項目 經過java -jar運行項目
訪問地址:http://localhost:8802/adminList 能夠看到以下效果
錯誤信息提示:
Error:java: Annotation processing is not supported for module cycles. Please ensure that all modules from cycle [qrcode-common,qrcode-manager-pojo] are excluded from annotation processing
緣由分析:循環依賴 死循環了
解決方案:修改模塊中的依賴
在使用IDEA開發多項目的時候發現這樣一個問題:修改pom文件以後,無論怎麼刷新都不生效
解決方案:重啓 IDEA 便可
本文涉及源碼下載地址: https://gitee.com/bingqilinpe...
恭喜你完成了本章的學習,爲你鼓掌!若是本文對你有幫助,請幫忙點贊,評論,轉發,這對做者很重要,謝謝。
讓咱們再次回顧本文的學習目標
- 掌握SpringBoot中多模塊開發
要掌握SpringBoot更多的用法,請持續關注本系列教程。
歡迎關注本人公衆號:鹿老師的Java筆記,將在長期更新Java技術圖文教程和視頻教程,Java學習經驗,Java面試經驗以及Java實戰開發經驗。
歡迎關注本人公衆號:鹿老師的Java筆記,將在長期更新Java技術圖文教程和視頻教程,Java學習經驗,Java面試經驗以及Java實戰開發經驗。