5.1 为什么要搭建
5.1.1 在我们学习过程中需要使用
在学习springboot的过程中,可能要进行入门,配置,springmvc,mybatis,redis等模块基础springboot测试学习,我们可以为每个测试创建一个springboot项目来测试,但是比较麻烦。其实我们有以下解决方案:
5.1.1.1 一个idea springboot项目中
我们可以在一个idea springboot项目中写很多代码,包含springboot的入门,配置,springmvc,mybatis,redis等,这样一个项目中代码过多,不好管理,并且容易把自己搞混。
5.1.1.2 一个idea springboot项目不同maven模块
我们可以在一个idea springboot项目中创建不同模块分别完成springboot的入门,配置,springmvc,mybatis,redis等的学习测试,这样代码方便管理,并且也不容易混淆。
5.1.2 项目开发中需要使用
以后我们在进行微服务项目搭建的时候也要进行maven多模块搭建!具体可以如下图:
5.1.3 小结
无论从我们学习的角度或者以后项目角度都要搭建springboot idea maven多模块,接下来我们就来搭建。
5.2 搭建
5.2.1 搭建步骤分析
原理图如下:
1 创建springboot-parent idea maven项目
1)springboot-parent作为我们所有模块的父模块,可以把src直接删除,我们不需要管理代码。
2)在springboot-parent的pom.xml,以spring-boot-starter-parent为父模块,这样
我们新建的模块以springboot-parent为父模块的模块,间接继承了spring-boot-starter-parent
2 以springboot-parent为父模块创建springboot-01hello
这样springboot-01hello就是以springboot-parent为父模块
3 在springboot-01hello实现springboot项目的功能
1)pom.xml
2)appcation.yml/application.properties
3)入口类
4)controller测试
5.2.2 搭建
5.2.2.1 创建springboot-parent
创建maven java项目后,以spring-boot-starter-parent来作为父模块,具体配置如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
5.2.2.2 创建springboot-01hello
我们以创建一个springboot-01hello子模块为例,以后所以的子模块就和它的创建过程一样。具体实现如下:
直接在springboot-parent上面右键,创建子模块
自动以springboot-parent作为父模块,会自动生成以下配置
<parent>
<artifactId>springboog-parent</artifactId>
<groupId>org.yang</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
5.2.2.3 实现springboot-01hello
-
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <build> <!--插件--> <plugins> <!--打包插件:将项目打成jar包或者war包--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
-
application.yml
server: port: 8080 #端口 spring: application: name: springboot-hello # 应用名称
-
入口类
//标识改项目为springboot项目 @SpringBootApplication //复合注解 public class App {//可以是任何名称 //springboot启动类,以后以main方法的方式就可以启动Springboot的项目。 public static void main(String[] args) { SpringApplication.run(App.class,args); } }
-
controller
@RestController @RequestMapping("/user") public class UserController { @GetMapping public String getUser(){ return "user"; } }
5.3 总结
本章节,我们讲了为什么要搭建springboot maven的多模块,以及搭建了springboot maven多模块,后面我们测试都是基于这个多模块来实现