Spring Boot是Spring家族下的一个全新开发框架,其设计目的主要是用来简化Spring应用的创建及开发过程,它提供了自动配置,starter依赖等特性,从而使开发人员从大量的XML配置中解脱出来,Spring Boot致力于在蓬勃发展的快速应用开发领域成为领导者。

动力节点的springboot本课程由浅入深,带你体验Spring Boot的极速开发过程,内容丰富,涵盖了SpringBoot开发的方方面面,并且同步更新到Spring Boot 2.x系列的最新版本,让你一次性拿下Spring Boot开发框架。

视频资源

https://www.bilibili.com/video/BV1XQ4y1m7ex

第 1 章 Spring Boot 框架入门

1.1 Spring Boot 简介

Spring Boot是Spring家族中的一个全新的框架,它用来简化Spring应用程序的创建和开发过程,也可以说Spring Boot能简化我们之前采用SpringMVC + Spring + MyBatis框架进行开发的过程。

在以往我们采用SpringMVC + Spring + MyBatis框架进行开发的时候,搭建和整合三大框架,我们需要做很多工作,比如配置web.xml,配置Spring,配置MyBatis,并将它们整合在一起等,而Spring Boot框架对此开发过程进行了革命性的颠覆,完全抛弃了繁琐的xml配置过程,采用大量的默认配置简化我们的开发过程。

所以采用Spring Boot可以非常容易和快速地创建基于Spring框架的应用程序,它让编码变简单了,配置变简单了,部署变简单了,监控变简单了。正因为 Spring Boot 它化繁为简,让开发变得极其简单和快速,所以在业界备受关注。

Spring Boot在国内的关注趋势图:http://t.cn/ROQLquP

1.2 Spring Boot 的特性

➢ 能够快速创建基于Spring的应用程序
➢ 能够直接使用java main方法启动内嵌的Tomcat服务器运行Spring Boot程序,不需要部署war包文件
➢ 提供约定的starter POM来简化Maven配置,让Maven的配置变得简单
➢ 自动化配置,根据项目的Maven依赖配置,Spring boot自动配置Spring、Spring mvc等
➢ 提供了程序的健康检查等功能
➢ 基本可以完全不使用XML配置文件,采用注解配置
北京动力节点 http://www.bjpowernode.com
1.3 Spring Boot 四大核心
1.3.1 自动配置
1.3.2 起步依赖
1.3.3 Actuator
1.3.4 命令行界面
北京动力节点 http://www.bjpowernode.com

第 2 章 Spring Boot 入门案例

2.1 第一个 SpringBoot 项目
2.1.1 开发步骤

项目名称: 001 - springboot-first

( 1 ) 创建一个 Module ,选择类型为 Spring Initializr 快速构建

在这里插入图片描述

( 2 ) 设置 GAV 坐标及 pom 配置信息

在这里插入图片描述

( 3 ) 选择 Spring Boot 版本及依赖

会根据选择的依赖自动添加起步依赖并进行自动配置 在这里插入图片描述

( 4 ) 设置模块名称、 Content Root 路径及模块文件的目录

点击 Finish ,如果是第一次创建,在右下角会提示正在下载相关的依赖 在这里插入图片描述

( 5 ) 项目创建完毕,如下

在这里插入图片描述

( 6 ) 项目结构

在这里插入图片描述

static:存放静态资源,如图片、CSS、JavaScript等

templates:存放Web页面的模板文件

application.properties/application.yml 用于存放程序的各种依赖模块的配置信息,比如 服务

端口,数据库连接配置等

2.2 入门案例

项目名称: 002 - springboot-springmvc

2.2.2 创建一个新的 Module ,选择类型为 Spring Initializr

在这里插入图片描述

2.2.3 指定 GAV 及 pom 配置信息

在这里插入图片描述

2.2.4 选择 Spring Boot 版本及依赖
会根据选择的依赖自动添加起步依赖并进行自动配置

在这里插入图片描述

2.2.5 修改 Content Root 路径及文件所在目录

在这里插入图片描述

