关于java:最适合初学者的SpringBoot笔记动力节点

6次阅读

共计 17366 个字符,预计需要花费 44 分钟才能阅读完成。

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

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

视频观看地址

https://www.bilibili.com/vide…

Spring Boot 讲义

第 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 疾速构建
北京能源节点 http://www.bjpowernode.com
(2)设置 GAV 坐标及 pom 配置信息
(3)抉择 Spring Boot 版本及依赖

会依据抉择的依赖主动增加起步依赖并进行主动配置

北京能源节点 http://www.bjpowernode.com
(4)设置模块名称、Content Root 门路及模块文件的目录

点击 Finish,如果是第一次创立,在右下角会提醒正在下载相干的依赖

北京能源节点 http://www.bjpowernode.com
(5)我的项目创立结束,如下
(6)我的项目构造
北京能源节点 http://www.bjpowernode.com

static:寄存动态资源,如图片、CSS、JavaScript 等

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

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

端口,数据库连贯配置等

2.2 入门案例

项目名称:002 – springboot-springmvc

2.2.2 创立一个新的 Module,抉择类型为 Spring Initializr
北京能源节点 http://www.bjpowernode.com
2.2.3 指定 GAV 及 pom 配置信息
2.2.4 抉择 Spring Boot 版本及依赖
会依据抉择的依赖主动增加起步依赖并进行主动配置
北京能源节点 http://www.bjpowernode.com
2.2.5 批改 Content Root 门路及文件所在目录
2.2.6 对 POM.xml 文件进行解释

http://maven.apache.org/xsd/m…d”>
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北京能源节点 http://www.bjpowernode.com
org.springframework.boot spring-boot-maven-plugin

北京能源节点 http://www.bjpowernode.com
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;

北京能源节点 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!”;
}
}

留神:新创建的类肯定要位于 Application 同级目录或者上级目录,否则 SpringBoot 加

载不到。

2.2.9 在 IDEA 中右键,运行 Application 类中的 main 办法
通过在控制台的输入,能够看到启动 SpringBoot 框架,会启动一个内嵌的 tomcat,端
口号为 8080,上下文根为空
2.2.10 在浏览器中输出 http://localhost:8080/springB… 拜访
北京能源节点 http://www.bjpowernode.com
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 我的项目根底上,进行批改

北京能源节点 http://www.bjpowernode.com

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

通过批改 application.properties 配置文件,在批改默认 tomcat 端口号及我的项目高低文件根

键值对的 properties 属性文件配置形式

\# 设置内嵌 Tomcat 端口号
server.port= 9090

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

配置结束之后,启动浏览器测试

页面显示后果
北京能源节点 http://www.bjpowernode.com
(8).yml 文件

项目名称:004 – springboot-yml,在 003 我的项目根底之上

yml 是一种 yaml 格局的配置文件,次要采纳肯定的空格、换行等格局排版进行配置。yaml 是一种直观的可能被计算机辨认的的数据序列化格局,容易被人类浏览,yaml 类

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

缀也能够应用 yaml 后缀

留神:当两种格局配置文件同时存在,应用的是 .properties 配置文件,为了演示 yml,能够

先将其改名,从新运行 Application,查看启动的端口及上下文根

咱们当前在授课的过程中,应用 properties,所以演示完 yml 成果后,将该配置文件改名

北京能源节点 http://www.bjpowernode.com
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

正文完
 0