SpringBoot教程(二) | 创建SpringBoot项目

一、前言

接下来我们要学习一下如何创建一个spring boot项目。

二、所需环境

  • JDK1.8
  • Maven 3.6
  • Idea 2021.1

三、创建方式

  1. 从Maven项目改造为springboot项目
  2. 在线创建springboot项目(官网和阿里云脚手架)
  3. 在使用开发工具idea创建SpringBoot项目( Spring Initializr),本质上其实就是使用在线创建的API

建议用第三种方式:idea创建SpringBoot项目

方式一:maven 改造

maven改造,比较麻烦

1. 创建maven项目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2. 添加依赖,让其成为springBoot项目

先讲一下 spring-boot-dependencies

  1. spring-boot-dependencies 称之为 Spring Boot的依赖项版本管理文档,包含了Spring Boot项目所需的大量依赖项及其推荐的版本号
  2. 通过引入它后,你可以不需要在依赖项上都显式指定版本号,因为Maven会自动从spring-boot-dependencies 中获取版本号。
  3. 重点:它只是声明哪些依赖项应该使用什么版本的,但它本身并不将任何依赖项添加到项目的类路径中

方式一(直接依赖 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值