2.2.6 对 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
<modelVersion>4.0.0</modelVersion>
<!--继承 SpringBoot 框架的一个父项目,所有自己开发的 Spring Boot 都必须的继承-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<!--当前项目的 GAV 坐标-->
<groupId>com.bjpowernode.springboot</groupId>
<artifactId>002-springboot-springmvc</artifactId>
<version>1.0.0</version>

<!--maven 项目名称,可以删除-->
<name>002-springboot-springmvc</name>
<!--maven 项目描述,可以删除-->
<description>Demo project for Spring Boot</description>

<!--maven 属性配置,可以在其它地方通过${}方式进行引用-->
<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<!--SpringBoot 框架 web 项目起步依赖, 通过该依赖自动关联其它依赖, 不需要我们一个一个去添加了
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--SpringBoot 框架的测试起步依赖,例如: junit 测试,如果不需要的话可以删除-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<!--SpringBoot 提供的打包编译等插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2.7 对 SpringBoot 项目结构进行说明

在这里插入图片描述

➢ .mvn|mvnw|mvnw.cmd:使用脚本操作执行maven相关命令,国内使用较少,可删
除
➢ .gitignore:使用版本控制工具git的时候,设置一些忽略提交的内容
➢ static|templates:后面模板技术中存放文件的目录
➢ application.properties:SpringBoot的配置文件,很多集成的配置都可以在该文件中
进行配置,例如:Spring、springMVC、Mybatis、Redis等。目前是空的
➢ Application.java:SpringBoot程序执行的入口,执行该程序中的main方法,SpringBoot
就启动了
2.2.8 创建一个 Spring MVC 的 Spring BootController

SpringBootController类所在包:com.bjpowernode.springboot.web

package com.bjpowernode.springboot.web;

import org.springframework.stereotype.Controller;

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!";
}
}
注意:新创建的类一定要位于 Application 同级目录或者下级目录,否则 SpringBoot 加载不到。
2.2.9 在 IDEA 中右键,运行 Application 类中的 main 方法
通过在控制台的输出,可以看到启动SpringBoot框架,会启动一个内嵌的tomcat,端
口号为 8080 ,上下文根为空

在这里插入图片描述

2.2.10 在浏览器中输入 http://localhost:8080/springBoot/say 访问

在这里插入图片描述

2.3 入门案例分析
➢ Spring Boot的父级依赖spring-boot-starter-parent配置之后,当前的项目就是Spring
Boot项目
➢ spring-boot-starter-parent是一个Springboot的父级依赖,开发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项目基础上,进行修改

项目名称: 003 - springboot-port-context-path

通过修改application.properties配置文件,在修改默认tomcat端口号及项目上下文件根

键值对的properties属性文件配置方式
\#设置内嵌Tomcat端口号
server.port= 9090

\#配置项目上下文根
server.servlet.context-path=/003-springboot-port-context-path

配置完毕之后,启动浏览器测试

页面显示结果

在这里插入图片描述

( 8 ) .yml 文件

项目名称: 004 - springboot-yml,在 003 项目基础之上

yml 是一种 yaml 格式的配置文件,主要采用一定的空格、换行等格式排版进行配置。
yaml 是一种直观的能够被计算机识别的的数据序列化格式,容易被人类阅读,yaml 类

似于 xml,但是语法比xml 简洁很多,值与前面的冒号配置项必须要有一个空格, yml 后

缀也可以使用 yaml 后缀 在这里插入图片描述

注意:当两种格式配置文件同时存在,使用的是 .properties 配置文件,为了演示 yml ,可以

先将其改名,重新运行 Application ,查看启动的端口及上下文根 在这里插入图片描述 在这里插入图片描述

我们以后在授课的过程中,使用 properties ,所以演示完 yml 效果后,将该配置文件改名 在这里插入图片描述

2.4.2 多环境配置
在实际开发的过程中,我们的项目会经历很多的阶段(开发->测试->上线),每个阶段

的配置也会不同,例如:端口、上下文根、数据库等,那么这个时候为了方便在不同的环境

之间切换,SpringBoot提供了多环境配置,具体步骤如下

( 9 ) 项目名称: 005 - springboot-multi-environment
为每个环境创建一个配置文件,命名必须以 application- 环境标识 .properties|yml
application-dev.properties
#开发环境
北京动力节点 http://www.bjpowernode.com

