学习目标
- 能够基于idea正确搭建SpringBoot工程
- 能够说出起步依赖(starter)的作用
- 能够说出SpringBoot工程的三种配置文件格式
- 能够书写格式正确的yml配置文件
- 能够配置多种环境并在需要时选择使用何种环境运行
- 能够基于SpringBoot整合JUnit
- 能够基于SpringBoot整合SSM
一、SpringBoot简介
1. 入门案例
问题导入
SpringMVC的HelloWord程序大家还记得吗?
SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程
原生开发SpringMVC程序过程
1.1 入门案例开发步骤
创建maven工程
pom.xml中最简SpringBoot程序所包含的基础文件
<groupId>com.itheima</groupId>
<artifactId>spring-boot-quck-start</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 1.使用spring-boot的父模块 -->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.5.0</version>
</parent>
<dependencies>
<!-- 2. 使用web的起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
③:开发控制器类
package com.itheima.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/demo1")
public class Demo1QuckStartController {
@RequestMapping("/hello")
public String hello() {
System.out.println("你好!Spring Boot!");
return "Hello, Spring Boot!";
}
}
④:在com.itheima目录下创建Spring的启动程序
/**
* 1.类命名约定:以Application结尾
* 2.这个类应该放在其它所有类的同级包或父包中
* 3.启动类需要添加注解 @SpringBootApplication
*/
@SpringBootApplication
public class QuickStartApplication {
public static void main(String[] args) {
//启动Spring的应用程序: 1. 启动类的类名 2. main函数上参数
SpringApplication.run(QuickStartApplication.class, args);
}
}
工程结构
执行结果
Spring程序与SpringBoot程序对比
注意事项:
基于idea开发SpringBoot程序需要确保联网且能够加载到程序框架结构
1.2 SpringBoot项目快速启动
注意事项:
jar支持命令行启动需要依赖maven插件支持,请确认打包时是否具有SpringBoot对应的maven插件。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
① 对SpringBoot项目打包(执行Maven构建指令package)
② 执行启动指令
java -jar spring-boot-quick-start-0.0.1-SNAPSHOT.jar # 项目的名称根据实际情况修改
运行效果
2. SpringBoot概述
问题导入
学习了SpringBoot入门案例之后,感觉对比SpringMVC哪一个更加方便简洁?
- SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程
Spring程序缺点
- 配置繁琐
- 依赖设置繁琐
SpringBoot程序优点
- 自动配置
- 起步依赖(简化依赖配置)
- 辅助功能(内置服务器,……)
2.1 起步依赖
starter
- SpringBoot中常见项目名称,定义了当前项目使用的所有项目坐标,以达到减少依赖配置的目的
<?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>
</parent>
<groupId>com.itheima</groupId>
<artifactId>springboot-01-quickstart</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.5.0</version>
<packaging>pom</packaging>
<properties>
<servlet-api.version>4.0.1</servlet-api.version>
...
</properties>
</project>
parent
- 所有SpringBoot项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.5.0</version>
</parent>
<artifactId>spring-boot-starter-parent</artifactId>
<packaging>pom</packaging>
...
</project>
实际开发
- 使用任意坐标时,仅书写GAV中的G和A,V由SpringBoot提供
- 如发生坐标错误,再指定version(要小心版本冲突)
<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2.2 辅助功能
- SpringBoot程序启动
@SpringBootApplication
public class SpringBootQuickStartApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootQuickStartApplication.class, args);
}
}
SpringBoot在创建项目时,采用jar的打包方式
SpringBoot的引导类是项目的入口,运行main方法就可以启动项目
使用maven依赖管理变更起步依赖项
2.3 分析SpringBoot的自动配置:以tomcat启动为例
查看自动配置的spring-boot-autoconfigure的包下的配置文件spring.factories
文件中包含所有Web容器(Tomcat)自动启动的配置类
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\
找到Tomcat的启动类
进入ServerProperties类中查看代码,可以看到端口号的set方法
public void setPort(Integer port) { this.port = port; }
在ServerProperties类中存在一个静态内部类Tomcat,配置了服务器的属性
查看默认配置:spring-configuration-metadata.json文件,大约在1213行
{ "name": "server.port", "type": "java.lang.Integer", "description": "Server HTTP port.", "sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties", "defaultValue": 8080 }
二、基础配置
1. 配置文件格式
问题导入
框架常见的配置文件有哪几种形式?
1.1 修改服务器端口
http://localhost:8080/books/1 >>> http://localhost/books/1
SpringBoot提供了多种属性配置方式
- application.properties
server.port=80
- application.yml
server:
port: 81
- application.yaml
server:
port: 82
1.2 自动提示功能消失解决方案
先创建Spring的启动类,即包含了main函数和@SpringBootApplication的那个类。如果已经存在生成则忽略这步。
选择File -> Project Structure
- 选择Modules
点右边的加号
右边会出现当前模块的配置文件
如果没有出现相应的配置文件,则点最右边的绿色按钮
出现对话框,点左边的加号,添加新的配置文件
选择相应的配置文件
选择以后窗口变成如下:
点确定以后显示以下窗口
项目结构中文件前面的图片变成绿叶,就会有提示了。
1.3 SpringBoot配置文件加载顺序(了解)
- application.properties > application.yml > application.yaml
注意事项:
- SpringBoot核心配置文件名为application
- SpringBoot内置属性过多,且所有属性集中在一起修改,在使用时,通过提示键+关键字修改属性
2. yaml
问题导入
什么是yaml,和properties有什么区别?
- YAML(YAML Ain’t Markup Language),一种数据序列化格式
优点:
- 容易阅读
- 容易与脚本语言交互
- 以数据为核心,重数据轻格式
YAML文件扩展名
- .yml(主流)
- .yaml
2.1 yaml语法规则
- 大小写敏感
- 属性层级关系使用多行描述,每行结尾使用冒号结束
- 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)
- 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
表示注释
- 核心规则:数据前面要加空格与冒号隔开
2.2 yaml数组数据
- 数组数据在数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分隔
2.3 yaml数据读取
- 使用@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名……}
注:这种方式只能注入单个数值
- 封装全部数据到Environment对象
注:数组元素也只能一个个取出来
- 自定义对象封装指定数据【常用】
- 将对象添加Spring容器中,在类上添加@Component注解
- 在类上添加@ConfigurationProperties(prefix=”指定前缀”)
- 添加get和set方法,toString方法
- 在控制器中注入下面Enterprise对象
@Component
@ConfigurationProperties(prefix = "enterprise")
@Data
public class Enterprise {
private String name;
private Integer age;
private String tel;
private String subject[];
}
注:如果使用lombok需要在pom.xml中导入坐标
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
- 当输入@ConfigurationProperties注解的时候,自定义对象封装数据警告解决方案
在pom.xml文件添加以下
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
3. 多环境开发配置
问题导入
在实际开发中,项目的开发环境、测试环境、生产环境的配置信息是否会一致?如何快速切换?
3.1 多环境启动配置
- yaml文件多环境启动
基本配置
新的写法
application.yml
spring:
profiles:
active: pro
---
spring:
config:
activate:
on-profile: pro
server:
port: 80
---
spring:
config:
activate:
on-profile: test
server:
port: 81
---
spring:
config:
activate:
on-profile: dev
server:
port: 82
- properties文件多环境启动
#主启动配置文件 application.properties
spring.profiles.active=pro
#环境分类配置文件 application-pro.properties
server.port=80
#环境分类配置文件 application-dev.properties
server.port=81
#环境分类配置文件application-test.properties
server.port=82
3.2 多环境启动命令格式
- 带参数启动SpringBoot
# 指定哪个配置名
java –jar springboot.jar --spring.profiles.active=test
# 指定具体的参数
java –jar springboot.jar --server.port=88
# 同时指定配置名 端口号
java –jar springboot.jar --server.port=88 --spring.profiles.active=test
3.3 多环境开发控制
Maven与SpringBoot多环境兼容(步骤)
先将application.properties中配置全部先注释了
- Maven中设置多环境属性
<profiles>
<profile>
<id>dev_env</id>
<properties>
<profile.active>dev</profile.active>
</properties>
<!-- 默认激活 -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>pro_env</id>
<properties>
<profile.active>pro</profile.active>
</properties>
</profile>
<profile>
<id>test_env</id>
<properties>
<profile.active>test</profile.active>
</properties>
</profile>
</profiles>
- SpringBoot中引用Maven属性
- 对资源文件开启对默认占位符的解析
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>utf-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
</build>
- 启动查看控制台输出的结果
注:如果application-dev.properties中的配置也存在,则优先使用这里面的配置,再使用yml中的配置
三、整合第三方技术
1. 整合JUnit
问题导入
回忆一下Spring整合JUnit的步骤?
1.1 Spring整合JUnit(复习)
1.2 SpringBoot整合JUnit
准备数据:创建业务层和实现类
package com.itheima.service;
public interface BookService {
/**
* 保存书籍
*/
void save();
}
package com.itheima.service.impl;
import com.itheima.service.BookService;
import org.springframework.stereotype.Service;
@Service
public class BookServiceImpl implements BookService {
/**
* 保存书籍
*/
@Override
public void save() {
System.out.println("业务层:保存书籍");
}
}
【第一步】添加整合junit起步依赖(可以直接勾选)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
【第二步】编写测试类
@SpringBootTest
public class BookTest {
@Autowired
private BookService bookService;
@Test
public void testSave() {
bookService.save();
}
}
2. 基于SpringBoot实现SSM整合
问题导入
回忆一下Spring整合MyBatis的核心思想?
2.1 SpringBoot整合MyBatis
- SpringBoot整合Spring(不存在)
- SpringBoot整合SpringMVC(不存在)
- SpringBoot整合MyBatis(主要)
使用idea的开发向导
- 创建新模块,选择Spring初始化,并配置模块相关基础信息
可以换成阿里云的URL:https://start.aliyun.io
- 选择当前模块需要使用的技术集
可以删除多余的文件,只保留必须的
删除后的结构
修改pom.xml文件如下:
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.5.0</version>
</parent>
<dependencies>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</version>
<scope>runtime</scope>
</dependency>
</dependencies>
配置application.yml,如果没有提示信息,按前面的步骤配置
设置数据源参数,使用之前的ssm_db数据库
# 数据源的配置,可以指定为druid的连接池
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
url: jdbc:mysql:///ssm_db
type: com.alibaba.druid.pool.DruidDataSource
# 显示sql语句
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
注意事项:SpringBoot版本低于2.4.3(不含),Mysql驱动版本大于8.0时,需要在url连接串中配置时区,或在MySQL数据库端配置时区解决此问题
jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
- 定义数据层接口与映射配置
@Data
public class Book {
private Integer id;
private String type;
private String name;
private String description;
}
- 接口上添加@Mapper 注解
另一种做法:在SpringBoot的启动类上使用@MapperScan(“com.itheima.dao”)注解,做一次就可以了
@Mapper
public interface BookDao {
@Select("select * from tbl_book where id=#{id}")
Book getById(Integer id);
}
- 测试类中注入dao接口,测试功能。如果bookDao下有红线,如果能正常执行,则忽略。
@SpringBootTest
class SpringBootMybatisApplicationTest {
@Autowired
private BookDao bookDao;
@Test
public void testGetById() {
Book book = bookDao.getById(1);
System.out.println(book);
}
}
- 将启动类向上移动到com.itheima这一级
2.2 案例-SpringBoot实现ssm整合
- 在上面的SpringBoot工程,添加SpringMVC依赖。
<!-- springmvc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 复制原型目录下SpringMVC工程各种资源(主java类、页面、测试类)到static目录下
- 在static目录中提供index.html页面,跳转到”pages/books.html”
<script>
location.href="pages/books.html"
</script>
- 其它的Java代码直接复制到相应的目录下即可