SpringBoot教程(二) | 创建SpringBoot项目
一、前言
接下来我们要学习一下如何创建一个spring boot项目。
二、所需环境
- JDK1.8
- Maven 3.6
- Idea 2021.1
三、创建方式
- 从Maven项目改造为springboot项目
- 在线创建springboot项目(官网和阿里云脚手架)
- 在使用开发工具idea创建SpringBoot项目( Spring Initializr),本质上其实就是使用在线创建的API
建议用第三种方式:idea创建SpringBoot项目
方式一:maven 改造
maven改造,比较麻烦
1. 创建maven项目
2. 添加依赖,让其成为springBoot项目
先讲一下 spring-boot-dependencies
- spring-boot-dependencies 称之为 Spring Boot的依赖项版本管理文档,包含了Spring Boot项目所需的大量依赖项及其推荐的版本号
- 通过引入它后,你可以
不需要
在依赖项上都显式指定版本号
,因为Maven会自动从spring-boot-dependencies 中获取版本号。 - 重点:
它只是声明哪些依赖项应该使用什么版本的,但它本身并不将任何依赖项添加到项目的类路径中
方式一(直接依赖 spring-boot-starter-parent)
直接继承starter parent项目
因为spring-boot-starter-parent 里面也是 引入了 spring-boot-dependencies
缺点就是该项目不能继承父POM了。
在pom文件中添加如下依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
然后点击项目右侧maven的刷新按钮,等到依赖下载完毕就可以了。
前提是maven一定要配置正确。
完整pom.xml如下:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/>
</parent>
<groupId>org.example</groupId>
<artifactId>springboot-test</artifactId