第一章:SpringBoot基础入门之HelloWorld-基础入门

一:创建Maven工程

二:添加依赖

官网文档最为致命

We need to start by creating a Maven pom.xml file. The pom.xml is the recipe that is used to build your project. Open your favorite text editor and add the following:

 ①要用到SpringBoot的功能,只需要引入父项目boot-starter-parent

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
    </parent>

②想要开发web,只需要添加web的场景启动器

想以往我们Spring,SpringMVC要导入一大推的东西,现在我们只需要添加一个依赖,spring-boot-starter-web,我们称之为web的场景启动器,也就是说我们想要开发web场景了,把这个依赖导进来即可

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

三:编写你的代码即可

①编写业务代码之前,需要有一个主程序将它引导SpringBoot的启动

/**
 * 主程序类
 * @SpringBootApplication:这是一个SpringBoot应用
 */
@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }
}

②编写我们的业务代码

//@ResponseBody
//@Controller
@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String handle01() {
        return "Hello,Spring Boot 2!";
    }
}

③测试

直接运行main方法即可

成功在页面中显示

 ④简化配置

application.properties,所有的配置都可以写在这个里面

我们以前想要改tomcat端口号,还要改打开tomcat配置文件等等一大堆,现在SpringBoot是来整合其他所有东西的一个总框架,所以SpringBoot为了简化期间,将我们未来所有的配置都可以抽取在一个配置文件里面,application.properties。可以修改tomcat,SpringMVC的一些设置,什么都可以改噢

server.port=8888

 ⑤简化部署

我们再也不需要在目标服务器来安装tomcat等一大堆,只需要给我们工程里面引入

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

把项目打成jar包,直接在目标服务器执行即可。

注意点:

  • 取消掉cmd的快速编辑模式
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值