SpringBoot创建第一个HelloWorld程序

一个字 爽 感觉之前学的都是为了学SpringBoot做铺垫了 配置文件一大堆 现在 简直舒服死了 话不多说 直接上图 上代码了

https://abc.flya.top/img/218

  • 首先依旧是 创建一个Maven应用 在pom.xml文件里加入Springboot启动依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--web场景启动器-->
</dependency>
</dependencies>

这个启动器包含了一堆jar包 包含之前 Springmvc 等一系列jar包 甚至包含Tomcat!! wc

https://abc.flya.top/img/219

  • 创建主程序 启动入口
package com.fl;

import org.springframework.boot.SpringApplication;

/**
* Created with IntelliJ IDEA.
*
* @author: 风离
* @Date: 2021/07/10/1:25
* @Description:
* 主程序类
*/
@org.springframework.boot.autoconfigure.SpringBootApplication
public class SpringBootApplicationTest {
public static void main(String[] args) {
SpringApplication.run((SpringBootApplicationTest.class),args);
}
}

  • 创建controller层 helloworld案例
    package com.fl.controller;

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

    /**
    * Created with IntelliJ IDEA.
    *
    * @author: 风离
    * @Date: 2021/07/10/1:30
    * @Description:
    */

    @RestController // 等价于 @Controller + @ResponseBody 目前~
    public class HelloController {

    @RequestMapping("/hello")
    public String hello()
    {
    return "hello ,SpringBoot!!!";
    }
    }

这样就能跑起来了??? 确实 离谱的一批 就是这么简单直接

  • 固定名称Application.properties SpringBoot的主配置文件 来进行配置端口号 以及数据库账号密码等~

https://abc.flya.top/img/220

你好 SpringBoot !