入门案例
入门案例:
1.创建新模块,选择Spring初始化,并配置模块相关基础信息
2.选择当前模块需要使用的技术集
3.开发控制类
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println("id ==》"+id);
return "hello , spring boot!";
}
}
4.运行自动生成的启动类(Application类)
最简SpringBoot程序所包含的基础文件
- pom.xml文件
- Application类
基于idea开发SpringBoot程序需要确保联网且能够加载到程序框架结构
对SpringBoot项目快速启动
步骤:
1.对SpringBoot项目打包(执行Maven构建指令package)
2.执行启动指令
SpringBoot简介
- SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程
- Spring程序缺点
- 配置繁琐
- 依赖设置繁琐
- SpringBoot程序优点
- 自动配置
- 起步依赖(简化依赖配置)
- 辅助功能(内置服务器,…)
SpringBoot起起步依赖
- starter
- SpringBoot中常见项目名称,定义了当前项目使用的所有项目坐标,以达到减少依赖配置的目的
- parent
- 所有SpringBoot项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的
- spring-boot-starter-parent(2.5.0)与spring-boot-starter-parent(2.4.6)共计57处坐标版本不同
- 实际开发
- 使用任意坐标时,仅书写GAV中的G和A,V由SpringBoot提供
- 如果发生坐标错误,再指定version(要小心版本冲突)
基础配置
配置文件格式
配置格式
SpringBoot提供了多种属性配置方式
application.properties
server.port=80
application.yml
server:
port:80
application.yaml
server:
port:80
自动提示功能消失解决方案
SpringBoot配置文件加载顺序(了解)
application.properties > application.yml > application.yaml
yaml
- YAML(YAML Ain’t Markup Language),一种数据序列化格式
- 优点:
- 容易阅读
- 容易与脚本语言交互
- 以数据为核心,重数据轻格式
- YAML文件扩展名
- .yml(主流)
- .yaml
yaml数据读取
使用@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名…}
封装全部数据到Environment对象
自定义对象封装指定数据
自定义对象封装数据警告解决方案
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
多环境启动
两种方式都可以
spring:
profiles:
active: dev
---
#开发
server:
port: 81
spring:
profiles: test
---
#生产
server:
port: 82
spring:
profiles: pro
---
#测试
server:
port: 80
spring:
config:
activate:
on-profile: dev
配置文件分类
多环境启动命令格式
带参数启动SpringBoot
java -jar springboot.jar --spring.profiles.active=test
Maven与SpringBoot多环境兼容
1.Maven中设置多环境属性
<profiles>
<!--开发环境-->
<profile>
<id>dev</id>
<properties>
<profile.active>dev</profile.active>
</properties>
</profile>
<!--测试环境-->
<profile>
<id>test</id>
<properties>
<profile.active>test</profile.active>
</properties>
</profile>
<!--生产环境-->
<profile>
<id>pro</id>
<properties>
<profile.active>pro</profile.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
2.SpringBoot中引用Maven属性
spring:
profiles:
active: ${profile.active}
---
#开发
server:
port: 81
spring:
profiles: test
---
#生产
server:
port: 82
spring:
profiles: pro
---
#测试
server:
port: 80
spring:
config:
activate:
on-profile: dev
3.对资源文件开启默认占位符的解析
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<encoding>UTF-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
配置文件分类
整合第三方技术
整合JUnit
基于SpringBoot实现SSM整合
SpringBoot整合Spring(不存在)
SpringBoot整合SpringMVC(不存在)
SpringBoot整合MyBatis(主要)
1.创建模块,选择Spring初始化,并配置模块相关基础信息
2.选择当前模块需要使用的技术集(MyBatis、MySQL)
3.设置数据源参数
4.定义数据层接口与映射配置