概念:SpringBoot是用来简化Spring应用的初始搭建以及开发过程的框架
1.起步依赖
减少依赖配置:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Spring-boot-starter-test 中引入好了依赖,里面有web ,Tomcat 在tomcat中配置了默认端口号为8080
parent 减少依赖冲突
2.基础配置
2.1配置文件
配置文件优先级 properties>yml>ymal
2.2 yaml 语法规则
-
大小写敏感
-
属性层级关系使用多行描述,每行结尾使用冒号结束
-
使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)
-
属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
-
#表示注释
-
数组前面加 -
-
核心规则:数据前面要加空格与冒号隔开
application.yml
enterprise:
name: itcast
age: 12
subjects:
- java
- 大数据
- 前端
appilcation.yml配置文件是SpringBoot默认加载的配置文件
2.3yml数据读取
2.3.1@Value读取单个数据,${}
@Value("${enterprise.name}")
private String name;
@Value("${enterprise.age}")
private Integer age;
@Value("${enterprise.subjects[1]}")
private String subjects1;
2.3.2封装全部数据到environment对象
2.3.3自定义对再跟封装指定数据
![](https://i-blog.csdnimg.cn/blog_migrate/e7d8a477b64fe7ba3de0d53a73022958.png)
2.4多环境启动
配置文件
SpringBoot项目快速启动
带参数启动Springboot项目,命令行参数设置优先级大于配置数据
java –jar springboot.jar --server.port=88 --spring.profiles.active=test
pom.xml 中配置开发 测试 生产环境依赖
<activeByDefault>true</activeByDefault> 默认次环境生效
<!-- 开发环境-->
<profile>
<id>env_dev</id>
<properties>
<profile.active>dev</profile.active>
</properties>
</profile>
<!--测试环境-->
<profile>
<id>env_test</id>
<properties>
<profile.active>test</profile.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!--生产环境配置-->
<profile>
<id>env_pro</id>
<properties>
<profile.active>pro</profile.active>
</properties>
</profile>
</profiles>
想要在application.yml 中获取到该环境
profiles:
active: ${profile.active}
这样是错误的,并不会替换,应该改成
profiles:
active: @profile.active@
2.5配置文件分类
classpath放在resource中
file 第二级
一级二级做系统打包后设置通用属性 (解决二次发版问题,可以将配置文件跟jar包放到一起)
三级四级用于系统开发阶段设置通用属性
3.整合第三方技术
3.1整合Junit
spring中整合Junit时测试类要写 配置类
springboot 中只要在测试类中加入
SpringbootTest
3.2整合SSM
spring中整合MyBatis
Springboot整合MyBatis
创建SpringBoot脚手架时 设置技术集
书写配置文件
创建实体类-->创建Dao层
注意为lMyBatis能够扫描到Dao层记得加入@Mapper