一篇文章帶你掌握主流辦公框架——SpringBoot
在之前的文章中我們已經學習了SSM的全部內容以及相關整合
SSM是Spring的產品,主要用來簡化開發,但我們現在所介紹的這款框架——SpringBoot,卻是用來簡化Spring開發的框架
SpringBoot是由Pivowtal團隊提供的全新框架,其設計目的就是用來簡化Spring應用的初始搭建以及開發程序,用來簡化開發工具的工具,你是否已經滿懷期待~
溫馨提醒:在學習前請學習SSM內容以及Maven的高階內容(依賴傳遞)等內容
SpringBoot簡介
SpringBoot是由Pivotal團隊提供的全新框架,其設計目的就是用來簡化Spring應用的初始搭建以及開發程序
SpringBoot概述
SpringBoot是針對Spring的繁瑣程序進行優化而產生的框架
Spring程式缺點:
- 配置繁瑣
- 依賴設定繁瑣
SpringBoot程式優點:
- 自動配置
- 起步依賴(簡化依賴配置)
- 輔助功能(內置服務器等)
SpringBoot專案開發
我們通過一個簡單的SpringBoot案例和SSM案例的比較來展現SpringBoot的優勢
SSM框架構造
首先我們回憶一下SSM框架的基本構造圖:

我們來總結一些SSM框架必備的一些檔案:
- pom.xml配置檔案
- ServletConfig配置Java類
- SpringMvcConfig配置Java類
- Collector服務層Java檔案
SpringBoot框架構造
相對而言,我們的SpringBoot將SSM的框架內容隱藏起來,達到簡化框架的作用
我們下面來介紹創建一個SpringBoot框架的具體步驟:
- IDEA創建新專案,選擇SpringBoot框架,JDK選擇1.8版本(Default默認在網頁下載,需要聯網)

- 選擇Maven,Java,jar等相關選項,注意選擇Java8(目前SpringBoot只支持Java8的版本)

- 選擇Web中的SpringWeb,確保右側存在Spring Web選項(上方可選擇SpringBoot版本)

- 創建專案即可

- 洗掉無關專案,只保留src和pom.xml即可

- 我們僅需書寫一個Collector相關類即可
package com.itheima.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println("id ==> "+id);
return "hello , spring boot!";
}
}
- 點擊啟動Application.java檔案即可(由系統自動創建)
package com.itheima;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
以上至此,我們的SpringBoot專案就開發完畢了
除此之外,我們的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>
</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>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我們會發現需要我們書寫代碼的部分僅僅只有Collector這一部分,相比于SSM框架簡化了并非一點點
SSM框架與SpringBoot框架比較
我們將SSM框架與SpringBoot框架進行簡單的對比:
| 類/組態檔 | Spring | SpringBoot |
|---|---|---|
| pom檔案中的坐標 | 手工添加 | 勾選添加 |
| web3.0配置類 | 手工添加 | 無 |
| Spring/SpringMvc配置類 | 手工添加 | 無 |
| 控制器 | 手工添加 | 手工添加 |
我們可以明顯比較出兩者的顯著差距!
注意:基于IDEA開發的SpringBoot框架需要聯網到SpringBoot官網加載程式框架結構
非IDEA進行SpringBoot開發
我們在實際作業中,可能使用的開發工具并非只有IDEA
那么IDEA中存在有SpringBoot的開發架構,其他不包含SpringBoot開發架構選項的軟體就無法開發了嗎?
我們可以選擇到官網進行jar包下載直接匯入開發即可:
- 打開官網(官網地址:Spring Boot)

- 拉至頁面底部,找到快速開發標志,點擊進入創建界面

- 勾選相對應圖示,點擊創建即可

- 創建后會自動下載jar包,直接匯入所用軟體即可

SpringBoot快速啟動
我們在實際開發中,常常會做到前后端分離開發
那么我們的SpringBoot中所使用的服務器或開發軟體等是否還需要交付給前端呢
SpringBoot為我們提供了一種全新的服務器開啟方法,我們只需要將SpringBoot打包后交付給前端,前端就可直接進行開啟
- 專案打包

