一、简介
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(要小心版本冲突)
 
 
二、入门案例
创建新模块,选择Spring初始化,并配置模块相关基础信息

选择当前模块需要的依赖、Spring Boot版本

注意:Spring Boot 3.0 以上版本只支持JDK17
开发控制器类
@RestController @RequestMapping("/books") public class BookController { @GetMapping("/{id};") public String getById(@PathVariable Integer id) { System.out.println("id = " + id); return "getById"; }; };运行自动生成的Application结尾的类,运行后控制台输出启动信息

三、以 “入门案例” 初时Spring Boot
- 最简SpringBoot程序所包含的基础文件
- pom.xml文件
 - Application类
 
 
Spring程序与SpringBoot程序对比
| 类/配置文件 | Spring | SpringBoot | 
|---|---|---|
| pom文件中的坐标 | 手动添加 | 勾选添加 | 
| web3.0配置类 | 手动编写 | 无 | 
| Spring/SpringMVC配置类 | 手动编写 | 无 | 
| 控制器 | 手动编写 | 手动编写 | 
基于idea开发Spring Boot程序需要确保联网且能够加载到程序框架结构
若不使用idea,使用其他IDE,可前往Spring官网生成并下载项目。
四、SpringBoot项目快速启动
- 对SpringBoot项目打包(执行Maven构建指令package)
 - 进入打包好的jar文件所在目录运行cmd,使用
jar -jar xxx.jar命令启动项目 
五、配置
1. 基础配置
e.g.:更改端口号
properties文件
server.port=80yaml文件
server: port: 80yml
同yml文件写法相同
当三种配置文件同时存在时,优先加载properties文件,其次是yml文件,最后是yaml文件
各种配置的优先级请参见官方文档:
yaml:YAML(YAML Ain’t Markup Language),一种数据序列化格式
优点:
- 容易阅读
 - 容易与脚本语言交互
 - 以数据为核心,重数据轻格式
 YAML文件扩展名:
- yml(主流)
 - yaml
 yaml语法规则:
大小写敏感
属性层级关系使用多行描述,每行结尾使用冒号结束
使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tb键)
属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
# 表示注释
核心规则:数据前面要加空格与冒号隔开
数组数据在数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分隔,如:
people: name: Cikian age: 22 hobby: - basketball - swimming - guitar
2. 读取配置
使用
@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名…},如:@Value("${server.port};") private Integer port;封装全部数据到Environment对象,使用
getProperty方法读取数据:@Autowired private Environment environment; @GetMapping("/{id};") public String getById(@PathVariable Integer id) { System.out.println(environment.getProperty("lesson")); System.out.println(environment.getProperty("server.port")); System.out.println(environment.getProperty("person.subject[1]")); return "String"; };自定义对象封装指定数据(常用)
自定义实体类:
public class Person { private String name; private Integer age; private String[] subject; // 省略setter和getter方法 };使用
@ConfigurationProperties(prefix = "属性名称")注解指定注入的数据:@Component @ConfigurationProperties(prefix = "person") public class Person { private String name; private Integer age; private String[] subject; // 省略setter和getter方法 };使用时直接自动装配此实体类即可
自定义对象封装数据警告解决办法:
在pom文件中加入下方依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
3. 多环境开发
yaml配置文件:
# 设置启用的环境 spring: profiles: active: dev --- # 开发环境 spring: config: activate: on-profile: dev server: port: 80 --- # 生产环境 spring: config: activate: on-profile: pro server: port: 81 --- # 测试环境 spring: config: activate: on-profile: test server: port: 82 --- # 公共配置 logging: level: root: info lesson: SpringBoot-Cikian person: name: Cikian age: 22 tel: 12345678910 subject: - java - Vue - MySql在SpringBoot 2.4之后的版本,只可以使用上面的格式定义环境名称;在2.4之前的版本,还可以使用下面的格式定义环境名称:
spring: profiles: dev #spring: # config: # activate: # on-profile: devproperties配置文件:
主配置文件(application.properties):
# 设置启动的环境 spring.profiles.active=devdev环境(application-dev.properties):
server.port=8080…
其他环境省略
其他环境的配置文件名称固定格式:
application-环境名称.properties
多环境启动命令:
java -jar xxx.jar --spring.profiles.active=环境名称同时,启动命令中可以修改其他参数,如修改项目启动端口为8888:
java -jar xxx.jar --server.port=8888
4. Maven-与SpringBoot多环境兼容
Maven中设置多环境属性
<profiles> <profile> <id>dev</id> <properties> <profiles.active>dev</profiles.active> </properties> </profile> <profile> <id>pro</id> <properties> <profiles.active>pro</profiles.active> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>test</id> <properties> <profiles.active>test</profiles.active> </properties> </profile> </profiles>SpringBoot中引用Maven属性
spring: profiles: active: ${profiles.active};修改pom文件对资源文件开启对默认占位符的解析
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.2.0</version> <configuration> <useDefaultDelimiters>true</useDefaultDelimiters> <encoding>UTF-8</encoding> </configuration> </plugin>
5. 配置文件分级
SpringBoot中4级配置文件
1级:file: config/application.yml(优先级最高)
jar包所在目录的config目录下的application.yml配置文件
2级:file: application.yml
jar包所在目录的application.yml配置文件
3级:classpath: config/application.yml
类路径下的config目录下的application.yml配置文件
4级:classpath: application.yml(优先级最低)
类路径下的application.yml配置文件
1、2级是为开发环境服务的,3、4级是为生产环境服务
作用:
- 1级与2级留做系统打包后设置通用属性
 - 3级与4级用于系统开发阶段设置通用属性
 
六、整合第三方技术
1. 整合JUnit
名称:
@SpringBootTest类型:测试类注解
位置:测试类定义上方
作用:设置JUnit加载的SpringBoot启动类
范例:
@SpringBootTest(classes = SpringBootJmApplication.class) class SpringBootJmApplicationTests { @Autowired private BookService bookService; @Test void contextLoads() { bookService.save(); }; };相关属性:设置SpringBoot启动类
注意事项:如果测试类在SpringBoot启动类的包或子包中,可以省略启动类的设置,也就是省略classes的设定,一般情况下会省略
2. 整合MyBatis
创建项目时,添加依赖:MyBatis Framework 和 MySQL Driver
设置数据源参数:
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/ssm_db?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC username: root password: 123456 type: com.alibaba.druid.pool.DruidDataSourceSpringBoot版本低于2.4.3(不含),MySQL驱动版本大于8.0时,需要在url连接串中配置时区
jdbc:mysq1://localhost:3306/ssm db?serverTimezone=UTC,或在MySQL数据库端配置时区解决此问题使用
@Mapper接口定义数据层接口与映射配置@Mapper public interface BookDao { @Select("select id,type,name,description from tbl_book where id=#{id};") public Book getById(Integer id); };
七、案例(SSM整合案例修改为SpringBoot项目)
Github仓库:SpringBoot整合SSM图书增删改查案例