#设置内嵌Tomcat默认端口号 server.port= 8080

#设置项目的上下文根 server.servlet.context-path=/00 5 - springboot-multi-environment-dev

application-product.properties

#生产环境

#配置内嵌Tomcat默认端口号 server.port= 80

#配置项目上下文根 server.servlet.context-path=/00 5 - springboot-multi-environment-product

application-test.properties

#测试环境

#配置内嵌Tomcat端口号 server.port= 8081

#配置项目的上下文根 server.servlet.context-path=/00 5 - springboot-multi-environment-test

在总配置文件application.properties进行环境的激活

#SpringBoot的总配置文件

#激活开发环境 #spring.profiles.active=dev

#激活测试环境 #spring.profiles.active=test

#激活生产环境 spring.profiles.active=product

等号右边的值和配置文件的环境标识名一致,可以更改总配置文件的配置,重新

北京动力节点 http://www.bjpowernode.com
运行 Application ,查看启动的端口及上下文根
( 10 ) 项目名称: 006 - springboot-multi-environment

为每个环境创建一个配置文件,命名必须以 application- 环境标识 .properties|yml

SpringBoot总配置文件:application.yml

#springboot 总配置文件

# 激活开发环境 _#spring:

profiles:

active: dev_

北京动力节点 http://www.bjpowernode.com

# 激活测试环境 _#spring:

profiles:

active: test_

# 激活生产环境 spring : profiles : active : product

开发环境配置文件:application-dev.yml

# 设置开发环境配置

server : port : 8080 # 设置 Tomcat 内嵌端口号 servlet : context-path : /dev # 设置上下文根

测试环境配置文件:application-test.yml

# 设置测试环境配置

server : port : 9090 servlet : context-path : /test

生产环境配置文件:application-product.yml

# 设置生产环境配置

server : port : 80 servlet : context-path : /product

北京动力节点 http://www.bjpowernode.com
2.4.3 Spring Boot 自定义配置
在SpringBoot的核心配置文件中,除了使用内置的配置项之外,我们还可以在自定义配

置,然后采用如下注解去读取配置的属性值