- 打包后在當前頁面采用cmd命令列輸入以下指令即可直接開啟服務器(注意需要在該jar包的檔案夾目錄下)
java -jar SpringBoot檔案包名.jar(可tab鍵補全)
注意點:
我們需要將所需的資料庫資訊交付給前端,因為SpringBoot只負責專案的開啟,與資料庫無關
該方法是由一種pom.xml中的插件支持的,請確保存在該插件(SpringBoot自動創建)
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
SpringBoot起步依賴
在簡單介紹SpringBoot的專案開發之后,你是否有疑惑為什么SpringBoot能夠省略如此多的資訊來直接開發
其實這一切都是源于SpringBoot的依賴的直接創建,我們稱之為起步依賴:
- parent起步依賴繼承
- starter起步依賴繼承
我們給出部分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>
<!----ctrl+左鍵 可以查看原始碼>
<!--Maven的繼承機制,繼承了spring-boot-starter-parent組態檔,再點開查看父類spring-boot-dependencies-->
<!--spring-boot-dependencies里包含了大量的properties,dependencyManagement,build可供選擇使用-->
<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>
<!--固定使用1.8JDK-->
<properties>
<java.version>1.8</java.version>
</properties>
<!--起步依賴,查看原始碼可以查看到關于SpringMvc的相關依賴,包括SpringMVC和Tomcat-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--起步依賴,查看原始碼可以查看到test的相關依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--打包插件,直接運行服務器-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
總而言之,SpringBoot創建時自帶的一系列起步依賴幫助我們簡化了大量SSM的繁瑣操作
我們再來詳細介紹幾個詞語:
Starter:
- SpringBoot中常見專案名稱,定義了當前專案使用的所有專案坐標,以達到減少依賴配置的目的
Parent:
- 所有SpringBoot專案要繼承的專案,定義了若干個坐標版本號(依賴管理,并非依賴),以達到減少沖突的目的
實際開發:
- 使用任意坐標時,僅書寫GAV中的G和A,不需要書寫V
- 如若發生坐標錯誤,再指定Version(小心版本沖突)
SpringBoot程式啟動
SpringBoot程式啟動方法就是開啟Application.java檔案即可
package com.itheima;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
我們給出兩個注意點:
- SpringBoot在創建專案時,采用jar的打包方式
- SpringBoot的引導類是專案的入口,運行main方法就可以啟動專案
SpringBoot切換服務器
我們最后給出一個Maven使用技巧來切換服務器
SpringBoot中默認使用Tomcat服務器并安裝了對應插件,
那么我們如果想切換服務器,只需要排除掉Tomcat插件,并添加新的插件即可
<?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>
<properties>
<java.version>1.8</java.version>
</properties>
<!--我們采用排除依賴的方法去除tomcat服務器-->
<dependencies>
<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>
<!--我們新添新的jetty服務器坐標即可-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
SpringBoot基礎配置
我們在Spring中能夠實作的技術,在SpringBoot中同樣可以實作
接下來我們依次來介紹一些SpringBoot基本配置的方法和多環境開發的問題
SpringBoot配置格式
SpringBoot為我們提供了三種配置格式來管理SpringBoot的配置(注意:以下配置均存在于resources檔案夾中):
- application.properties
# 修改服務器埠號為80
server.port=80
- application.yml (主流)
# 修改服務器埠號為81(注意:存在空格)
server:
port: 81
- application.yaml
# 修改服務器埠號為82(注意:存在空格)
server:
port: 82
當三者均存在時,其優先級為:application.properties>application.yml >application.yaml
以上三種配置格式均在resources檔案夾下創建相對應名稱以及后綴的檔案下書寫:

