SpringBoot简介
1.什么是 SpringBootSpringBoot?
SpringBootSpringBoot 是由Pivotal团队提供的全新框架,其中“Boot”的意思就是“引导”,意在简化开发模式,使开发者能够快速的开发出基于Spring 的应用。Spring Boot 并不是对 Spring 功能上的增强,而是提供了一种快速使用 Spring 的方式。
2.SpringBoot 的特点
1.嵌入的 Tomcat,无需部署 WAR 文件
2.简化Maven配置
3.自动配置Spring
3.SpringBoot 启动器
spring-boot-starter-web:支持全栈式的 web 开发,
包括了 tomcat 和 springMVC 等 jar
Ø spring-boot-starter-jdbc:支持 spring 以 jdbc 方式操作数据库的 jar 包的集合
Ø spring-boot-starter-redis:支持 redis 键值存储的数据库操作
2.搭建SpringBoot项目
- 创建maven项目,不使用骨架直接创建maven项目
- 继承springBoot父工程(已经被Pivotal 提前写好直接继承就行)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
- 设置jdk版本(尽量使用1.8版本的JDK)
<java.version>1.8</java.version>
- 添加启动器
<dependencies>
<!-- springboot的web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Spring Boot入门HelloWorld
1 控制层(controller)
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@Controller
public class HelloWorld {
@RequestMapping("/hello")
@ResponseBody
public Map<String, Object> showHelloWorld() {
Map<String, Object> map = new HashMap<>();
map.put("msg", "HelloWorld");
return map;
}
}
2 启动类
位置:
controller同一个包下
controller 的上一级包中
Spring Boot的全局配置文件
SpringBoot项目使用一个全局的配置文件application.properties或者是application.yml,在resources目录下或者类路径下的/config下,一般我们放到resources下。他有2种格式
以 . properties 结尾的配置文件
修改tomcat的端口号
Server.port=8888
修改访问项目时的名字
Server.servlet.context-path=/springboot
以 .yml 结尾的配置文件是一种新增的配置文件格式,特点为
具有天然的树状结构
两种配置文件的区别
文件的扩展名不同
文件中的语法不同
1在yml中使用 :分割
2在yml 不允许使用tab键缩进的空格数不重要,只要是左对齐的一列数据,都是同一个层级
3每个K的冒号后面一定要加一个空格
Yml方式配置springBoot
server:
port: 8090
servlet:
context-path: /springboot
Spring Boot项目的两种发布方式
1.方式1:通过jar包发布
步骤1:在pom中添加一个SpringBoot的构建的插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.方式2:通过war包发布
步骤1:在pom.xml文件中将jar修改为war
war</ packaging >
步骤2:设置tomcat启动器依赖范围
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!--tomcat启动器依赖范围-->
<scope>provided</scope>
</dependency>
步骤3设置war包的名字
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>hello</warName>
</configuration>
</plugin>
步骤4:修改启动类
package com.usian;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}