( 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注解或者自定义配置值,并对
北京动力节点 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包下创建ConfigInfo类,并为该类加上Component和
ConfigurationProperties注解,并在ConfigurationProperties注解中添加属性prefix,
作用可以区分同名配置
@Component
北京动力节点 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配置文件

#设置内嵌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

北京动力节点 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会出现一个警告,
不影响程序的执行
➢ 点击open documentnation跳转到网页,在网页中提示需要加一个依赖,我们将这
个依赖拷贝,粘贴到pom.xml文件中
北京动力节点 http://www.bjpowernode.com
<!--解决使用@ConfigurationProperties注解出现警告问题-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
( 14 ) 中文乱码
如果在SpringBoot核心配置文件中有中文信息,会出现乱码:
◼ 一般在配置文件中,不建议出现中文(注释除外)
◼ 如果有,可以先转化为ASCII码
( 15 ) 友情提示
大家如果是从其它地方拷贝的配置文件,一定要将里面的空格删干净
2.5 Spring Boot 前端使用 JSP

项目名称: 009 - springboot-jsp

北京动力节点 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目录下才能访问,否则

访问不到。其实官方已经更建议使用模板技术(后面会讲模板技术)

北京动力节点 http://www.bjpowernode.com src/main/webapp META-INF/resources **/.

2.5.6 在 application.properties 文件配置 Spring MVC 的视图展示为
jsp ,这里相当于 Spring MVC 的配置

#SpringBoot核心配置文件 #指定内嵌Tomcat端口号 server.port= 8090

#配置SpringMVC视图解析器 #其中:/ 表示目录为src/main/webapp spring.mvc.view.prefix=/ spring.mvc.view.suffix=.jsp

集成完毕之后,剩下的步骤和我们使用 Spring MVC 一样

application.yml格式的配置文件

#SpringBoot 核心配置文件 # 指定内嵌 Tomcat 端口号 server : port : 8090 servlet : context-path : /

# 配置 SpringMVC 视图解析器 # 其中: / 表示目录为 src/main/webapp spring : mvc : view : prefix : / suffix : .jsp

北京动力节点 http://www.bjpowernode.com
2.5.7 在 com.abc.springboot.controller 包下创建 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 下创建一个 webapp 目录,然后在该目录下新建
index.jsp 页面
如果在webapp目录下右键,没有创建jsp的选项,可以在Project Structure中指定webapp

为Web Resource Directory

北京动力节点 http://www.bjpowernode.com
2.5.9 在 jsp 中获取 Controller 传递过来的数据
2.5.10 重新运行 Application ,通过浏览器访问测试

第 3 章 Spring Boot 框架 Web 开发

3.1 Spring Boot 集成 MyBatis

项目名称: 010 - springboot-web-mybatis

3.1.1 案例思路
通过SpringBoot +MyBatis实现对数据库学生表的查询操作
数据库参考:springboot.sql脚本文件
北京动力节点 http://www.bjpowernode.com
3.1.2 实现步骤
( 1 ) 准备数据库
➢ 启动Linux系统上的mySQL服务器,通过Navicat连接
➢ 创建新的数据库springboot,指定数据库字符编码为utf- 8
➢ 向表中插入数据
( 2 ) 创建 010 - springboot-web-mybatis 项目
➢ 创建一个新的SpringBoot的Module
北京动力节点 http://www.bjpowernode.com

➢ 指定GAV坐标

北京动力节点 http://www.bjpowernode.com

➢ 选择SpringBoot版本以及web依赖

北京动力节点 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>
北京动力节点 http://www.bjpowernode.com
<!--MySQL的驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
( 4 ) 在 Springboot 的核心配置文件 application.properties 中配
置数据源
注意根据自己数据库的信息修改以下内容
#配置内嵌Tomcat端口号
server.port= 9090
#配置项目上下文根
server.servlet.context-path=/010-springboot-web-mybatis
#配置数据库的连接信息
#注意这里的驱动类有变化
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 ) 开发代码
➢ 使用Mybatis反向工程生成接口、映射文件以及实体bean,具体步骤参见附录 1
北京动力节点 http://www.bjpowernode.com

➢ 在web包下创建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包下创建service接口并编写代码

public interface StudentService {
/**
* 根据学生标识获取学生详情
* @param id
* @return
*/
Student queryStudentById(Integer id);
}

➢ 在service.impl包下创建service接口并编写代码