注意:
application.properties屬于SpringBoot自帶,不需要創建
application.yml,application.yaml需要自我創建,因而不被標記為組態檔
如果我們希望該檔案被標記為組態檔并包含有補全功能,我們需要手動設定為組態檔
yaml檔案詳細介紹
我們在這里詳細介紹一下yaml檔案:
- YAML,一種資料序列化格式
優點:
- 容易閱讀
- 容易與腳本語言互動
- 以資料為核心,重資料輕格式
YAML檔案擴展名:
- .yml(主流)
- .yaml
YAML語法規則:
-
大小寫敏感
-
屬性層級關系
-
使用縮進表示層級關系,同層級左側對齊,只允許使用空格(不能使用tab)
-
屬性值前面添加空格(屬性名與屬性值之間使用冒號+空格作為分隔)
-
# 表示注釋
-
使用 - 來表示資料開始符號(陣列)
YAML語法使用規范示例:
server:
port: 82
logging:
level:
root: info
likes:
- music
- game
- PE
YAML的資料讀取方法:
首先我們先給出我們在yml檔案中所列出的屬性:
lesson: SpringBoot
server:
port: 80
enterprise:
name: itcast
age: 16
tel: 4006184000
subject:
- Java
- 前端
- 大資料
下面我們來介紹yaml資料讀取的三種方法:
- ${屬性名},${屬性名.屬性名},${屬性名.屬性名[陣列下標]}
package com.itheima.controller;
import com.itheima.domain.Enterprise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/books")
public class BookController {
//使用@Value讀取單一屬性資料
@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物件匹配方法
package com.itheima.controller;
import com.itheima.domain.Enterprise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/books")
public class BookController {
//使用Environment封裝全配置資料(自動裝配封裝Environment,里面會包含yaml中所有屬性和屬性值)
@Autowired
private Environment environment;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
// 我們采用environment的getProperty方法,根據屬性名,獲得屬性值
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 , spring boot!";
}
}
- 自定義物件封裝指定資料
// 自定義物件Enterprise實作類(屬于Domain)
package com.itheima.domain;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Arrays;
//封裝yaml物件格式資料必須先宣告當前物體類受Spring管控
@Component
//使用@ConfigurationProperties注解定義當前物體類讀取配置屬性資訊,通過prefix屬性設定讀取哪個資料
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {
private String name;
private Integer age;
private String tel;
private String[] subject;
@Override
public String toString() {
return "Enterprise{" +
"name='" + name + '\'' +
", age=" + age +
", tel='" + tel + '\'' +
", subject=" + Arrays.toString(subject) +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer 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;
}
}
// 服務層Controller
package com.itheima.controller;
import com.itheima.domain.Enterprise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/books")
public class BookController {
// 自動裝配實作類即可
@Autowired
private Enterprise enterprise;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println(enterprise);
return "hello , spring boot!";
}
}
<!--實作自定義物件封裝時會產生警告,我們需要添加以下依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
SpringBoot多環境啟動
我們在開發程序中可能會采用不同的環境,頻繁的轉換環境當然不是一個好辦法
SpringBoot選擇配置多環境來控制環境選擇啟動
我們從兩種不同的組態檔方向來講解多環境:
- yaml多環境啟動:
# yaml采用 --- 來表示環境層級更換
# yaml采用 spring:profiles:active: 環境id 設定啟用的環境
spring:
profiles:
active: dev
---
#開發環境
#yaml采用 spring:config:activate:on-profile: 環境id 來定義當前環境id(規范寫法)
spring:
config:
activate:
on-profile: dev
#以下屬于環境配置
server:
port: 80
---
#生產
#yaml采用 spring:profiles: 環境id 來定義當前環境id(舊版寫法,同樣適用)
spring:
profiles: pro
#以下屬于環境配置
server:
port: 81
---
#測驗
#yaml采用 spring:profiles: 環境id 來定義當前環境id(舊版寫法,同樣適用)
spring:
profiles: test
#以下屬于環境配置
server:
port: 82
---
- properties多環境啟動:
# application.properties檔案(環境主檔案)
#設定啟用的環境
spring.profiles.active=pro
# application-dev.properties檔案(環境組態檔)
# 設定相關資源配置
server.port=8080
# application-pro.properties檔案(環境組態檔)
# 設定相關資源配置
server.port=8081
# application-test.properties檔案(環境組態檔)
# 設定相關資源配置
server.port=8082
SpringBoot前端多環境啟動
我們前面提及過SpringBoot的快速啟動直接將jar包打包后發給前端就可以采用命令列啟動服務器
但是我們的配置可能會導致更多的細節問題:
- 當我們的yaml出現中文注釋時,需要將IDEA的encoding均設定為UTF-8

