1. 程式人生 > 程式設計 >spring boot maven 多模組專案建立筆記

spring boot maven 多模組專案建立筆記

一、建立父專案(parent)

使用IDE建立功能

Intellij > File > New > Project > Spring Initializr
複製程式碼

設定專案名稱,使用預設選項,建立空的主專案

修改pom.xml

  1. 新增子模組
<!-- 模組說明:這裡宣告多個子模組 -->
    <modules>
        <module>btp-bean</module>
        <module>btp-common</module>
        <module>btp-dao</module>
        <module>btp-service</module>
        <module>btp-admin</module>
        <module>btp-user</module>
    </modules>
複製程式碼
  1. 刪除dependencies內容
  2. 新增pom 父專案打包不能是預設的jar,所以得改成pom
  3. 刪除

最終的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 http://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.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.boyuantang</groupId> <artifactId>btp-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <name>btp-parent</name> <description>Demo project for
Spring Boot</description> <packaging>pom</packaging> <!-- 模組說明:這裡宣告多個子模組 --> <modules> <module>btp-bean</module> <module>btp-common</module> <module>btp-dao</module> <module>btp-service</module> <module>btp-admin</module> <module>btp-user</module> </modules> <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> </dependencies> </project> 複製程式碼

父專案的作用

  • 父專案繼承spring框架
  • 父專案定義版本號
  • 父專案讓子模組之間可以相互引用

二、建立子模組

在IDE中點選父專案,右鍵

New > Module > Spring Initializr
複製程式碼

設定專案名稱,使用預設選項,建立空的子模組

修改pom.xml

  1. 新增父專案

取代預設的parent spring,因為父專案已經繼承了parent spring

<!-- 繼承本專案的父工程 -->
    <parent>
        <groupId>com.boyuantang</groupId>
        <artifactId>btp-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
複製程式碼
  1. 刪除dependencies內容 根據自己的需要新增依賴,如果是web模組,要新增spring的一整套依賴。如果是module模組,則按需要新增依賴。同時子模組之間也可以相互引用,如下
<dependency>
	<groupId>com.boyuantang</groupId>
	<artifactId>btp-dao</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>
複製程式碼
  1. 如果非web模組,則刪除

非web模組,最終的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!-- 繼承本專案的父工程 -->
    <parent>
        <groupId>com.boyuantang</groupId>
        <artifactId>btp-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.boyuantang</groupId>
    <artifactId>btp-dao</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>btp-dao</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
        	<groupId>com.boyuantang</groupId>
        	<artifactId>btp-service</artifactId>
        	<version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>
複製程式碼

三、打包

子模組可以分開打包。生成的jar可執行。

四、配置問題

  1. 會遇到報錯說 xxxDAO 不存在,新增@MapperScan
@MapperScan("com.boyuantang.dao")
@SpringBootApplication
public class BtpAdminApplication {
    public static void main(String[] args) {
        SpringApplication.run(BtpAdminApplication.class,args);
    }
}
複製程式碼
  1. 會遇到無法註冊service模組的bean,新增@ComponentScan
@ComponentScan("com.boyuantang")
@SpringBootApplication
public class BtpAdminApplication {

    public static void main(String[] args) {
        SpringApplication.run(BtpAdminApplication.class,args);
    }

}
複製程式碼
  1. 無法掃描mybatis的xml檔案,在application.properties中新增
mybatis.mapper-locations=classpath*:mapper/*.xml
複製程式碼

五、專案之間的依賴關係

  1. 父專案匯入全域性依賴,這樣子模組就不用再匯入這些模組
    比如lombok,fastjson之類
  2. web模組匯入service模組,不用匯入entity,common這些,因為service已經包含了這些子模組