SpringBoot多模块项目结构搭建

Posted eddieblog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot多模块项目结构搭建相关的知识,希望对你有一定的参考价值。

前言:

必需学会SpringBoot基础知识

简介:

Takes an opinionated view of building production-ready Spring applications. Spring Boot favors convention over configuration and is designed to get you up and running as quickly as possible.

工具:

JDK8

apache-maven-3.5.2

IntelliJ IDEA 2018.1.3 x64

 

准备工作

通过之前章节进行改造,更符合企业需求。

先做一个简单 Web Flux 项目 

 

理论分析

模型层: model
持久层: persostence
表示层: web

 

web 依赖 persostence
persostence 依赖 model

 

选中项目右键点击 New –> Module,选择 Maven 下一步 填写 artifactId (web) 然后,一直下一步到完成;

技术分享图片

技术分享图片

然后从原始项目里移植代码

技术分享图片

如此类推的把另外一个也新建起来

技术分享图片

 

好了!  下面到pom文件加入依赖, 大家应该也有看上面我写的准备工作把? 对 就按这个做!

 

first-app-demo

  1. 需要手动修改为pom
  2. <modules> 是在新建子目录的时候自动增加的,并非手动
<?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>

    <groupId>com.lwc</groupId>
    <artifactId>first-app-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <modules>
        <module>web</module>
        <module>persostence</module>
        <module>model</module>
    </modules>

    <!--修改成 pom-->
    <packaging>pom</packaging>
    <!--
        模型层: model
        持久层: persostence
        表示层: web

        web 依赖 persostence
        persostence 依赖 model

        web UserController -> UserRepository -> User

    -->

    <name>first-app-demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.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>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

 

web.pom

  1. web 依赖 persostence (复制persostence的<parent>里面的<groupId>、<version>、<artifactId>到 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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>first-app-demo</artifactId>
        <groupId>com.lwc</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>web</artifactId>

    <dependencies>

        <!-- 添加对persostence依赖 -->
        <dependency>
            <groupId>com.lwc</groupId>
            <artifactId>persostence</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>

</project>

 

persostence.pom

  1. persostence 依赖 model (原理和上面一样)
<?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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>first-app-demo</artifactId>
        <groupId>com.lwc</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>persostence</artifactId>

    <dependencies>
        <!-- 添加对model依赖 -->
        <dependency>
            <groupId>com.lwc</groupId>
            <artifactId>model</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>

</project>

 

model.pom

  1. 不需要依赖任何东西
<?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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>first-app-demo</artifactId>
        <groupId>com.lwc</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>model</artifactId>

</project>

 

最后Build一下,启动项目。 完成!

 

源码下载

https://github.com/eddie-code/SpringBootDemo/tree/master/first-app-demo




以上是关于SpringBoot多模块项目结构搭建的主要内容,如果未能解决你的问题,请参考以下文章

IDEA 搭建多模块SpringBoot 项目:集成MyBatisDruidDubbo

IntelliJ IDEA 2017.3 搭建一个多模块的springboot项目

springboot 搭建多模块的作用以及优点

SpringBoot之多模块项目

2019最新 Java商城秒杀系统的设计与实战视频教程(SpringBoot版)_2-1微服务项目的搭建-SpringBoot搭建多模块项目一

SpringBoot - 多模块项目的搭建教程