欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

【SpringBoot】多模块项目结构搭建

程序员文章站 2022-09-21 15:16:16
必需学会SpringBoot基础知识 Takes an opinionated view of building production-ready Spring applications. Spring Boot favors convention over configuration and is ......
前言:

必需学会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

 

准备工作

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

先做一个简单  

 

理论分析

模型层: 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