1.我们为什么要学习SpringBoot?
1.Spring Boot 可以轻松创建可以“直接运行”的独立的、生产级的基于 Spring 的应用程序。
2.内置web的服务器(如tomcat,jetty等),我们不再需要对服务器进行部署设置
3.我们不再需要编写大量的配置文件,springBoot会为我们自动搭建,我们只需要关注程序的编写
4.SpringBoot是整合Spring技术栈的一站式框架,是简化Spring技术栈的快速开发脚手架
2.第一个springBoot程序
1.系统要求
java8及以上吗,maven3.3+
maven的setting设置(设置阿里云镜像等)
<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.想要实现的功能:浏览器发送/hello的请求,响应hello,springBoot2
3.创建maven工程,引入依赖
<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>
</dependency>
</dependencies>
4.创建主程序
/*主程序类
* @SpringBootApplication:这是一个springBoot应用
* */
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
5.在controller中编写程序业务
/*@ResponseBody //表示类中的每个方法的数据是写给浏览器,而不是跳转
@Controller*/
@RestController //上面两个注解的合体
public class HelloController {
@RequestMapping("/hello")
public String handle01() {
return "hello,springBoot";
}
}
6.可以在resources目录中创建application.properties文件中简化配置,如设置服务器端口号
server.port=8888
7.测试--直接运行主程序中的main方法
这样,我们就实现了第一个springBoot项目