一. 先创建一个maven工程

选择maven
选择 Maven,点击 NEXT
二. 填写包名和项目名

填写包名和项目名
填写后点击 FINISH,完成 maven 项目的创建
三. 打开 pom.xml 文件,添加如下配置
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/>
</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>

添加配置
四. 现在开始创建子工程,右键项目名,New -> Module

New -> Module
同样是选择 maven,next

New Module

image.png
此时 Parent 显示为 test-project ,然后填写包名和项目名,点击 FINISH
五. test-common 创建完成后,我们打开 test-project 的 pom.xml 文件,会发现自动添加了 test-common module

test-common module
此时的项目目录结构为:

项目目录结构
六. 按照第4步骤,继续创建 test-pojo、test-mapper、test-service、test-api 子项目,都创建完成后,查看 test-project 的 pom.xml 文件,是否都自动添加了对应的 module

test-project
创建完成后的项目目录结构如下:

创建完成后项目目录结构
七. 接着我们添加依赖,在父工程 test-project 的 pom.xml 文件中添加 Springboot 依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
然后我们执行一下 mvn install 命令,将其 module 都安装一遍

install
八. 安装完成后,接着给子模块添加依赖
子模块的依赖关系如下:
api -> service -> mapper -> pojo -> common
-
打开 test-api中的 pom.xml 文件,添加 test-service 依赖
<dependencies>
<dependency>
<groupId>com.wuxl</groupId>
<artifactId>test-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

test-api pom.xml
-
test-service的依赖关系如下
service -> mapper -> pojo -> common
打开 test-service 中的 pom.xml 文件,添加 test-mapper 依赖
<dependencies>
<dependency>
<groupId>com.wuxl</groupId>
<artifactId>test-mapper</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

test-service pom.xml
-
test-mapper的依赖关系如下
mapper -> pojo -> common
所以子项目 test-mapper 的 pom.xml 文件 添加 test-pojo 依赖
<dependencies>
<dependency>
<groupId>com.wuxl</groupId>
<artifactId>test-pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

test-mapper pom.xml
-
最后,子项目 test-pojo 添加 test-common 依赖即可
<dependencies>
<dependency>
<groupId>com.wuxl</groupId>
<artifactId>test-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

test-pojo pom.xml
九. 接着来创建启动类和 controller 测试项目创建是否成功
-
在 test-api 项目中新建包 com.wuxl ,再在包下新建 Application.java 启动文件
package com.wuxl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author wuxl
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
-
接着在包下再创建一个 controller 包,新建 HelloController.java 文件
package com.wuxl.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author wuxl
*/
@RestController
public class HelloController {
@GetMapping("hello")
public Object hello() {
return "Hello World";
}
}

创建启动类和controller
-
运行 Application.java ,访问 localhost:8080/hello,页面输出 Hello World 说明项目创建成功
网友评论