1. 程式人生 > 其它 >Springboot多模組專案搭建

Springboot多模組專案搭建

目錄

新建專案,作為父模組

刪除除pom.xml之外的所有檔案

效果如下:

.gitignore和ReadMe.md是可選檔案

建立子模組

工程上右鍵 -> new -> model -> 選擇Spring Initializer -> 下一步

依此步驟分別建立 web、service、entity、dao,刪除每個模組中無用的檔案,只留下pom.xml

刪除除web模組外其他模組的Application啟動類和resources目錄下的application.properties配置檔案

各個模組建立關係

父POM配置

1. packaging改為pom
2. 宣告子模組
3. properties/denpendencies下的配置都為公共配置
4. build模組是打包用的,這裡聲明瞭所有模組都跳過單元測試

<?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.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/>
    </parent>

    <!--父POM-->
    <groupId>com.xxx</groupId>
    <artifactId>myblog</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>myblog</name>
    <packaging>pom</packaging>

    <!--宣告子模組-->
    <modules>
        <module>entity</module>
        <module>dao</module>
        <module>service</module>
        <module>web</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>
        <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>
            <!--maven-surefire-plugin是執行測試用例的外掛,這裡宣告跳過TEST-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--宣告父模組-->
    <parent>
        <groupId>com.xxx</groupId>
        <artifactId>myblog</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <!--預設就是 ../pom.xml-->
        <relativePath>../pom.xml</relativePath>
    </parent>

    <!--當前模組-->
    <groupId>com.xxx</groupId>
    <artifactId>entity</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>entity</name>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
</project>

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--宣告父模組-->
    <parent>
        <groupId>com.xxx</groupId>
        <artifactId>myblog</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <!--當前模組-->
    <groupId>com.xxx</groupId>
    <artifactId>dao</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dao</name>
    <packaging>jar</packaging>

    <!--依賴-->
    <dependencies>
        <!--資料庫連線-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!--Mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>

        <!--引入entity模組-->
        <dependency>
            <groupId>com.xxx</groupId>
            <artifactId>entity</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

Service的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.0</modelVersion>

    <!--宣告父模組-->
    <parent>
        <groupId>com.xxx</groupId>
        <artifactId>myblog</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <!--當前模組-->
    <groupId>com.xxx</groupId>
    <artifactId>service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>service</name>
    <packaging>jar</packaging>

    <!--依賴-->
    <dependencies>
        <!--引入dao模組-->
        <dependency>
            <groupId>com.xxx</groupId>
            <artifactId>dao</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!--引入entity模組-->
        <dependency>
            <groupId>com.xxx</groupId>
            <artifactId>entity</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

Web的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.0</modelVersion>

    <!--宣告父模組-->
    <parent>
        <groupId>com.xxx</groupId>
        <artifactId>myblog</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <!--當前模組-->
    <groupId>com.xxx</groupId>
    <artifactId>web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>web</name>
    <packaging>jar</packaging>

    <!--依賴-->
    <dependencies>
        <!--Web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--引入service模組-->
        <dependency>
            <groupId>com.xxx</groupId>
            <artifactId>service</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!--引入entity模組-->
        <dependency>
            <groupId>com.xxx</groupId>
            <artifactId>entity</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>

    <!--編譯打包相關-->
    <build>
        <plugins>
            <!--SpringBoot專案必須加入這個外掛,才會將普通JAR打成SpringBoot的JAR-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--指定Main Class為全域性的唯一入口-->
                    <mainClass>com.xxx.blog.WebApplication</mainClass>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>

程式碼

Entity

package com.xxx.blog;

import lombok.Data;

import java.util.Date;

/**
 * @author computer
 */
@Data
public class UmsMenu {
    private int id;

    private String title;

    private int level;

    private Date createTime;

    private int parentId;
}

Mapper

package com.xxx.blog;

import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @author computer
 */
public interface UmsMenuMapper {
    /**
     * 根據ID查詢
     * @param id
     * @return
     */
    /*@Select("select * from ums_menu where id = #{id}")
    @Results({
            @Result(property = "createTime",column = "create_time",javaType = Date.class),
            @Result(property = "parentId",column = "parent_id",javaType = int.class)
    })*/
    UmsMenu findById(@Param("id") int id);

    /**
     * 查詢所有
     * @return
     */
    /*@Select("select * from ums_menu")
    @Results({
            @Result(property = "createTime",column = "create_time",javaType = Date.class),
            @Result(property = "parentId",column = "parent_id",javaType = int.class)
    })*/
    List<UmsMenu> findAll();
}

Mapper.xml

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxx.blog.UmsMenuMapper">
    <resultMap id="resultMap" type="com.xxx.blog.UmsMenu">
        <result column="id" property="id" javaType="java.lang.Integer"/>
        <result column="title" property="title" javaType="java.lang.String"/>
        <result column="level" property="level" javaType="java.lang.Integer"/>
        <result column="create_time" property="createTime" javaType="java.util.Date"/>
        <result column="parent_id" property="parentId" javaType="java.lang.Integer"/>
    </resultMap>

    <select id="findById" parameterType="java.lang.Integer" resultMap="resultMap">
        select * from ums_menu where id = #{id}
    </select>

    <select id="findAll" resultMap="resultMap">
        select * from ums_menu
    </select>
</mapper>

Service

package com.xxx.blog;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author computer
 */
@Service
public class WebService {

    @Autowired
    UmsMenuMapper mapper;

    /**
     * 獲取所有Menu資訊
     * @return
     */
    public List<UmsMenu> getAllMenu(){
        return mapper.findAll();
    }
}

Controller

package com.xxx.blog.controller;

import com.xxx.blog.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author computer
 */
@RestController
@RequestMapping("/test")
public class WebTest {

    @Autowired
    WebService webService;

    /**
     * 獲取所有Menu資訊
     * @return
     */
    @RequestMapping(value = "/getAllMenu",produces = "application/json")
    public String getAllMenu(){
        return webService.getAllMenu().toString();
    }
}

驗證

效果

注意事項

1.父pom.xml 打包方式,jar要更改為pom,build 需要更改

2.不需要打包的模組pom.xml檔案中不要寫<build>,全刪掉,例如有些工程中的common模組,utils模組,entity模組,service模  塊都不需要打包

3.宣告父工程時,填寫父工程位置<relativePath>../pom.xml</relativePath>

4.關於applicatin.properties配置檔案,只需要在啟動的模組中配置就可以了,

5.關於打包為什麼打包jar包,不打war包,打war包目的是war包可以執行在tomcat下,但是SpringBoot是內建tomcat,如果你打war包,前提是幹掉內建的tomcat,然後才能打包,各種麻煩,直接打包可執行jar包,使用java -jar 命令就可以完美的執行起來很方便!
————————————————
版權宣告:本文為CSDN博主「凌雲冷海」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處連結及本宣告。
原文連結:https://blog.csdn.net/baidu_41885330/article/details/81875395

參考資料

SpringBoot+Maven多模組專案(建立、依賴、打包可執行jar包部署測試)完整流程