1. SpringBoot简介
SpringBoot
是由 Pivotal
团队提供的全新框架,其设计目的是用来简化 Spring
应用的初始搭建以及开发过程
-
SpringBoot快速入门
-
开发步骤
- 创建新模块,选择Spring初始化,并配置模块相关基础信息
- 选择当前模块需要使用的技术集
- 开发控制器类
- 运行自动生成的Application类
代码之所以能简化,就是因为指定的父工程和Spring Web
依赖实现的
-
对比
-
官网构建工程
之所以能快速构建SpringBoot
工程,是因为Idea
使用了官网提供的快速构建SpringBoot
工程的组件实现的
SpringBoot官网,进入到SpringBoot
官网后拖到最下方点击Spring Initializr
超链接
-
SpringBoot工程快速启动
后端可以将SpringBoot
工程打成jar
包,该jar
包运行不依赖于Tomcat
和Idea
这些工具也可以正常运行
-
1打包
由于我们在构建SpringBoot
工程时已经在pom.xml
中配置了如下插件<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>
所以我们只需要使用
Maven
的package
指令打包就会在target
目录下生成对应的Jar
包。注意:该插件必须配置,不然打好的
jar
包也是有问题的。 -
2启动
进入jar
包所在位置,在命令提示符
中输入如下命令jar -jar springboot_01_quickstart-0.0.1-SNAPSHOT.jar
执行上述命令就可以看到
SpringBoot
运行的日志信息
-
-
-
SpringBoot概述
SpringBoot
主要作用:简化Spring
的搭建过程和开发过程
原始Spring
环境搭建和开发存在以下问题:- 配置繁琐
- 依赖设置繁琐
SpringBoot
程序优点恰巧就是针对Spring
的缺点- 自动配置。这个是用来解决
Spring
程序配置繁琐的问题 - 起步依赖。这个是用来解决
Spring
程序依赖设置繁琐的问题 - 辅助功能(内置服务器,…)。我们在启动
SpringBoot
程序时既没有使用本地的tomcat
也没有使用tomcat
插件,而是使用SpringBoot
内置的服务器。
-
起步依赖
-
starter:
SpringBoot
中常见项目名称,定义了当前项目使用的所有项目坐标,以达到减少依赖配置的目的 -
parent:所有
SpringBoot
项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的 -
实际开发:使用任意坐标时,仅书写GAV中的G和A,V由SpringBoot提供
G:groupid
A:artifactId
V:version -
如发生坐标错误,再指定version(要小心版本冲突)
-
-
程序启动
创建的每一个SpringBoot
程序时都包含一个类似于下面的类,我们将这个类称作引导类@SpringBootApplication public class Springboot01QuickstartApplication { public static void main(String[] args) { SpringApplication.run(Springboot01QuickstartApplication.class, args); } }
SpringBoot
在创建项目时,采用jar的打包方式SpringBoot
的引导类是项目的入口,运行main
方法就可以启动项目
-
切换web服务器
web
服务器默认为tomcat
服务器
切换web服务器:使用exclusion
标签<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <artifactId>spring-boot-starter-tomcat</artifactId> <groupId>org.springframework.boot</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>
在使用
SpringBoot
换技术时只需要导入该技术的起步依赖即可
2. 配置文件
-
配置文件格式
SpringBoot
提供了三种属性配置方式:-
application.properties
server.port=80
-
application.yml
server: port: 81#冒号后有空格
-
application.yaml
server: port: 82 #冒号后有空格
注意:
SpringBoot
程序的配置文件名必须是application
,只是后缀名不同而已
三种配置文件的优先级是:
application.properties
>application.yml
>application.yaml
-
-
yaml格式
-
YAML(YAML Ain’t Markup Language),一种数据序列化格式
-
优点:
-
容易阅读
yaml
类型的配置文件比xml
类型的配置文件更容易阅读,结构更加清晰 -
容易与脚本语言交互
-
以数据为核心,重数据轻格式
yaml
更注重数据,而xml
更注重格式
-
-
YAML 文件扩展名:
.yml
(主流).yaml
-
语法规则
-
大小写敏感
-
属性层级关系使用多行描述,每行结尾使用冒号结束
-
使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)
空格的个数并不重要,只要保证同层级的左侧对齐即可。 -
属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
-
# 表示注释
核心规则:数据前面要加空格与冒号隔开
数组数据在数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分隔,例如enterprise: name: itcast age: 16 tel: 4006184000 subject: - Java - 前端 - 大数据
-
-
-
读取yaml配置文件数据
application.yml
文件lesson: SpringBoot server: port: 80 enterprise: name: itcast age: 16 tel: 4006184000 subject: - Java - 前端 - 大数据
-
使用 @Value注解
引用方式:${一级属性名.二级属性名……}
@RestController @RequestMapping("/books") public class BookController { @Value("${lesson}") private String lesson; @Value("${server.port}") private Integer port; @Value("${enterprise.subject[0]}") private String subject_00; @GetMapping("/{id}") public String getById(@PathVariable Integer id){ System.out.println(lesson); System.out.println(port); System.out.println(subject_00); return "hello , spring boot!"; } }
-
Environment对象
@RestController @RequestMapping("/books") public class BookController { @Autowired private Environment env; @GetMapping("/{id}") public String getById(@PathVariable Integer id){ System.out.println(env.getProperty("lesson")); System.out.println(env.getProperty("enterprise.name")); System.out.println(env.getProperty("enterprise.subject[0]")); return "hello , spring boot!"; } }
注意:这种方式,框架内容大量数据,而在开发中我们很少使用。
-
自定义对象
- 将实体类
bean
的创建交给Spring
管理。
在类上添加@Component
注解 - 使用
@ConfigurationProperties
注解表示加载配置文件
在该注解中也可以使用prefix
属性指定只加载指定前缀的数据 - 在
BookController
中进行注入
@Component @ConfigurationProperties(prefix = "enterprise") public class Enterprise { private String name; private int age; private String tel; private String[] subject; //setter and getter //toString } @RestController @RequestMapping("/books") public class BookController { @Autowired private Enterprise enterprise; @GetMapping("/{id}") public String getById(@PathVariable Integer id){ System.out.println(enterprise.getName()); System.out.println(enterprise.getAge()); System.out.println(enterprise.getSubject()); System.out.println(enterprise.getTel()); System.out.println(enterprise.getSubject()[0]); return "hello , spring boot!"; } }
用第三种方式,在实体类上有如下警告提示
在pom.xml
中添加如下依赖<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
- 将实体类
-
-
多环境配置
-
yaml文件
在application.yml
中使用---
来分割不同的配置#设置启用的环境 spring: profiles: active: dev --- #开发 spring: profiles: dev server: port: 80 --- #生产 spring: profiles: pro server: port: 81 --- #测试 spring: profiles: test server: port: 82 ---
在上面配置中给不同配置起名字的
spring.profiles
配置项已经过时。最新用来起名字的配置项是#开发 spring: config: activate: on-profile: dev
-
properties文件
properties
类型的配置文件配置多环境需要定义不同的配置文件-
application-dev.properties
是开发环境的配置文件server.port=80
-
application-test.properties
是测试环境的配置文件server.port=81
-
application-pro.properties
是生产环境的配置文件server.port=82
SpringBoot
只会默认加载名为application.properties
的配置文件,所以需要在application.properties
配置文件中设置启用哪个配置文件,配置如下:spring.profiles.active=pro
-
-
命令行启动参数设置
java –jar xxx.jar --spring.profiles.active=test java –jar xxx.jar –-server.port=88 java –jar springboot.jar –-server.port=88 –-spring.profiles.active=test
配置的优先级:点击官网查看
-
-
配置文件分类
SpringBoot
定义了配置文件不同的放置的位置优先级不同
SpringBoot
中4级配置文件放置位置:- 1级:classpath:application.yml
- 2级:classpath:config/application.yml
- 3级:file :application.yml
- 4级:file :config/application.yml
4级最高
3. SpringBoot整合junit
回顾Spring
整合 junit
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class UserServiceTest {
@Autowired
private BookService bookService;
@Test
public void testSave(){
bookService.save();
}
}
SpringBoot
整合 junit
@SpringBootTest
class Springboot07TestApplicationTests {
@Autowired
private BookService bookService;
@Test
public void save() {
bookService.save();
}
}
注意:这里的引导类所在包必须是测试类所在包及其子包。
例如:
- 引导类所在包是
com.itheima
- 测试类所在包是
com.itheima
如果不满足这个要求的话,就需要在使用
@SpringBootTest
注解时,使用classes
属性指定引导类的字节码对象。如@SpringBootTest(classes = Springboot07TestApplication.class)
4. SpringBoot整合mybatis
回顾Spring
整合 Mybatis
需要定义很多配置类
-
SpringConfig
配置类- 导入
JdbcConfig
配置类 - 导入
MybatisConfig
配置类
- 导入
-
JdbcConfig
配置类- 定义数据源(加载properties配置项:driver、url、username、password)
-
MybatisConfig
配置类-
定义
SqlSessionFactoryBean
-
定义映射配置
-
SpringBoot整合mybatis
在 com.itheima.dao
包下定义 BookDao
接口,内容如下
@Mapper //重点
public interface BookDao {
@Select("select * from tbl_book where id = #{id}")
public Book getById(Integer id);
}
在 application.yml
配置文件中配置如下内容
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
SpringBoot
版本低于2.4.3(不含),Mysql驱动版本大于8.0时,需要在url连接串中配置时区 jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
,