SpringBoot初体验

以下内容都是参考官方文档做的笔记 https://docs.spring.io/spring-boot/docs/current/reference/html/index.html

0.为什么要用SpringBoot

在这里插入图片描述

1.前置准备

注意idea的maven用的是我们自己的maven

在这里插入图片描述

然后在pom中修改内容
在这里插入图片描述

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

maven如果卡顿,可以使用如下setting,然后下面的jdk是根据自己的jdk,springboot要求1.8+

<mirrors>
    <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>central</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror>
</mirrors>
 
<profiles>
    <profile>
        <id>jdk-1.8</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <jdk>1.8</jdk>
        </activation>
        <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
        </properties>
    </profile>
</profiles>

2.加入jar包依赖

SpringBoot为我们提供了一系列的“Starters”,整合了不同场景下开发所需要的jar包依赖
在这里插入图片描述

现在我们加入web依赖

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

3.书写代码

src/main/java/MyApplication.java

//@RestController告诉Spring直接把resulting String返回给caller
@RestController
//@EnableAutoConfiguration让SpringBoot自动去配置Spring
@EnableAutoConfiguration
public class MyApplication {
    //@RequestMapping("/") 表示任何带有/路径的http请求都会被映射到home方法中
    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

4.运行

在这里插入图片描述

5.用SpringBoot创建一个可执行的Jar包

前提:我们需要在pom.xml中添加如下插件,如下代码添加在dependencies代码块后面

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

在这里插入图片描述

然后运行package即可打包成一个可执行的jar包

在这里插入图片描述

6.SpringBoot的统一配置文件

所有的配置都可以在这两个文件夹下配

src/main/resources/application.properties

src/main/resources/application.yaml
在这里插入图片描述


springboot真的好好用,简化了ssm好多配置,而且starter自带tomcat,简化开发,摆脱了配置地狱呜呜呜

springboot就是封装太深了,内部原理复杂,不太容易掌握

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值