- 當我們的前端需要不同的環境配置時,我們不能在后臺手動設定默認環境,因而需要采用指令設定
前端在呼叫時,可以采用指令來更改默認環境
默認開啟服務器
java -jar jar包名稱.jar
更換默認條件開啟服務器樣板
java -jar jar包名稱.jar --配置屬性=配置值
更換默認環境開啟服務器
java -jar jar包名稱.jar --spring.profiles.active=test
更換默認埠號開啟服務器
java -jar jar包名稱.jar --server.port=88
更換條件可以疊加使用
java -jar jar包名稱.jar --spring.profiles.active=test --server.port=88
SpringBoot多環境兼容問題
SpringBoot中存在有很多的環境設定,不僅如此,包括有Maven也存在有多環境配置
那么Maven的多環境配置優先級和SpringBoot的多環境配置優先級誰的更高呢?
- 我們的package操作是由Maven來完成的
- 多環境優先級:Maven > SpringBoot
我們通過一個簡單的案例來證明:
- Maven中配置多環境屬性
<?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_05_maven_and_boot_profile</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--開啟${}占位符作用于yaml檔案中的決議-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<encoding>UTF-8</encoding>
<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>
</project>
- SpringBoot組態檔中引入Maven屬性
# 設定啟用的環境
# 采用${}參考Maven中的屬性
spring:
profiles:
active: ${profile.active}
---
#開發
spring:
profiles: dev
server:
port: 80
---
#生產
spring:
profiles: pro
server:
port: 81
---
#測驗
spring:
profiles: test
server:
port: 82
---
- 打包并開啟服務器后,查看埠號
埠號為81
那么關于Maven的測驗就到這里結束
SpringBoot組態檔分類
我們的環境配置可以寫于許多位置,由此我們大致分為四類:
- classpath:application.yml[最低](Resources的一層配置中)

- classpath:config/application.yml(Resources的二層配置中)

- classpath:config/application.yml(package后jar包同目錄下的組態檔)

- file:config/application.yml[最高]

我們將這些位置進行分類并排出優先級:
- 1級:file:config/application.yml[最高]
- 2級:file:application.yml
- 3級:classpath:config/application.yml
- 4級:classpath:application.yml[最低]
不同位置環境配置作用:
- 1級與2級留作系統打包后設定通用屬性
- 3級與4級用于系統開發階段設定通用屬性
SpringBoot整合第三方技術
在基本介紹了SpringBoot之后,我們介紹最重要的一部分——整合第三方技術
下面我們以三個小案例來展現SpringBoot的整合
整合JUnit
SpringBoot是用于簡化Spring的工具,所以我們分別從Spring和SpringBoot的視角進行整合
Spring整合JUnit
我們先給出Spring整合JUnit的代碼:
// 設定運行器
@RunWith(SpringJUnit4ClassRunner.class)
// 加載環境
@ContextConfiguration(classes = SpringConfig.class)
public class UserServiceTesst{
// 自動裝配測驗物件
@Autowired
private BookService bookService;
// 測驗方法
@Test
public void testSave(){
bookService.save();
}
}
SpringBoot整合JUnit
我們從頭說起:
- 創建新專案(這次我們只整合JUnit,所以我們的技術選擇選擇空白)

- 我們首先查看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_07_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--我們提供了spring-boot-starter來做依賴傳遞(web時用的是spring-boot-starter-web)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--用來做測驗的相關依賴坐標匯入-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 專案自帶有一個測驗Java類
// 這里就是包,倘若為com.itheima1,classes需要設定為啟動類.class
package com.itheima;
import com.itheima.Springboot07TestApplication;
import com.itheima.service.BookService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
// 設定JUnit加載的SpringBoot啟動類(類似于@RunWith和@ContextConfiguration的整合)
@SpringBootTest
class Springboot07TestApplicationTests {
// 自動裝配測驗物件(未發生變化)
@Autowired
private BookService bookService;
// 測驗方法(未發生變化)
@Test
public void save() {
bookService.save();
}
}
/*
名稱:@SpringBootTest
型別:測驗類注解
位置:測驗類定義上方
作用:設定JUnit加載的SpringBoot啟動類
相關屬性:
classes:設定SpringBoot啟動類
注意點:
如果該測驗類在SpringBoot啟動類的包或子包中,可以省略啟動類的設定,也就是省略classes的設定
當該測驗類與啟動主Java類不屬于同一目錄名稱下時,需要設定classes屬性為啟動類
@SpringBootTest(classes = Springboot07TestApplication.class)
*/
整合MyBatis
我們如果想要采用SpringBoot整合SSM,那么就需要同時整合以下三門技術:
- Spring
- SpringMVC
- MyBatis
但SpringBoot本身就是為了簡化Spring,SpringMVC而存在,所以這兩部分整合實際上我們已經完成了
所以我們將MyBatis單列出來提前進行整合學習,為后續的SSM整合打下基礎##
Spring整合MyBatis
Spring對MyBatis的整合主要從三部分進行:
- SpringConfig
- 匯入JdbcConfig
- 匯入MyBatisConfig
- JdbcConfig
- 定義資料源(加載properties項:driver,url,username,password)
- MyBatisConfig
- 定義sqlSessionFactoryBean
- 定義映射配置
我們在這里就不做贅述了,如果遺忘可以查看之前的MyBatis文章
SpringBoot整合MyBatis
我們同樣從頭開始整合:
- 創建專案(這次我們需要MyBatis和Mysql兩門技術堆疊)

- 查看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>
</parent>
<groupId>com.itheima</groupId>
<artifactId>springboot_08_mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--自動添加MyBatis相關依賴-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!--自動添加mysql相關依賴-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--手動添加druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 創建與資料庫相同的物體類
package com.itheima.domain;
public class Book {
private Integer id;
private String name;
private String type;
private String description;
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", type='" + type + '\'' +
", description='" + description + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
- 資料層實作
package com.itheima.dao;
import com.itheima.domain.Book;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
// 注意:我們SpringBoot整合中的SpringConfig已經被省略,所以我們的JdbcConfig和MyBatisConfig配置類不用配置
// JdbcConfig主要用于配置DataSource,我們將會在yaml組態檔中配置
// MyBatisConfig配置sqlSessionFactoryBean,大部分屬于固定代碼,唯一的變數setTypeAliasesPackage我們選擇設定整個代碼包
// MyBatisConfig配置MapperScannerConfigurer映射地址,我們選擇在dao資料層采用@Mapper來代替操作
@Mapper
public interface BookDao {
@Select("select * from tbl_book where id = #{id}")
public Book getById(Integer id);
}
- 配置資料庫關聯
# 直接配置datasource即可
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
- 啟動服務器即可
package com.itheima;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Springboot08MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot08MybatisApplication.class, args);
}
}
- 簡單測驗
package com.itheima;
import com.itheima.dao.BookDao;
import com.itheima.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot08MybatisApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void testGetById() {
Book book = bookDao.getById(1);
System.out.println(book);
}
}
整合SSM
我們SpringBoot的最后課程就是用來整合SSM
我們同樣采用和之前SSM案例整合的代碼對比來介紹SpringBoot的SSM整合
Spring整合SSM
我們先給出之前SSM整合的大致框架:

我們來簡單介紹上述代碼的作用不做具體代碼展示了(如有需要可以查看之前文章SSM整合):
- Config檔案夾:各種技術的Java配置類
- SpringMvcSupport:攔截器,用來控制相關頁面展示
- controller檔案夾:服務層
- Code:狀態碼集合
- ProjectExceptionAdvice:例外處理類
- Result:回傳內容集合
- dao檔案夾:資料層
- domain檔案夾:實作類
- exception檔案夾:例外類
- service檔案夾:業務層介面以及實作類
- resources檔案夾:相關組態檔(jdbc組態檔內容)
- webapp檔案夾:前端代碼
- pom.xml:各種依靠坐標
SpringBoot整合SSM
由于我們的SSM內容過多,我們針對上次的SSM案例進行整合,部分內容不做修改,我們僅介紹更改部分
下面讓我們開始運行SpringBoot開始整合:
- 創建專案(運用了web,Mybatis,mysql技術堆疊)

- 查看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_09_ssm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_09_ssm</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--TODO 添加必要的依賴坐標-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 設定相關資料源,埠等(yaml)
# 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: root
- 對dao資料層進行簡單修改(添加@Mapper)
// 我們前面有提起Config檔案夾全部洗掉,導致我們需要手動配置dao的資料層映射
package com.itheima.dao;
import com.itheima.domain.Book;
import org.apache.ibatis.annotations.*;
import java.util.List;
// TODO 添加@Mapper
@Mapper
public interface BookDao {
@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<Book> getAll();
}
- 我們將頁面相關內容移至Sources檔案夾下的static檔案夾下

- 基本修改完畢,采用測驗類測驗
package com.itheima.service;
import com.itheima.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class BookServiceTest {
@Autowired
private BookService bookService;
@Test
public void testGetById(){
Book book = bookService.getById(2);
System.out.println(book);
}
@Test
public void testGetAll(){
List<Book> all = bookService.getAll();
System.out.println(all);
}
}
最后為大家展示一下SpringBoot整合后的整體框架:

結束語
好的,關于SpringBoot的內容就介紹到這里,希望能為你帶來幫助!
附錄
該文章屬于學習內容,具體參考B站黑馬程式員李老師的SSM框架課程
這里附上鏈接:SpringBoot-01-SpringBoot工程入門案例開發步驟_嗶哩嗶哩_bilibili
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/514311.html
標籤:架構設計
上一篇:流程引擎的架構設計
下一篇:里氏替換原則
