文章目录
原始SpringMVC程序开发过程
创建maven工程,在pom.xml中配置依赖的坐标;SpringMVC的配置类,web的配置类;提供一个Controller
SpringBoot版
创建新模块,选择Spring初始化,并配置模块相关基础信息
选择当前模块需要使用的技术集(即添加起步依赖,如勾选web下的spring web)
开发controller
运行自动生成的引导类Application类
SpringBoot 简介
SpringBoot 是用于 简化 Spring应用的 初始搭建 和 开发过程
Spring缺点:配置繁琐,依赖设置导入繁琐
Boot优点:
- 自动配置
- 起步依赖(简化依赖配置)
Boot为每种技术提前写好了其所依赖的所有坐标,开发者只需要通过dependency导入你要用的技术的起步依赖(如spring-boot-starter-web),就可以简化其所依赖坐标的导入工作
Boot还提供了spring-boot-starter-parent,其中定义了若干坐标版本号,以减少依赖冲突
若要 - 辅助功能(内置服务器)
spring-boot-starter-web中内置了Tomcat的starter,因此项目中不用额外设置服务器
最简Boot程序包含的基础文件:pom.xml + Application类
SpringBoot项目创建要求必须联网
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 核心 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.itheima</groupId>
<artifactId>springboot_01_quickstart</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 创建时勾选spring web得到的 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
Application类
也叫引导类,是Boot项目创建后自动生成的,运行该类的 main 方法即可启动项目
@SpringBootApplication
public class Springboot01QuickstartApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot01QuickstartApplication.class, args);
}
}
更换添加技术
Boot为我们定义好了技术的组合类型,如 web 的 starter 中内置了 Tomcat 的服务器,但此时我们要用其他的服务器怎么办?用 exclusions 标签将 Tomcat 的 starter 从 web 的 starter 中排除,然后自己导 jetty 的 starter
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
SpringBoot项目快速启动
对SpringBoot项目打包(执行Maven构建指令package)
执行启动指令执行打好的jar包(java -jar springboot.jar)
注意:要使用java -jar去执行jar包,依赖于SpringBoot中的maven插件,因此打包时要确认项目pom.xml中包含SpringBoot对应的maven插件(插件要指定版本,否则会报错,版本和boot parent一致即可)
原始的maven项目也有package功能,但打出来的jar包没法用java -jar执行,这是因为
SpringBoot的Maven插件将我们的 项目 和 依赖的jar包 打到一起了,并设置了一个入口程序
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.0</version>
</plugin>
</plugins>
</build>
基础配置
SpringBoot 的相关配置在项目路径下的 resources 中的配置文件内编写
Boot工程的配置文件格式
主要有三种:application.properties、application.yml、application.yaml
主要用yml格式的
当三种同时存在时,生效优先级为:properties > yml > yaml
语法格式:
//properties
server.port=8080
//yml和yaml
server:
port: 80 //注意数据前和冒号间要有个空格
yaml 文件
yaml,一种数据序列化格式,易读性强;易与脚本语言交互;以数据为核心,重数据轻格式
yaml 文件扩展名有 .yaml 和 .yml,.yml 是主流
语法规则:
① 大小写敏感
② 属性层级关系用多行描述,每行结尾使用冒号结束
③ 缩进表示层## 标题级关系,同级左对齐,只能用空格(空格数量不限,对齐就行)
④ 属性值前加空格
⑤ # 为注释
数组数据,一个属性有多个成员值,每行以减号空格开头写一个数据
yaml 数据读取
-
@Value给单成员变量赋值获取单个数据。@Value{${一级属性名.二级属性名…}}
数组的用下标索引:@Value{${一级属性名.二级属性名…[9]}}@RestController @RequestMapping("/books") public class BookController { @Value("${lesson}") private String lesson; @Value("${server.port}") private Integer port; @Value("${enterprise.subject[0]}") private String subject_0; @GetMapping("/{id}") public String getById(@PathVariable Integer id){ System.out.println(lesson); System.out.println(port); System.out.println(subject_0); return "hello zjh"; } }
-
Environment对象封装数据。将yaml文件中的全部
import org.springframework.core.env.Environment; @RestController @RequestMapping("/books") public class BookController { @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("enterprise.subject[2]")); return "hello zjh"; } }
-
自定义对象封装指定数据。要将自定义对象作为bean管理,且绑定对象与配置文件中属性的关系
@Component // 让Spring管理 @ConfigurationProperties(prefix = "enterprise") // 指定本对象读取yaml文件中的哪个属性的数据 public class Enterprise { private String name; // 对应配置文件中的属性名 private Integer age; private String tel; private String[] subject; } @RestController @RequestMapping("/books") public class BookController { @Autowired private Enterprise enterprise; @GetMapping("/{id}") public String getById(@PathVariable Integer id){ System.out.println(enterprise); return "hello zjh"; } }
多环境开发配置
不同环境下(生产、开发、测试)项目的访问路径、账户、密码等一般是不同的,因此要针对不同环境做相应的配置,并可以切换使用
配置文件写法
yaml版本
# 设置启用的环境
spring:
profiles:
active: aaa
---
# 开发环境
spring:
profiles: dev
server:
port: 80
---
# 生产环境
spring:
profiles: pro
server:
port: 81
---
# 测试环境
spring:
profiles: test
server:
port: 82
properties版本
多环境启动命令格式
带参数启动SpringBoot,用临时参数覆盖配置中的属性
java -jar springboot_04_profile-0.0.1-SNAPSHOT.jar --spring.profiles.active=test
java -jar springboot_04_profile-0.0.1-SNAPSHOT.jar --server.port=88
多环境开发控制
当 Maven 和 Boot 中都配置了多环境开发,两者相遇以哪个为主呢?
maven最后要对项目进行打包,因此以maven的profile为主,boot为辅。maven控版本,boot加载maven中设定的版本
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<!-- 解析resource中设置的值,让yaml文件可以用${}读取 -->
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<!--开发环境-->
<profile>
<id>dev</id>
<properties>
<profile.active>dev</profile.active>
</properties>
</profile>
<!--生产环境(默认启动)-->
<profile>
<id>pro</id>
<properties>
<profile.active>pro</profile.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!--测试环境-->
<profile>
<id>test</id>
<properties>
<profile.active>test</profile.active>
</properties>
</profile>
</profiles>
# 设置启用的环境
spring:
profiles:
active: ${profile.active}
---
# 开发环境
spring:
profiles: dev
server:
port: 80
---
# 生产环境
spring:
profiles: pro
server:
port: 81
---
# 测试环境
spring:
profiles: test
server:
port: 82
多级配置文件
临时参数过多时,命令行+参数的启动方式就很不方便且容易出错
可以通过多级配置文件设置项目运行时的参数,不同级配置文件有不同的优先级
各级配置文件优先级降序排序如下:
jar包同级目录下–config文件夹中的配置文件
jar包同级目录下的配置文件
项目路径下–config文件夹中的配置文件
项目路径下的配置文件
jar包同级配置文件留作系统打包上线后设置通用属性
项目路径下的配置文件用作开发阶段设置通用属性
整合第三方技术
整合Junit(挖坑)
整合SSM(即SpringBoot整合MyBatis)
在Spring中,要使用mybatis,需要在配置类中配置SQL连接 (数据源和domain) 和操作映射 (dao)
SpringBoot中只需要用 @Mapper注解修饰dao的接口 即可完成配置
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface BookDao {
@Select("select * from tbl_book where id = #{id}")
public Book getById(Integer id);
}