以下内容都是参考官方文档做的笔记 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就是封装太深了,内部原理复杂,不太容易掌握