创建SpringBoot项目方式

130 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路

可以使用Maven方式和Spring Initializer 方式创建项目

环境支持:Maven 3.6.2 + JDK1.8 + IDEAJ

一、使用Maven 方式构建SpringBoot项目

1、初始化Maven设置 打开IDEA欢迎页,右下角【Configure】--->【Settings】--->左侧搜索Maven,配置Maven信息 图片1.png 2、JDK初始化设置 打开IDEA欢迎页,右下角【Configure】--->【Structure for New Project】--->左侧选择【Project Settings】--->【Project】,设置JDK版本为1.8

图片2.png 3、在IDEA欢迎页,【Create New Project】创建项目,选择Maven方式创建项目

图片3.png 点击Next后,输入项目信息和Maven信息后点击Finish

图片4.png 4、打开pom.xml添加SpringBoot的相关依赖,并选择自动导入依赖【Enable Auto-Import】

图片5.png

<?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>

   <groupId>com.chen</groupId>

   <artifactId>SpringBoot_demo01</artifactId>

   <version>1.0-SNAPSHOT</version>

<!--引入SpringBoot父依赖-->

<parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.1.3.RELEASE</version>

</parent>

<dependencies>

    <!--引入web场景依赖启动器-->

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

</dependencies>

</project>

5、编写SpringBoot的启动类

在SpringBoot_demo01下的src-main-java下新建一个包com.chen,在该包下新建启动类

Demo01Applicationimport org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication//标明当前类为SpringBoot启动类

public class Demo01Application {

        public static void main(String[] args) {

            SpringApplication.run(Demo01Application.class,args);

        }
}

6、在com.chen下新建一个controller包,在该包下创建一个HelloController用于测试

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController//@RequestMapping+@ResponseBody

public class HelloController {

        @RequestMapping("/hello")

        public String hello(){

            return "hello Spring Boot" ;
        }
}

7、运行启动类Demo01Application

在浏览器访问http://localhost:8080/hello

图片6.png

二、使用Spring Initializr 方式构建SpringBoot项目

1、新建项目时选择Spring Initializr ,点击Next

图片7.png 2、填写Maven信息,然后点击Next

图片8.png 3、选择依赖场景,然后点击Next

图片9.png 4、填写项目信息,然后点击Finish

图片10.png 5、生成的项目结构

图片11.png 生成的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

https://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.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
    <groupId>com.chen</groupId>
    <artifactId>demo02</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo02</name>
    <description>Demo project for Spring Boot</description>
    
<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <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>
    <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

    </plugins>
</build>
</project>

可以看到,通过Spring Initializr生成的SpringBoot项目包含了SpringBoot启动类和SpringBoot的web场景下所需要的依赖启动器 spring-boot-starter-parent和spring-boot-starter-web 此外还配置了单元测试的启动器spring-boot-starter-test和maven打包插件

6、在com.chen.demo02下新建一个controller包,在该包下新建一个HelloController

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @RequestMapping("/hi")
    public String sayHello(){
        return "SpringInitlizer 启动成功";
    }
}

7、运行启动类Demo02Application在浏览器输入http://localhost:8080/hi

图片12.png