@Service
public class StudentServiceImpl implements StudentService {
北京动力节点 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自动扫描数据持久层的映射文件及DAO接口的关系
@Mapper
public interface StudentMapper {

➢ 注意:默认情况下,Mybatis的xml映射文件不会编译到target的class目录下,所

以我们需要在pom.xml文件中配置resource
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
北京动力节点 http://www.bjpowernode.com
</resource>
</resources>
( 6 ) 启动 Application 应用,浏览器访问测试运行
3.1.3 DAO 其它开发方式
( 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提供的注解:扫描数据持久层的mapper映谢配置文件,DAO接口上就不用加@Mapper //basePackages通常指定到数据持久层包即可 @MapperScan(basePackages = "com.abc.springboot.mapper") public class Application {

测试运行

北京动力节点 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 开启事务支持
➢ 在访问数据库的Service方法上添加注解 @Transactional 即可
北京动力节点 http://www.bjpowernode.com
3.2.1 案例思路
通过SpringBoot +MyBatis实现对数据库学生表的更新操作,在service层的方法中构建

异常,查看事务是否生效

项目名称: 012 - springboot-web-mybatis-transacation

该项目是在 011 的基础上添加新增方法,在新增方法中进行案例的演示

3.2.2 实现步骤
( 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 接口中添加更新学生方法

/*** 根据学生标识更新学生信息 *** @param student

北京动力节点 http://www.bjpowernode.com

*** @return */ int modifyStudentById(Student student);

( 11 ) 在 StudentServiceImpl 接口实现类中对更新学生方法进
行实现,并构建一个异常,同时在该方法上加 @Transactional 注解

@Override @Transactional //添加此注解说明该方法添加的事务管理 public int update(Student student) {

int updateCount = studentMapper.updateByPrimaryKeySelective(student);

System. out .println("更新结果:" + updateCount);

//在此构造一个除数为 0 的异常,测试事务是否起作用 int a = 10 / 0 ;

return updateCount; }

( 12 ) 在 Application 类上加 @EnableTransactionManagement
开启事务支持

@EnableTransactionManagement 可选,但是业务方法上必须添加 @Transactional 事务才生效

@SpringBootApplication @MapperScan(basePackages = "com.abc.springboot.mapper") @EnableTransactionManagement //开启事务支持(可选项,但@Transactional必须添加) public class Application {

北京动力节点 http://www.bjpowernode.com
( 13 ) 启动 Application ,通过浏览器访问进行测试
浏览器
控制台
数据库表
通过以上结果,说明事务起作用了
( 14 ) 注释掉 StudentServiceImpl 上的 @Transactional 测试
数据库的数据被更新
北京动力节点 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数据

案例:
➢ 创建MyRestController类,演示@RestController替代@Controller + @ResponseBody
@RestController
public class MyRestController {
@Autowired
private StudentService studentService;
@RequestMapping("/boot/stu")
public Object stu(){
return studentService.getStudentById( 1 );
}
}
➢ 启动应用,浏览器访问测试
北京动力节点 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请求主要用户新增数据

3.3.6 @PutMapping

RequestMapping和Put请求方法的组合

只支持Put请求

Put通常用于修改数据

3.3.7 @DeleteMapping

RequestMapping 和 Delete请求方法的组合

只支持Delete请求

通常用于删除数据

3.3.8 综合案例

项目名称: 013 - springboot-springmvc项目集成springmvc

项目作用:演示常见的SpringMVC注解

北京动力节点 http://www.bjpowernode.com
( 15 ) 创建一个 MVCController ,里面使用上面介绍的各种注解
接收不同的请求

/*** 该案例主要演示了使用 Spring 提供的不同注解接收不同类型的请求 */ //RestController注解相当于加了给方法加了@ResponseBody注解,所以是不能跳转页面的, 只能返回字符串或者json数据 @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注解,通常更新数据时使用"; }

@DeleteMapping(value = "/remove") public String remove() { return "@DeleteMapping注解,通常删除数据时使用"; } }

北京动力节点 http://www.bjpowernode.com
( 16 ) 启动应用,在浏览器中输入不同的请求进行测试
( 17 ) Http 接口请求工具 Postman 介绍
因为通过浏览器输入地址,默认发送的只能是get请求,通过Postman工具,可以模拟

发送不同类型的请求,并查询结果,在安装的时候,有些机器可能会需要安装MicroSort .NET

Framework

( 18 ) 使用 Postman 对其它请求类型做个测试
北京动力节点 http://www.bjpowernode.com
3.4 Spring Boot 实现 RESTful
3.4.1 认识 RESTFul

REST (英文: Representational State Transfer ,简称 REST

一种互联网软件架构设计的风格,但它并不是标准,它只是提出了一组客户端和服务器

交互时的架构理念和设计原则,基于这种理念和原则设计的接口可以更简洁,更有层次,REST

这个词,是Roy Thomas Fielding在他 2000 年的博士论文中提出的。

任何的技术都可以实现这种理念,如果一个架构符合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 开发 RESTFul
Spring boot开发RESTFul 主要是几个注解实现
( 1 ) @PathVariable
获取url中的数据
该注解是实现 RESTFul 最主要的一个注解
( 2 ) @PostMapping
接收和处理Post方式的请求
( 3 ) @DeleteMapping
接收delete方式的请求,可以使用GetMapping代替
北京动力节点 http://www.bjpowernode.com
( 4 ) @PutMapping
接收put方式的请求,可以用PostMapping代替
( 5 ) @GetMapping
接收get方式的请求
3.4.3 案例:使用 RESTful 风格模拟实现对学生的增删改查操作

项目名称: 014 - springboot-restful

该项目集成了MyBatis、spring、SpringMVC,通过模拟实现对学生的增删改查操作

( 6 ) pom.xml 文件添加内容如下

org.springframework.boot spring-boot-starter-web

org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.1

mysql mysql-connector-java