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

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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理