IDEA创建SpringBoot的多模块项目教程

最近在写一个多模块的SpringBoot项目,基于过程总了一些总结,故把SpringBoot多个模块的项目创建记录下来。

首先,先建立一个父工程:

(1)在IDEA工具栏选择File->New->Project

(2)选择Spring Initializr,默认选择Default,然后点击Next:    

IDEA创建SpringBoot的多模块项目教程_第1张图片

(3)在输入框填写以下截图内容,点击Next

IDEA创建SpringBoot的多模块项目教程_第2张图片

(4)直接点Next,无需选择

IDEA创建SpringBoot的多模块项目教程_第3张图片

(5)直接点击Finish完成创建

IDEA创建SpringBoot的多模块项目教程_第4张图片

(6)按照以上步骤,可以生成以下的项目目录结构:

IDEA创建SpringBoot的多模块项目教程_第5张图片

(7)这时把没用的.mvn目录,src目录,mvnw还有mvnw.cmd都删除,最终只保留.gitignore和pom.xml,若是web项目,可在该pom.xml里添加以下依赖:

1 
2 
3     org.springframework.boot
4     spring-boot-starter-web
5     2.3.1.RELEASE
6 

最终得到以下的父结构目录:

IDEA创建SpringBoot的多模块项目教程_第6张图片

以上是创建父模块,下面创建子模块:

(1)在父模块的根目录fte上点右键,在弹出的框里选择New->Module

IDEA创建SpringBoot的多模块项目教程_第7张图片

(2)选择Maven,点击Next

IDEA创建SpringBoot的多模块项目教程_第8张图片

(3)填写以下内容,点击Next

IDEA创建SpringBoot的多模块项目教程_第9张图片

(4)填写Module,点击Finish

IDEA创建SpringBoot的多模块项目教程_第10张图片

(5)同理添加fte-controller,fte-dao,fte-service,fte-web,最终得到以下的目录结构:

IDEA创建SpringBoot的多模块项目教程_第11张图片

(6)增加模块之间的依赖:

controller层添加以下依赖:

 1 
 2     
 3         com.example
 4         fte-common
 5         0.0.1-SNAPSHOT
 6     
 7 
 8     
 9         com.example
10         fte-dao
11         0.0.1-SNAPSHOT
12     
13 
14     
15         com.example
16         fte-service
17         0.0.1-SNAPSHOT
18     
19 

service层添加以下依赖:

1 
2     
3         com.example
4         fte-dao
5         0.0.1-SNAPSHOT
6     
7 

(7)测试

在fte-controller创建com.zhu.fte.web包,增加以下两个类:

fteWebApplication类:

 1 package com.zhu.fte.web;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 
 6 @SpringBootApplication
 7 public class fteWebApplication {
 8     public static void main(String[] args) {
 9         SpringApplication.run(fteWebApplication.class,args);
10     }
11 }
DemoController类

 1 package java.com.zhu.fte.web;
 2 
 3 import org.springframework.web.bind.annotation.GetMapping;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RestController;
 6 
 7 @RestController
 8 @RequestMapping("demo")
 9 public class DemoController {
10 
11     @GetMapping("test")
12     public String test(){
13         return "hello world";
14     }
15 
16 }

运行发现出现错误:

出现错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project fte-common: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test failed: Plugin org.apache.maven.plugins:maven-surefire-plugin:2.22.2 or one of its dependencies could not be resolved: Could not transfer artifact junit:junit:jar:4.12 from/to central (https://repo.maven.apache.org/maven2): Connect to repo.maven.apache.org:443 [repo.maven.apache.org/151.101.52.215] failed: Connection timed out: connect -> [Help 1]

把缺少的org.apache.maven.plugins手动放到父工程的pom.xml里

 1 
 2    
 3       
 4          org.apache.maven.plugins
 5          maven-clean-plugin
 6          2.5
 7       
 8       
 9          org.apache.maven.plugins
10          maven-source-plugin
11          2.2
12       
13       
14          org.apache.maven.plugins
15          maven-compiler-plugin
16          3.0
17          
18             1.8
19             1.8
20             ${file.encoding}
21             
22             -parameters
23          
24       
25       
26          org.apache.maven.plugins
27          maven-install-plugin
28          2.4
29       
30       
31          org.apache.maven.plugins
32          maven-jar-plugin
33          2.4
34          
35             
36                
37                   true
38                   
39                
40             
41          
42       
43 
44       
45          org.apache.maven.plugins
46          maven-surefire-plugin
47          2.13
48          
49             -Xmx512M -Dfile.encoding=${file.encoding}
50          
51       
52    
53 

运行fteWebApplication类里的main方法,默认端口为8080,访问http://localhost:8080/demo/test,正常出现以下情况:

IDEA创建SpringBoot的多模块项目教程_第12张图片

按照以上步骤,就可以初步建立SpringBoot多模块的项目,下一章将基于这个基础搭建Mybatis以及其逆向工程。

你可能感兴趣的:(spring,boot,intellij-idea,java)