1:为什么SpringBoot很流行?
Spring Boot makes it easy to create stand-alone, production-grade
Spring based Applications that you can “just run”.
这是SpringBoot官网的标语,SpringBoot可以很快速的搭建起项目,让程序员更注重业务而不是配置。在SSM框架整合时,需要引入大量的依赖以及创建大量的配置文件,即使这样还很容易出现配置上的错误,而SpringBoot站在更高层次,对这些框架进行应用,快速搭建生产级别的Spring应用。
2:SpringBoot项目环境要求
以SpringBoot2.3.4为例:
- Maven3.3以上
- java8
为Maven的配置文件添加阿里云镜像:
<mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/central</url>
</mirror>
</mirrors>
3:HelloSpringBoot
1:创建Maven工程
2:引入SpringBoot的Web模块依赖
<!--父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<!--Web场景开发-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!--fat.jar 包含所有的运行环境-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.4.RELEASE</version>
</plugin>
</plugins>
</build>
3:创建主程序类
@SpringBootApplication注解标识此类为主程序类
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
//主程序类 启动方式
SpringApplication.run(MainApplication.class, args);
}
4:创建Controller
@RestController就等于@Controller和ResponseBody的结合
@RestController
public class Controller {
@RequestMapping("/hello")
public String handle01(){
return "hello,SpringBoot!!!";
}
}
5:方式一-运行主程序类
6:方式二-打包后cmd命令行运行
package: