SpringBoot简介
- SpringBoot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化 Spring 应用的初始搭建以及开发过程。
SpringBoot快速入门
开发步骤
SpringBoot 开发起来特别简单,分为如下几步:
- 创建新模块,选择Spring初始化,并配置模块相关基础信息
- 选择当前模块需要使用的技术集
- 开发控制器类
- 运行自动生成的Application类
创建新模块

image.png

image.png

image.png

image.png
创建 Controller
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println("id ==> "+id);
return "hello , spring boot!";
}
}

image.png
SpringBoot工程快速启动
打包
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
注意:该插件必须配置,不然打好的 jar 包也是有问题的。
启动
- 进入 jar 包所在位置,在 命令提示符 中输入如下命令
jar -jar springboot_01_quickstart-0.0.1-SNAPSHOT.jar
SpringBoot概述
- SpringBoot 程序优点恰巧就是针对 Spring 的缺点
1.自动配置。这个是用来解决 Spring 程序配置繁琐的问题
2.起步依赖。这个是用来解决 Spring 程序依赖设置繁琐的问题
3.辅助功能(内置服务器,...)。我们在启动 SpringBoot 程序时既没有使用本地的 tomcat 也没有使用 tomcat 插件,而是使用 SpringBoot 内置的服务器。
起步依赖
-
我们使用 Spring Initializr 方式创建的 Maven 工程的的 pom.xml 配置文件中自动生成了很多包含 starter 的依赖
image.png
小结
- starter
SpringBoot 中常见项目名称,定义了当前项目使用的所有项目坐标,以达到减少依赖配置的目的 - parent
所有 SpringBoot 项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的
spring-boot-starter-parent (2.5.0)与 spring-boot-starter-parent (2.4.6)共计57处坐标版本不同 - 实际开发
使用任意坐标时,仅书写GAV中的G和A,V由SpringBoot提供
G:groupid
A:artifactId
V:version
如发生坐标错误,再指定version(要小心版本冲突)
切换web服务器
- 现在我们启动工程使用的是 tomcat 服务器,那能不能不使用 tomcat 而使用 jetty 服务器, jetty 在我们 maven 高级时讲 maven 私服使用的服务器。而要切换 web 服务器就需要将默认的 tomcat 服务器给排除掉,怎么排除呢?使用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>
配置文件
配置文件格式
我们现在启动服务器默认的端口号是 8080 ,访问路径可以书写为
http://localhost:8080/books/1
在线上环境我们还是希望将端口号改为 80 ,这样在访问的时候就可以不写端口号了,如下
http://localhost/books/1
而 SpringBoot 程序如何修改呢? SpringBoot 提供了多种属性配置方式
- application.properties
server.port=80
- application.yml
server:
port: 81
注意: 在 : 后,数据前一定要加空格。
- application.yaml
server:
port: 82
- 在配合文件中如果没有提示,可以使用一下方式解决

image.png

image.png

image.png

image.png

image.png
三种配合文件的优先级
application.properties > application.yml > application.yaml
yaml格式
- YAML(YAML Ain't Markup Language),一种数据序列化格式。
- YAML 文件扩展名:
.yml (主流)
.yaml
上面两种后缀名都可以,以后使用更多的还是 yml 的。
语法规则
- 大小写敏感
- 属性层级关系使用多行描述,每行结尾使用冒号结束
- 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)
- 空格的个数并不重要,只要保证同层级的左侧对齐即可。
- 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
-
# 表示注释
核心规则:数据前面要加空格与冒号隔开
enterprise:
name: itcast
age: 16
tel: 4006184000
subject:
- Java
- 前端
- 大数据
yaml配置文件数据读取
读取配置数据
使用 @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对象
- 这种方式 SpringBoot 会将配置文件中所有的数据封装到 Environment 对象中,如果需要使用哪个数据只需要通过调用Environment 对象的 getProperty(String name) 方法获取
@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 中进行注入
Enterprise 实体类内容如下:
@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {
private String name;
private int age;
private String tel;
private String[] subject;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String[] getSubject() {
return subject;
}
public void setSubject(String[] subject) {
this.subject = subject;
}
@Override
public String toString() {
return "Enterprise{" +
"name='" + name + '\'' +
", age=" + age +
", tel='" + tel + '\'' +
", subject=" + Arrays.toString(subject) +
'}';
}
}
- BookController 内容如下:
@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!";
}
}

image.png
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
多环境配置
yaml文件
#设置启用的环境
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文件
- application-dev.properties 是开发环境的配置文件。我们在该文件中配置端口号为 80
server.port=80
- application-test.properties 是测试环境的配置文件。我们在该文件中配置端口号为 81
server.port=81
- application-pro.properties 是生产环境的配置文件。我们在该文件中配置端口号为 82
server.port=82
- SpringBoot 只会默认加载名为 application.properties 的配置文件,所以需要在 application.properties 配置文件中设置启用哪个配置文件,配置如下:
spring.profiles.active=pro
命令行启动参数设置
- 使用 SpringBoot 开发的程序以后都是打成 jar 包,通过 java -jar xxx.jar 的方式启动服务的。
- SpringBoot 提供了在运行 jar 时设置开启指定的环境的方式
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 中4级配置文件放置位置:
1级:classpath:application.yml
2级:classpath:config/application.yml
3级:file :application.yml
4级:file :config/application.yml
级别越高优先级越高
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)
SpringBoot整合mybatis
创建模块

image.png
-
选择当前模块需要使用的技术集(MyBatis、MySQL)
image.png
定义实体类
public class Book {
private Integer id;
private String name;
private String type;
private String description;
//setter and getter
//toString
}
定义dao接口
@Mapper
public interface BookDao {
@Select("select * from tbl_book where id = #{id}")
public Book getById(Integer id);
}
定义测试类
@SpringBootTest
class Springboot08MybatisApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void testGetById() {
Book book = bookDao.getById(1);
System.out.println(book);
}
}
编写配置
- 在 application.yml 配置文件中配置如下内容
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db
username: root
password: root
使用Druid数据源
- 导入 Druid 依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
- 在 application.yml 配置文件配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource

