Spring Boot是Spring家族下的一個全新開發(fā)框架灾测,其設計目的主要是用來簡化Spring應用的創(chuàng)建及開發(fā)過程爆价,它提供了自動配置,starter依賴等特性媳搪,從而使開發(fā)人員從大量的XML配置中解脫出來铭段,Spring Boot致力于在蓬勃發(fā)展的快速應用開發(fā)領域成為領導者。
動力節(jié)點的springboot本課程由淺入深秦爆,帶你體驗Spring Boot的極速開發(fā)過程序愚,內(nèi)容豐富,涵蓋了SpringBoot開發(fā)的方方面面等限,并且同步更新到Spring Boot 2.x系列的最新版本爸吮,讓你一次性拿下Spring Boot開發(fā)框架。
視頻觀看地址
https://www.bilibili.com/video/BV1XQ4y1m7ex
Spring Boot 講義
第 1 章 Spring Boot 框架入門
1.1 Spring Boot 簡介
Spring Boot是Spring家族中的一個全新的框架精刷,它用來簡化Spring應用程序的創(chuàng)建和
開發(fā)過程拗胜,也可以說Spring Boot能簡化我們之前采用SpringMVC + Spring + MyBatis框架進行
開發(fā)的過程。
在以往我們采用SpringMVC + Spring + MyBatis框架進行開發(fā)的時候怒允,搭建和整合三大框
架埂软,我們需要做很多工作,比如配置web.xml,配置Spring勘畔,配置MyBatis所灸,并將它們整合在
一起等,而Spring Boot框架對此開發(fā)過程進行了革命性的顛覆炫七,完全拋棄了繁瑣的xml配
置過程爬立,采用大量的默認配置簡化我們的開發(fā)過程。
所以采用Spring Boot可以非常容易和快速地創(chuàng)建基于Spring框架的應用程序万哪,它讓編
碼變簡單了侠驯,配置變簡單了,部署變簡單了奕巍,監(jiān)控變簡單了吟策。正因為 Spring Boot 它化繁為
簡,讓開發(fā)變得極其簡單和快速的止,所以在業(yè)界備受關注檩坚。
Spring Boot在國內(nèi)的關注趨勢圖:http://t.cn/ROQLquP
1.2 Spring Boot 的特性
? 能夠快速創(chuàng)建基于Spring的應用程序
? 能夠直接使用java main方法啟動內(nèi)嵌的Tomcat服務器運行Spring Boot程序,不需
要部署war包文件
? 提供約定的starter POM來簡化Maven配置诅福,讓Maven的配置變得簡單
? 自動化配置匾委,根據(jù)項目的Maven依賴配置,Spring boot自動配置Spring氓润、Spring mvc
等
? 提供了程序的健康檢查等功能
? 基本可以完全不使用XML配置文件赂乐,采用注解配置
北京動力節(jié)點 http://www.bjpowernode.com
1.3 Spring Boot 四大核心
1.3.1 自動配置
1.3.2 起步依賴
1.3.3 Actuator
1.3.4 命令行界面
北京動力節(jié)點 http://www.bjpowernode.com
第 2 章 Spring Boot 入門案例
2.1 第一個 SpringBoot 項目
2.1.1 開發(fā)步驟
項目名稱: 001 - springboot-first
( 1 ) 創(chuàng)建一個 Module ,選擇類型為 Spring Initializr 快速構建
北京動力節(jié)點 http://www.bjpowernode.com
( 2 ) 設置 GAV 坐標及 pom 配置信息
( 3 ) 選擇 Spring Boot 版本及依賴
會根據(jù)選擇的依賴自動添加起步依賴并進行自動配置
北京動力節(jié)點 http://www.bjpowernode.com
( 4 ) 設置模塊名稱旺芽、 Content Root 路徑及模塊文件的目錄
點擊 Finish 沪猴,如果是第一次創(chuàng)建辐啄,在右下角會提示正在下載相關的依賴
北京動力節(jié)點 http://www.bjpowernode.com
( 5 ) 項目創(chuàng)建完畢采章,如下
( 6 ) 項目結(jié)構
北京動力節(jié)點 http://www.bjpowernode.com
static:存放靜態(tài)資源,如圖片壶辜、CSS悯舟、JavaScript等
templates:存放Web頁面的模板文件
application.properties/application.yml 用于存放程序的各種依賴模塊的配置信息,比如 服務
端口砸民,數(shù)據(jù)庫連接配置等
2.2 入門案例
項目名稱: 002 - springboot-springmvc
2.2.2 創(chuàng)建一個新的 Module 抵怎,選擇類型為 Spring Initializr
北京動力節(jié)點 http://www.bjpowernode.com
2.2.3 指定 GAV 及 pom 配置信息
2.2.4 選擇 Spring Boot 版本及依賴
會根據(jù)選擇的依賴自動添加起步依賴并進行自動配置
北京動力節(jié)點 http://www.bjpowernode.com
2.2.5 修改 Content Root 路徑及文件所在目錄
2.2.6 對 POM.xml 文件進行解釋
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0org.springframework.boot spring-boot-starter-parent 2.2.1.RELEASE com.bjpowernode.springboot 002 - springboot-springmvc 1.0.0 002 - springboot-springmvc Demo project for Spring Boot 1.8org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine北京動力節(jié)點 http://www.bjpowernode.com
org.springframework.boot spring-boot-maven-plugin
北京動力節(jié)點 http://www.bjpowernode.com
2.2.7 對 SpringBoot 項目結(jié)構進行說明
? .mvn|mvnw|mvnw.cmd:使用腳本操作執(zhí)行maven相關命令,國內(nèi)使用較少岭参,可刪
除
? .gitignore:使用版本控制工具git的時候反惕,設置一些忽略提交的內(nèi)容
? static|templates:后面模板技術中存放文件的目錄
? application.properties:SpringBoot的配置文件,很多集成的配置都可以在該文件中
進行配置演侯,例如:Spring姿染、springMVC、Mybatis秒际、Redis等悬赏。目前是空的
? Application.java:SpringBoot程序執(zhí)行的入口狡汉,執(zhí)行該程序中的main方法,SpringBoot
就啟動了
2.2.8 創(chuàng)建一個 Spring MVC 的 Spring BootController
SpringBootController類所在包:com.bjpowernode.springboot.web
package com.bjpowernode.springboot.web;
import org.springframework.stereotype.Controller;
北京動力節(jié)點 http://www.bjpowernode.com
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
_/**
- ClassName:SpringBootController
- Package:com.bjpowernode.springboot.web
- Description:
*/_
@Controller
public class SpringBootController {
@RequestMapping(value = "/springBoot/say")
public @ResponseBody String say() {
return "Hello,springBoot!";
}
}
注意:新創(chuàng)建的類一定要位于 Application 同級目錄或者下級目錄闽颇,否則 SpringBoot 加
載不到盾戴。
2.2.9 在 IDEA 中右鍵,運行 Application 類中的 main 方法
通過在控制臺的輸出兵多,可以看到啟動SpringBoot框架尖啡,會啟動一個內(nèi)嵌的tomcat,端
口號為 8080 剩膘,上下文根為空
2.2.10 在瀏覽器中輸入 http://localhost:8080/springBoot/say 訪問
北京動力節(jié)點 http://www.bjpowernode.com
2.3 入門案例分析
? Spring Boot的父級依賴spring-boot-starter-parent配置之后可婶,當前的項目就是Spring
Boot項目
? spring-boot-starter-parent是一個Springboot的父級依賴,開發(fā)SpringBoot程序都需
要繼承該父級項目援雇,它用來提供相關的Maven默認依賴矛渴,使用它之后,常用的jar
包依賴可以省去version配置
? Spring Boot提供了哪些默認jar包的依賴惫搏,可查看該父級依賴的pom文件
? 如果不想使用某個默認的依賴版本具温,可以通過pom.xml 文件的屬性配置覆蓋各個
依賴項,比如覆蓋Spring版本
<spring-framework.version>5.0.0.RELEASE</ spring-framework.version >
</properties>
? @SpringBootApplication 注解是 Spring Boot 項目的核心注解筐赔,主要作用是開啟
Spring 自動配置铣猩,如果在 Application 類上去掉該注解,那么不會啟動 SpringBoot
程序
? main方法是一個標準的Java程序的main方法茴丰,主要作用是作為項目啟動運行的入
口
? @Controller 及 @ResponseBody 依然是我們之前的Spring MVC达皿,因為Spring Boot
的里面依然是使用我們的Spring MVC + Spring + MyBatis 等框架
2.4 Spring Boot 的核心配置文件
Spring Boot的核心配置文件用于配置Spring Boot程序,名字必須以application開始
2.4.1 核心配置格式
( 7 ) .properties 文件(默認采用該文件)
在 002 - springboot-springmvc項目基礎上贿肩,進行修改
北京動力節(jié)點 http://www.bjpowernode.com
項目名稱: 003 - springboot-port-context-path
通過修改application.properties配置文件峦椰,在修改默認tomcat端口號及項目上下文件根
鍵值對的properties屬性文件配置方式
#設置內(nèi)嵌Tomcat端口號
server.port= 9090
#配置項目上下文根
server.servlet.context-path=/003-springboot-port-context-path
配置完畢之后,啟動瀏覽器測試
頁面顯示結(jié)果
北京動力節(jié)點 http://www.bjpowernode.com
( 8 ) .yml 文件
項目名稱: 004 - springboot-yml汰规,在 003 項目基礎之上
yml 是一種 yaml 格式的配置文件汤功,主要采用一定的空格、換行等格式排版進行配置溜哮。
yaml 是一種直觀的能夠被計算機識別的的數(shù)據(jù)序列化格式滔金,容易被人類閱讀,yaml 類
似于 xml茂嗓,但是語法比xml 簡潔很多餐茵,值與前面的冒號配置項必須要有一個空格, yml 后
綴也可以使用 yaml 后綴
注意:當兩種格式配置文件同時存在述吸,使用的是 .properties 配置文件忿族,為了演示 yml ,可以
先將其改名,重新運行 Application 肠阱,查看啟動的端口及上下文根
我們以后在授課的過程中票唆,使用 properties ,所以演示完 yml 效果后屹徘,將該配置文件改名
北京動力節(jié)點 http://www.bjpowernode.com
2.4.2 多環(huán)境配置
在實際開發(fā)的過程中走趋,我們的項目會經(jīng)歷很多的階段(開發(fā)->測試->上線),每個階段
的配置也會不同噪伊,例如:端口簿煌、上下文根、數(shù)據(jù)庫等鉴吹,那么這個時候為了方便在不同的環(huán)境
之間切換姨伟,SpringBoot提供了多環(huán)境配置,具體步驟如下
( 9 ) 項目名稱: 005 - springboot-multi-environment
為每個環(huán)境創(chuàng)建一個配置文件豆励,命名必須以 application- 環(huán)境標識 .properties|yml
application-dev.properties
#開發(fā)環(huán)境
北京動力節(jié)點 http://www.bjpowernode.com
#設置內(nèi)嵌Tomcat默認端口號
server.port= 8080
#設置項目的上下文根
server.servlet.context-path=/00 5 - springboot-multi-environment-dev
application-product.properties
#生產(chǎn)環(huán)境
#配置內(nèi)嵌Tomcat默認端口號
server.port= 80
#配置項目上下文根
server.servlet.context-path=/00 5 - springboot-multi-environment-product
application-test.properties
#測試環(huán)境
#配置內(nèi)嵌Tomcat端口號
server.port= 8081
#配置項目的上下文根
server.servlet.context-path=/00 5 - springboot-multi-environment-test
在總配置文件application.properties進行環(huán)境的激活
#SpringBoot的總配置文件
#激活開發(fā)環(huán)境
#spring.profiles.active=dev
#激活測試環(huán)境
#spring.profiles.active=test
#激活生產(chǎn)環(huán)境
spring.profiles.active=product
等號右邊的值和配置文件的環(huán)境標識名一致夺荒,可以更改總配置文件的配置,重新
北京動力節(jié)點 http://www.bjpowernode.com
運行 Application 良蒸,查看啟動的端口及上下文根
( 10 ) 項目名稱: 006 - springboot-multi-environment
為每個環(huán)境創(chuàng)建一個配置文件技扼,命名必須以 application- 環(huán)境標識 .properties|yml
SpringBoot總配置文件:application.yml
#springboot 總配置文件
# 激活開發(fā)環(huán)境
_#spring:
profiles:
active: dev_
北京動力節(jié)點 http://www.bjpowernode.com
# 激活測試環(huán)境
_#spring:
profiles:
active: test_
# 激活生產(chǎn)環(huán)境
spring :
profiles :
active : product
開發(fā)環(huán)境配置文件:application-dev.yml
# 設置開發(fā)環(huán)境配置
server :
port : 8080 # 設置 Tomcat 內(nèi)嵌端口號
servlet :
context-path : /dev # 設置上下文根
測試環(huán)境配置文件:application-test.yml
# 設置測試環(huán)境配置
server :
port : 9090
servlet :
context-path : /test
生產(chǎn)環(huán)境配置文件:application-product.yml
# 設置生產(chǎn)環(huán)境配置
server :
port : 80
servlet :
context-path : /product
北京動力節(jié)點 http://www.bjpowernode.com
2.4.3 Spring Boot 自定義配置
在SpringBoot的核心配置文件中,除了使用內(nèi)置的配置項之外嫩痰,我們還可以在自定義配
置剿吻,然后采用如下注解去讀取配置的屬性值
( 11 ) @Value 注解
A 、 項目名稱: 007 - springboot-custom-configuration
用于逐個讀取application.properties中的配置
案例演示
? 在核心配置文件applicatin.properties中串纺,添加兩個自定義配置項 school.name和
website丽旅。在IDEA中可以看到這兩個屬性不能被SpringBoot識別,背景是桔色的
application.yml格式配置文件
# 設置端口號及上下文根
server :
port : 9090
servlet :
context-path : /
school :
name : ssm
websit : http://www.baidu.com
? 在SpringBootController中定義屬性纺棺,并使用@Value注解或者自定義配置值榄笙,并對
北京動力節(jié)點 http://www.bjpowernode.com
其方法進行測試
@Controller
public class SpringBootController {
@Value("${school.name}")
private String schoolName;
@Value("${websit}")
private String websit;
@RequestMapping(value = "/springBoot/say")
public @ResponseBody String say() {
return schoolName + "------" + websit;
}
}
? 重新運行Application,在瀏覽器中進行測試
( 12 ) @ConfigurationProperties
項目名稱: 008 - springboot-custom-configuration
將整個文件映射成一個對象五辽,用于自定義配置項比較多的情況
案例演示
? 在com.abc.springboot.config包下創(chuàng)建ConfigInfo類办斑,并為該類加上Component和
ConfigurationProperties注解外恕,并在ConfigurationProperties注解中添加屬性prefix杆逗,
作用可以區(qū)分同名配置
@Component
北京動力節(jié)點 http://www.bjpowernode.com
@ConfigurationProperties(prefix = "school")
public class ConfigInfo {
private String name;
private String websit;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsit() {
return websit;
}
public void setWebsit(String websit) {
this.websit = websit;
}
}
application.properties配置文件
#設置內(nèi)嵌Tomcat端口號
server.port= 9090
#設置上下文根
server.servlet.context-path=/config
school.name=ssm
school.websit=http://www.baidu.com
application.yml配置文件
server :
port : 9090
servlet :
context-path : /config
北京動力節(jié)點 http://www.bjpowernode.com
school :
name : ABC
websit : http://www.baidu.com
? 在SpringBootController中注入ConfigInfo配置類
@Autowired
private ConfigInfo configInfo;
? 修改SpringBootController類中的測試方法
@RequestMapping(value = "/springBoot/config")
public @ResponseBody String say() {
return configInfo.getName() + "=======" + configInfo.getWebsit();
}
? 重新運行Application,在瀏覽器中進行測試
( 13 ) 警告解決
? 在ConfigInfo類中使用了ConfigurationProperties注解后鳞疲,IDEA會出現(xiàn)一個警告,
不影響程序的執(zhí)行
? 點擊open documentnation跳轉(zhuǎn)到網(wǎng)頁尚洽,在網(wǎng)頁中提示需要加一個依賴挣柬,我們將這
個依賴拷貝,粘貼到pom.xml文件中
北京動力節(jié)點 http://www.bjpowernode.com
<!--解決使用@ConfigurationProperties注解出現(xiàn)警告問題-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
( 14 ) 中文亂碼
如果在SpringBoot核心配置文件中有中文信息,會出現(xiàn)亂碼:
? 一般在配置文件中,不建議出現(xiàn)中文(注釋除外)
? 如果有育叁,可以先轉(zhuǎn)化為ASCII碼
( 15 ) 友情提示
大家如果是從其它地方拷貝的配置文件框舔,一定要將里面的空格刪干凈
2.5 Spring Boot 前端使用 JSP
項目名稱: 009 - springboot-jsp
北京動力節(jié)點 http://www.bjpowernode.com
2.5.4 在 pom.xml 文件中配置以下依賴項
org.apache.tomcat.embed tomcat-embed-jasper
javax.servlet javax.servlet-api
javax.servlet.jsp javax.servlet.jsp-api 2.3.1
javax.servlet jstl
2.5.5 在 pom.xml 的 build 標簽中要配置以下信息
SpringBoot要求jsp文件必須編譯到指定的META-INF/resources目錄下才能訪問,否則
訪問不到。其實官方已經(jīng)更建議使用模板技術(后面會講模板技術)
北京動力節(jié)點 http://www.bjpowernode.com
src/main/webapp META-INF/resources /.
2.5.6 在 application.properties 文件配置 Spring MVC 的視圖展示為
jsp 聘鳞,這里相當于 Spring MVC 的配置
#SpringBoot核心配置文件
#指定內(nèi)嵌Tomcat端口號
server.port= 8090
#配置SpringMVC視圖解析器
#其中:/ 表示目錄為src/main/webapp
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
集成完畢之后薄辅,剩下的步驟和我們使用 Spring MVC 一樣
application.yml格式的配置文件
#SpringBoot 核心配置文件
# 指定內(nèi)嵌 Tomcat 端口號
server :
port : 8090
servlet :
context-path : /
# 配置 SpringMVC 視圖解析器
# 其中: / 表示目錄為 src/main/webapp
spring :
mvc :
view :
prefix : /
suffix : .jsp
北京動力節(jié)點 http://www.bjpowernode.com
2.5.7 在 com.abc.springboot.controller 包下創(chuàng)建 JspController 類,并
編寫代碼
@Controller
public class SpringBootController {
@RequestMapping(value = "/springBoot/jsp")
public String jsp(Model model) {
model.addAttribute("data","SpringBoot 前端使用JSP頁面抠璃!");
return "index";
}
}
2.5.8 在 src/main 下創(chuàng)建一個 webapp 目錄站楚,然后在該目錄下新建
index.jsp 頁面
如果在webapp目錄下右鍵,沒有創(chuàng)建jsp的選項搏嗡,可以在Project Structure中指定webapp
為Web Resource Directory
北京動力節(jié)點 http://www.bjpowernode.com
2.5.9 在 jsp 中獲取 Controller 傳遞過來的數(shù)據(jù)
2.5.10 重新運行 Application 窿春,通過瀏覽器訪問測試
第 3 章 Spring Boot 框架 Web 開發(fā)
3.1 Spring Boot 集成 MyBatis
項目名稱: 010 - springboot-web-mybatis
3.1.1 案例思路
通過SpringBoot +MyBatis實現(xiàn)對數(shù)據(jù)庫學生表的查詢操作
數(shù)據(jù)庫參考:springboot.sql腳本文件
北京動力節(jié)點 http://www.bjpowernode.com
3.1.2 實現(xiàn)步驟
( 1 ) 準備數(shù)據(jù)庫
? 啟動Linux系統(tǒng)上的mySQL服務器,通過Navicat連接
? 創(chuàng)建新的數(shù)據(jù)庫springboot采盒,指定數(shù)據(jù)庫字符編碼為utf- 8
? 向表中插入數(shù)據(jù)
( 2 ) 創(chuàng)建 010 - springboot-web-mybatis 項目
? 創(chuàng)建一個新的SpringBoot的Module
北京動力節(jié)點 http://www.bjpowernode.com
? 指定GAV坐標
北京動力節(jié)點 http://www.bjpowernode.com
? 選擇SpringBoot版本以及web依賴
北京動力節(jié)點 http://www.bjpowernode.com
? 修改Content root以及Mudule file location
( 3 ) 在 pom.xml 中添加相關 jar 依賴
<!--MyBatis整合SpringBoot的起步依賴-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
北京動力節(jié)點 http://www.bjpowernode.com
<!--MySQL的驅(qū)動依賴-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
( 4 ) 在 Springboot 的核心配置文件 application.properties 中配
置數(shù)據(jù)源
注意根據(jù)自己數(shù)據(jù)庫的信息修改以下內(nèi)容
#配置內(nèi)嵌Tomcat端口號
server.port= 9090
#配置項目上下文根
server.servlet.context-path=/010-springboot-web-mybatis
#配置數(shù)據(jù)庫的連接信息
#注意這里的驅(qū)動類有變化
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=t
rue&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyD
atetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password= 123456
( 5 ) 開發(fā)代碼
? 使用Mybatis反向工程生成接口旧乞、映射文件以及實體bean,具體步驟參見附錄 1
北京動力節(jié)點 http://www.bjpowernode.com
? 在web包下創(chuàng)建StudentController并編寫代碼
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping(value = "/springBoot/student")
public @ResponseBody Object student() {
Student student = studentService.queryStudentById( 1 );
return student;
}
}
? 在service包下創(chuàng)建service接口并編寫代碼
public interface StudentService {
/**
* 根據(jù)學生標識獲取學生詳情
* @param id
* @return
*/
Student queryStudentById(Integer id);
}
? 在service.impl包下創(chuàng)建service接口并編寫代碼
@Service
public class StudentServiceImpl implements StudentService {
北京動力節(jié)點 http://www.bjpowernode.com
@Autowired
private StudentMapper studentMapper;
@Override
public Student queryStudentById(Integer id) {
return studentMapper.selectByPrimaryKey(id);
}
}
? 如果在web中導入service存在報錯纽甘,可以嘗試進行如下配置解決
? 在Mybatis反向工程生成的StudentMapper接口上加一個Mapper注解
@Mapper作用:mybatis自動掃描數(shù)據(jù)持久層的映射文件及DAO接口的關系
@Mapper
public interface StudentMapper {
? 注意:默認情況下良蛮,Mybatis的xml映射文件不會編譯到target的class目錄下,所
以我們需要在pom.xml文件中配置resource
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
北京動力節(jié)點 http://www.bjpowernode.com
</resource>
</resources>
( 6 ) 啟動 Application 應用悍赢,瀏覽器訪問測試運行
3.1.3 DAO 其它開發(fā)方式
( 7 ) 在 運 行 的 主 類 上 添 加 注 解 包 掃 描
@MapperScan("com.abc.springboot.mapper")
注釋掉StudentMapper接口上的@Mapper注解
在運行主類Application上加@MapperScan("com.abc.springboot.mapper")
@SpringBootApplication
@MapperScan("com.abc.springboot.mapper")
public class Application {
或
@SpringBootApplication
//Mybatis提供的注解:掃描數(shù)據(jù)持久層的mapper映謝配置文件,DAO接口上就不用加@Mapper
//basePackages通常指定到數(shù)據(jù)持久層包即可
@MapperScan(basePackages = "com.abc.springboot.mapper")
public class Application {
測試運行
北京動力節(jié)點 http://www.bjpowernode.com
( 8 ) 將接口和映射文件分開
A 决瞳、 項目名稱: 011 - springboot-web-mybatis
因為SpringBoot不能自動編譯接口映射的xml文件,還需要手動在pom文件中指定左权,
所以有的公司直接將映射文件直接放到resources目錄下
? 在resources目錄下新建目錄mapper存放映射文件皮胡,將StudentMapper.xml文件移
到resources/mapper目錄下
? 在application.properties配置文件中指定映射文件的位置,這個配置只有接口和映
射文件不在同一個包的情況下赏迟,才需要指定
# 指定Mybatis映射文件的路徑
mybatis.mapper-locations=classpath:mapper/*.xml
3.2 Spring Boot 事務支持
Spring Boot 使用事務非常簡單屡贺,底層依然采用的是Spring本身提供的事務管理
? 在入口類中使用注解 @EnableTransactionManagement 開啟事務支持
? 在訪問數(shù)據(jù)庫的Service方法上添加注解 @Transactional 即可
北京動力節(jié)點 http://www.bjpowernode.com
3.2.1 案例思路
通過SpringBoot +MyBatis實現(xiàn)對數(shù)據(jù)庫學生表的更新操作,在service層的方法中構建
異常锌杀,查看事務是否生效
項目名稱: 012 - springboot-web-mybatis-transacation
該項目是在 011 的基礎上添加新增方法甩栈,在新增方法中進行案例的演示
3.2.2 實現(xiàn)步驟
( 9 ) 在 StudentController 中添加更新學生的方法
@RequestMapping(value = "/springboot/modify")
public @ResponseBody Object modifyStudent() {
int count = 0 ;
try {
Student student = new Student();
student.setId( 1 );
student.setName("Jack");
student.setAge( 33 );
count = studentService.modifyStudentById(student);
} catch (Exception e) {
e.printStackTrace();
return "fail";
}
return count;
}
( 10 ) 在 StudentService 接口中添加更新學生方法
/*** 根據(jù)學生標識更新學生信息
*** @param student
北京動力節(jié)點 http://www.bjpowernode.com
*** @return
*/
int modifyStudentById(Student student);
( 11 ) 在 StudentServiceImpl 接口實現(xiàn)類中對更新學生方法進
行實現(xiàn),并構建一個異常糕再,同時在該方法上加 @Transactional 注解
@Override
@Transactional //添加此注解說明該方法添加的事務管理
public int update(Student student) {
int updateCount = studentMapper.updateByPrimaryKeySelective(student);
System. out .println("更新結(jié)果:" + updateCount);
//在此構造一個除數(shù)為 0 的異常量没,測試事務是否起作用
int a = 10 / 0 ;
return updateCount;
}
( 12 ) 在 Application 類上加 @EnableTransactionManagement
開啟事務支持
@EnableTransactionManagement 可選,但是業(yè)務方法上必須添加 @Transactional 事務才生效
@SpringBootApplication
@MapperScan(basePackages = "com.abc.springboot.mapper")
@EnableTransactionManagement //開啟事務支持(可選項突想,但@Transactional必須添加)
public class Application {
北京動力節(jié)點 http://www.bjpowernode.com
( 13 ) 啟動 Application 殴蹄,通過瀏覽器訪問進行測試
瀏覽器
控制臺
數(shù)據(jù)庫表
通過以上結(jié)果,說明事務起作用了
( 14 ) 注釋掉 StudentServiceImpl 上的 @Transactional 測試
數(shù)據(jù)庫的數(shù)據(jù)被更新
北京動力節(jié)點 http://www.bjpowernode.com
3.3 Spring Boot 下的 Spring MVC
Spring Boot下的Spring MVC和之前的Spring MVC使用是完全一樣的猾担,主要有以下注解
3.3.1 @Controller
Spring MVC的注解袭灯,處理http請求
3.3.2 @RestController
Spring 4 后新增注解,是@Controller注解功能的增強
是 @Controller與@ResponseBody的組合注解
如果一個Controller類添加了@RestController绑嘹,那么該Controller類下的所有方法都相當
于添加了@ResponseBody注解
用于返回字符串或json數(shù)據(jù)
案例:
? 創(chuàng)建MyRestController類稽荧,演示@RestController替代@Controller + @ResponseBody
@RestController
public class MyRestController {
@Autowired
private StudentService studentService;
@RequestMapping("/boot/stu")
public Object stu(){
return studentService.getStudentById( 1 );
}
}
? 啟動應用,瀏覽器訪問測試
北京動力節(jié)點 http://www.bjpowernode.com
3.3.3 @RequestMapping (常用)
支持Get請求工腋,也支持Post請求
3.3.4 @GetMapping
RequestMapping和Get請求方法的組合
只支持Get請求
Get請求主要用于查詢操作
3.3.5 @PostMapping
RequestMapping和Post請求方法的組合
只支持Post請求
Post請求主要用戶新增數(shù)據(jù)
3.3.6 @PutMapping
RequestMapping和Put請求方法的組合
只支持Put請求
Put通常用于修改數(shù)據(jù)
3.3.7 @DeleteMapping
RequestMapping 和 Delete請求方法的組合
只支持Delete請求
通常用于刪除數(shù)據(jù)
3.3.8 綜合案例
項目名稱: 013 - springboot-springmvc項目集成springmvc
項目作用:演示常見的SpringMVC注解
北京動力節(jié)點 http://www.bjpowernode.com
( 15 ) 創(chuàng)建一個 MVCController 蛤克,里面使用上面介紹的各種注解
接收不同的請求
/*** 該案例主要演示了使用 Spring 提供的不同注解接收不同類型的請求
*/
//RestController注解相當于加了給方法加了@ResponseBody注解,所以是不能跳轉(zhuǎn)頁面的夷蚊,
只能返回字符串或者json數(shù)據(jù)
@RestController
public class MVCController {
@GetMapping(value = "/query")
public String get() {
return "@GetMapping注解,通常查詢時使用";
}
@PostMapping(value = "/add")
public String add() {
return "@PostMapping注解构挤,通常新增時使用";
}
@PutMapping(value = "/modify")
public String modify() {
return "@PutMapping注解,通常更新數(shù)據(jù)時使用";
}
@DeleteMapping(value = "/remove")
public String remove() {
return "@DeleteMapping注解惕鼓,通常刪除數(shù)據(jù)時使用";
}
}
北京動力節(jié)點 http://www.bjpowernode.com
( 16 ) 啟動應用筋现,在瀏覽器中輸入不同的請求進行測試
( 17 ) Http 接口請求工具 Postman 介紹
因為通過瀏覽器輸入地址傍菇,默認發(fā)送的只能是get請求坯汤,通過Postman工具威兜,可以模擬
發(fā)送不同類型的請求横缔,并查詢結(jié)果督暂,在安裝的時候壁晒,有些機器可能會需要安裝MicroSort .NET
Framework
( 18 ) 使用 Postman 對其它請求類型做個測試
北京動力節(jié)點 http://www.bjpowernode.com
3.4 Spring Boot 實現(xiàn) RESTful
3.4.1 認識 RESTFul
REST (英文: Representational State Transfer 赁温,簡稱 REST )
一種互聯(lián)網(wǎng)軟件架構設計的風格挑势,但它并不是標準价淌,它只是提出了一組客戶端和服務器
交互時的架構理念和設計原則申眼,基于這種理念和原則設計的接口可以更簡潔瞒津,更有層次,REST
這個詞括尸,是Roy Thomas Fielding在他 2000 年的博士論文中提出的巷蚪。
任何的技術都可以實現(xiàn)這種理念,如果一個架構符合REST原則濒翻,就稱它為RESTFul架
構
比如我們要訪問一個http接口:http://localhost:8080/boot/order?id=1021&status=1
采用RESTFul風格則http地址為:http://localhost:8080/boot/order/1021/1
3.4.2 Spring Boot 開發(fā) RESTFul
Spring boot開發(fā)RESTFul 主要是幾個注解實現(xiàn)
( 1 ) @PathVariable
獲取url中的數(shù)據(jù)
該注解是實現(xiàn) RESTFul 最主要的一個注解
( 2 ) @PostMapping
接收和處理Post方式的請求
( 3 ) @DeleteMapping
接收delete方式的請求屁柏,可以使用GetMapping代替
北京動力節(jié)點 http://www.bjpowernode.com
( 4 ) @PutMapping
接收put方式的請求,可以用PostMapping代替
( 5 ) @GetMapping
接收get方式的請求
3.4.3 案例:使用 RESTful 風格模擬實現(xiàn)對學生的增刪改查操作
項目名稱: 014 - springboot-restful
該項目集成了MyBatis有送、spring淌喻、SpringMVC,通過模擬實現(xiàn)對學生的增刪改查操作
( 6 ) pom.xml 文件添加內(nèi)容如下
org.springframework.boot spring-boot-starter-web
org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.1
mysql mysql-connector-java