1、SpringBoot 简介
1.1 SpringBoot 快速入门
1.1.1 开发步骤
- 创建新模块,选择Spring初始化,并配置模块相关基础信息
- 选择当前模块需要使用的技术集
- 开发控制器类
- 运行自动生成的Application类
1.1.1.1 创建新模块
- 选择 Spring Initializr ,用来创建 SpringBoot 工程
- 打包方式这里需要设置为 Jar
- 选中 Web,然后勾选 Spring Web
- Java 版本选 Java8
- pom.xml 文件预览
- 需要注意:
-
- 父包 spring-boot-starter-parent 的版本手动更改为了 2.6.3,jdk版本1.8 与 spring boot 3.0.0 版本不匹配,会报错
- <java.version> 修改为了1.8
- 4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.3 priv.dandelion springboot_01_quickstart 0.0.1-SNAPSHOT 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
1.1.1.2 创建 Controller
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping("/{id}")
public String getById(@PathVariable Integer id) {
System.out.println("id = " + id);
return "hello boot!";
}
}
1.1.1.3 启动服务器
直接运行引导类即可
1.1.2 开发内容对比
- 坐标
-
- Spring 程序中的坐标需要自己编写,而且坐标非常多
- SpringBoot 程序中的坐标是我们在创建工程时进行勾选自动生成的
- web3.0配置类
-
- Spring 程序需要自己编写这个配置类。这个配置类大家之前编写过,肯定感觉很复杂
- SpringBoot 程序不需要我们自己书写
- 配置类
- 注意:基于Idea的 Spring Initializr 快速构建 SpringBoot 工程时需要联网。
-
- Spring/SpringMVC 程序的配置类需要自己书写。而 SpringBoot 程序则不需要书写。
1.1.3 官网构建工程
SpringBoot官网:
spring.io/projects/sp…
- 点击下方的 Quickstart 中的 Spring Initializr,开始快速创建项目,如下图所示
1.1.4 SpringBoot 工程快速启动
1.1.4.1 问题引入
SpringBoot 程序服务器运行在本机当进行前后端联调时,按理说前端需要连接后端开发的机器,比较麻烦是否有更好的方式?
- 后端可以将 SpringBoot 工程打成 jar 包
-
- 该 jar 包运行不依赖于 Tomcat 和 Idea也可以正常运行
- 这个 jar 包在运行过程中连接和我们自己程序相同的 Mysql 数据库即可
1.1.4.2 打包
- 首先必须在 pom.xml 中配置如下插件
- org.springframework.boot spring-boot-maven-plugin
- 使用 Maven 的 package 指令打包就会在 target 目录下生成对应的 Jar 包
- 注意:
-
- 在运行 package 指令之前,最好先运行一次clean指令,防止出现问题
- 如果配置文件中包含中文,最好在设置中设置编码为 UTF-8 并重新检查配置文件中的中文,避免出现乱码问题导致配置文件无法使用,具体操作如下
1.1.4.3 启动
- 进入 jar 包所在目录,使用 cmd 输入命令
- java -jar springboot_01_quickstart-0.0.1-SNAPSHOT.jar
1.2 SpringBoot 概述
原始 Spring 环境搭建和开发存在以下问题:配置繁琐依赖设置繁琐
SpringBoot 程序优点恰巧就是针对 Spring 的缺点自动配置。这个是用来解决 Spring 程序配置繁琐的问题起步依赖。这个是用来解决 Spring 程序依赖设置繁琐的问题辅助功能(内置服务器,...)。在启动 SpringBoot 程序时既没有使用本地的 tomcat 也没有使用 tomcat 插件,而是使用 SpringBoot 内置的服务器。
1.2.1 起步依赖 —— 核心:Maven 继承
以后需要使用技术,只需要引入该技术对应的起步依赖即可
- 使用 Spring Initializr 方式创建的 Maven 工程的的 pom.xml 配置文件中自动生成了很多包含 starter 的依赖,这些以来就是启动依赖,如下
- org.springframework.boot spring-boot-starter-parent 2.7.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test
1.2.1.1 探索父工程(源码不表)
- 进入父工程 spring-boot-starter-parent 中,其也有一个父工程
- 进入 spring-boot-starter-parent 的父工程 spring-boot-dependencies 中,其中包含:
-
- <properties...> 标签中定义了各个技术软件依赖的版本,避免了我们在使用不同软件技术时考虑版本的兼容问题。在 properties 中可以找到各种技术的版本。
- <dependencyManagement...> 标签是进行依赖版本锁定,但是并没有导入对应的依赖;如果我们工程需要那个依赖只需要引入依赖的 groupid 和 artifactId 不需要定义 version。
- <build...> 标签中对插件的版本进行了锁定
1.2.1.2 探索依赖
- 本工程中添加了如下依赖
- org.springframework.boot spring-boot-starter-web
- 可以看到 spring-boot-starter-web 引入了如下依赖
-
- 引入了 spring-web 和 spring-webmvc 的依赖,这就是为什么我们的工程中没有依赖这两个包还能正常使用 springMVC 中的注解的原因。
- 而依赖 spring-boot-starter-tomcat ,从名字基本能确认内部依赖了 tomcat,所以我们的工程才能正常启动。
- org.springframework.boot spring-boot-starter 2.7.8 compile org.springframework.boot spring-boot-starter-json 2.7.8 compile org.springframework.boot spring-boot-starter-tomcat 2.7.8 compile org.springframework spring-web 5.3.25 compile org.springframework spring-webmvc 5.3.25 compile
1.2.1.3 小结
- starter
-
- SpringBoot 中常见项目名称,定义了当前项目使用的所有项目坐标,以达到减少依赖配置的目的
- parent
-
- 所有 SpringBoot 项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的
- 实际开发
-
- 使用任意坐标时,仅书写GAV中的G和A,V由SpringBoot提供
- G:groupid
- A:artifactId
- V:version
- 如发生坐标错误,再指定version(要小心版本冲突)
1.2.2 程序启动
- 引导类
- @SpringBootApplication public class Springboot01QuickstartApplication { public static void main(String[] args) { SpringApplication.run(Springboot01QuickstartApplication.class, args); } }
- 注意
-
- SpringBoot 在创建项目时,采用 jar 的打包方式
- SpringBoot 的引导类是项目的入口,运行 main 方法就可以启动项目
- 因为 pom.xml 中配置了 spring-boot-starter-web 依赖,而该依赖中依赖 tomcat ,所以运行 main 方法就可以使用 tomcat 启动工程。
1.2.3 切换 web 服务器
- 目前启动工程使用的是 tomcat 服务器,spring-boot-starter-web 依赖中依赖了 tomcat
- 如果要使用其他服务器就需要将 tomcat 排除,更换为 jetty
- org.springframework.boot spring-boot-starter-web spring-boot-starter-tomcat org.springframework.boot org.springframework.boot spring-boot-starter-jetty
2、配置文件
2.1 配置文件格式及优先级
当三种配置文件均存在时,优先级application.properties > application.yml >
application.yamlSpringBoot 程序的配置文件名必须是 application ,只是后缀名不同
- application.properties
- server.port=80
- application.yml(常用)
- server: port: 81
- application.yaml
- server: port: 82
2.2 yaml 格式
2.2.1 概述
- 特点
-
- YAML(YAML Ain't Markup Language),一种数据序列化格式。
- 相比于 xml 和 properties 而言重数据,轻格式
- 优点
-
- 容易阅读
-
- yaml 类型的配置文件比 xml 类型的配置文件更容易阅读,结构更加清晰
- 容易与脚本语言交互
- 以数据为核心,重数据轻格式
-
- yaml 更注重数据,而 xml 更注重格式
- 扩展名
-
- .yml (主流)
- .yaml
2.2.2 语法规则
- 规则
-
- 大小写敏感
- 属性层级关系使用多行描述,每行结尾使用冒号结束
- 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键),空格的个数并不重要,只要保证同层级的左侧对齐即可。
- 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
-
表示注释
- 示例
-
层级关系 enterprise: name: dandelion age: 16 tel: 12345456767 # 数组格式 subject: - Java - C++ - 算法
2.3 yaml 配置文件数据读取
2.3.1 环境准备
- 准配配置文件 application.yaml
- lesson: SpringBoot server: port: 80 enterprise: name: dandelion age: 16 tel: 12345456767 subject: - Java - C++ - 算法
2.3.2 读取配置数据
2.3.2.1 使用 @Value 注解
直接使用 @Value("{ }包裹的数据名称的字符串
- Controller 示例
- @RestController @RequestMapping("/books") public class BookController { // 读取数据 @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 boot!"; } }
2.3.2.2 Environment 对象
用于获取全部数据,使用时对Environment类型的属性进行自动装配,使用其getProperty()方法来获取数据
- Controller 示例
- import org.springframework.core.env.Environment; // import ... @RestController @RequestMapping("/books") public class BookController { // 获取全部数据,注入到Environment类型中 @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.age")); System.out.println(environment.getProperty("enterprise.subject[1]")); return "hello boot!"; } }
2.3.2.3 自定义对象
该方式用于读取任意数据,将其封装为实体类,使用注解绑定获取数据的范围,在获取时,通过自动注入获取该实体类对象并进行操作
- 定义数据的实体类,和配置文件中保持一致,并实现其 Getter 和 Setter
-
- 后续要使用自动装配,故要添加 @Component 注解,将这个类交给 Spring 管理
- 使用@ConfigurationProperties(prefix = "enterprise")注解,prefix属性值表示读取配置文件的哪一部分数据,此处代表读取 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) + '}'; } }
- Controller 示例
- @RestController @RequestMapping("/books") public class BookController { @Autowired private Enterprise enterprise; @GetMapping("/{id}") public String getById(@PathVariable Integer id) { System.out.println(enterprise); return "hello boot!"; } }
2.4 多环境配置
2.4.1 yaml 文件
- 简单书写
-
- 使用spring.profiles设定环境的名称
- 使用---将不同的环境进行分隔
- 使用spring.profiles.active设置启动项目时使用的环境
- 公用的配置可以写在第一部分中
- #设置启用的环境 spring: profiles: active: test --- #开发环境 spring: profiles: dev server: port: 80 --- #测试环境 spring: profiles: test server: port: 81 --- #生产环境 spring: profiles: pro server: port: 82
- 标注书写
- 简单书写部分中,设定环境的名称的spring.profiles的书写格式并不规范,规范的书写格式如下
- #开发环境 spring: config: activate: on-profile: dev server: port: 80
2.4.2 properties 文件
与 yaml 文件不同,properties 的多环境配置写在不同的文件中,并在主配置文件中指定使用的环境多个环境使用文件名进行区分和定义,application-环境名.properties公用的配置可以写application.properties中
- 主配置文件application.properties
- #设置启用的环境 spring.profiles.active=dev
- 开发环境和测试环境
-
- 开发环境application-dev.properties
- server.port=80
- 测试环境application-test.properties
- server.port=81
2.4.3 命令行启动参数配置
- 命令行临时配置
- 可以在启动指令后添加参数来临时覆盖配置文件中的内容,参数可以有多个,每一个参数的格式为-- 空格 使用.连接的配置属性名称=属性值,如下
- java -jar springboot_01_quickstart-0.0.1-SNAPSHOT.jar -- spring.profiles.active=test -- server.port=88
- 配置的优先级
-
- 如上,命令中添加参数的优先级大于原本配置文件的优先级
- SpringBoot 官网对于优先级已经进行了说明,参见:官方文档:Externalized Configuration中文手册:外化配置
2.5 配置文件分类
- 命令行启动参数配置存在问题
-
- 由于测试环境和开发环境的很多配置都不相同,所以测试人员在运行我们的工程时需要临时修改很多配置,可能参数过多,过于复杂
- 解决方案:额外的配置类
-
- SpringBoot 配置文件中 4 级配置文件位置及其优先级(优先级逐级提升):
-
- 1级:classpath:application.yml 【最低】
- 2级:classpath:config/application.yml
- 3级:file :application.yml
- 4级:file :config/application.yml 【最高】
- 说明
-
- classpath 指的是项目的 resources 目录下,file 指的是打好的 jar 包所在的目录下
- file 中的配置文件一般用于系统打包后临时设置通用属性,classpath 中的配置文件一般用于开发阶段设置通用属性
- file 中的配置文件的优先级高于 classpath 中的配置文件
- 同一分类的配置中,config 文件夹中的配置文件的优先级高于 config文件夹外的配置文件
3、SpringBoot 整合 Junit
Spring 整合 Junit 回顾
3.1 环境准备
- 创建新的模块,不需要依赖
- Service 实现类(接口不表)
- @Service public class BookServiceImpl implements BookService { @Override public void save() { System.out.println("book save!"); } }
3.2 测试类编写
- 测试类中(@SpringBootTest 修饰),将 BookService 注入
- 如果测试类和引导类的包名不一致,需要为@SpringBootTest 的 class 属性手动指定引导类的字节码对象,如@SpringBootTest(classes = Springboot02TestApplication.class)
- @SpringBootTest class Springboot02TestApplicationTests { @Autowired private BookService bookService; @Test void contextLoads() { bookService.save(); } }
4、SpringBoot 整合 Mybatis
4.1 回顾 Spring 整合 Mybatis
- Spring 整合 MyBatis
-
- 整合思路
- 整合步骤
4.2 SpringBoot 整合Mybatis
4.2.1 创建模块
- 依赖
-
- MyBatis Framework
- MySQL Driver
4.2.2 定义实体类
public class Book {
private Integer id;
private String name;
private String type;
private String description;
// Getter、Setter、toString...
}
4.2.3 定义 dao
数据库SQL见前置内容,tbl_book 表SQL,此处不再赘述
- dao (报错)
- public interface BookDao { @Select("select * from tbl_book where id = #{id}") public Book getById(Integer id); }
- 存在问题及解决方案
-
- 报错:No qualifying bean of type 'priv.dandelion.dao.BookDao' available
- 错误信息显示在 Spring 容器中没有 BookDao 类型的 bean
- Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'priv.dandelion.dao.BookDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1801) ~[spring-beans-5.3.25.jar:5.3.25] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1357) ~[spring-beans-5.3.25.jar:5.3.25] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311) ~[spring-beans-5.3.25.jar:5.3.25] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.25.jar:5.3.25] ... 74 common frames omitted
- 原因
-
- Mybatis 会扫描接口并创建接口的代码对象交给 Spring 管理,但是现在并没有告诉 Mybatis 哪个是 dao 接口
- 注意: Mysql驱动版本大于8.0时,需要在url连接串中配置时区 jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC,或在MySQL数据库端配置时区解决此问题
- 解决方案:在BookDao 接口上使用 @Mapper
- @Mapper public interface BookDao { @Select("select * from tbl_book where id = #{id}") public Book getById(Integer id); }
4.2.4 定义测试类
@SpringBootTest
class Springboot03MybatisApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void contextLoads() {
Book book = bookDao.getById(1);
System.out.println(book);
}
}
4.2.5 编写配置
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db
username: root
password: 123456
4.2.6 使用 Druid 数据源
- 添加依赖
- com.alibaba druid 1.1.16
- 添加配置,指定使用 Druid 数据源
- spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/ssm_db username: root password: 123456 type: com.alibaba.druid.pool.DruidDataSource
5、基于 SpringBoot 的 SSM 整合案例
本节内容是对上一篇 SSM 整合案例的改进,可参见SpingMVC 学习笔记 - 第二章 -SSM整合案例开发步骤pom.xml:配置起步依赖,必要的资源坐标(druid)application.yml:设置数据源、端口等配置类:现在不需要了dao:设置@Mapper测试类页面:放置在resources目录下的static目录中
5.1 创建工程
- 依赖
-
- Spring Web
- MyBatis Framework
- MySQL Driver
- 引入Druid
-
- 添加依赖
- com.alibaba druid 1.1.16
5.2 后端代码整理
- config 包,对比 SSM 项目全部删除,现在不需要了
- entity 包,实体,无变化
- dao,添加@Mapper注解
- // TODO 添加@Mapper @Mapper public interface BookDao { @Insert("insert into tbl_book values(null,#{type},#{name},#{description})") // @Insert("insert into tbl_book (type,name,description) values(#{type},#{name},#{description})") public int save(Book book); @Update("update tbl_book set type = #{type}, name = #{name}, description = #{description} where id = #{id}") public int update(Book book); @Delete("delete from tbl_book where id = #{id}") public int delete(Integer id); @Select("select * from tbl_book where id = #{id}") public Book getById(Integer id); @Select("select * from tbl_book") public List getAll(); }
- service,无变化
- exception,无变化
- controller,无变化
- 测试类,使用了 @SpringBootTest 注解,更换了使用的包
- // import org.junit.Test; import org.junit.jupiter.api.Test; // import ... //@RunWith(SpringJUnit4ClassRunner.class) //@ContextConfiguration(classes = SpringConfig.class) // TODO 将原先使用的注解更改为 @SpringBootTest @SpringBootTest public class BookServiceTest { @Autowired private BookService bookService; // TODO 原先使用的 @Test 注解 现在需要重新导包 @Test public void testGetById() { Book byId = bookService.getById(1); System.out.println(byId); } @Test public void testGetAll() { List all = bookService.getAll(); System.out.println(all); } }
5.3 配置文件
#TODO 配置端口和数据源相关信息
server:
port: 80
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db
username: root
password: 123456
5.4 静态资源
- 静态资源存放在 resources 目录下的 static 目录下
- 配置主页 index.html
5.5 包结构展示
5.6 其他问题
-
若再未加入前端代码时启动过服务器,加入前端代码后无法正常访问到页面时执行 Maven 的 clean 指令,再重新启动服务器(重新打包)即可
-
SpringBoot 版本问题、MySQL时区问题、项目无法构建、配置文件打包乱码等问题,本节笔记中均有提及,可向上查阅