目录
1SpringBoot框架学习
1.1 什么是SpringBoot
定位: SpringBoot框架是框架的框架,简化常规框架的配置的方式,只需要很少的代码,即可以实现大量的功能. 体现了**“开箱即用”** 思想
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
1.2 创建SpringBoot项目
1.2.1 创建项目
1.3 pom.xml文件说明
1.3.1 pom.xml基本配置
<!--maven坐标 必须唯一-->
<groupId>com.jt</groupId>
<artifactId>springboot_demo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_demo1</name>
<description>Demo project for Spring Boot</description>
<!--
配置信息: JDK版本/设定了字符集/springboot版本信息
-->
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
1.3.2 jar包依赖关系
<!-- 指定依赖项 -->
<dependencies>
<!--
现象:
springboot中的jar包文件缺少版本号
原因:
原来开发框架时需要自己手动添加版本号.SpringBoot框架
引入之后,所有的版本号信息,由SpringBoot官网进行测试(springboot将市面上
常用的框架进行了整合,并且测试了jar包的版本及依赖关系).springBoot内部已经完成
了版本定义.所以无需再写.
-->
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<!--springboot项目依赖管理器,其中定义了其它框架的jar包,
以后使用直接添加jar包即可
-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
1.3.3 springboot项目 maven 操作方式
<!--
标签说明:
maven操作springboot项目的工具
1.编译工具 maven-compiler-plugin
作用: maven执行指令时,通过插件管理SpringBoot项目
2.项目打包工具 spring-boot-maven-plugin
采用install的方式将springboot项目打成jar包.
springboot的项目和常规项目不一样.所以需要添加插件
-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.jt.SpringbootDemo1Application</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
1.4 开箱即用原理说明(面试必问)
1.4.1 介绍
springBoot将繁琐的配置封装到某些jar包中. 该jar包中的文件已经完成了配置.引入即可使用.只需要少量的配置就可以获取其功能的方式 叫做 “开箱即用”
1.4.2 开箱即用规则-启动项
启动项: spring-boot-starter-xxxx
说明: 包中已经将框架进行了整合.用户拿来就用
<!--springboot整合springmvc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
1.4.3 关于主启动类的说明
说明: @ComponentScan(“包路径!!!”)
规则: 当前包扫描的路径 默认就是主启动类所在的包路径…
注意事项: 以后写代码 必须在主启动类所在包路径的 同包及子包中编辑
1.4.4 开箱即用-自动配置注解
注解名称: @EnableAutoConfiguration 启动自动化的配置
规则: 该注解的作用用来加载springBoot-start-xxx的启动项.当主启动类执行时,则会开始加载启动项中的配置.则功能加载成功.
1.5 SpringBoot-YML配置文件说明
1.5.1 properties文件说明
说明: SpringBoot项目中有多个配置文件.如果大量的重复的配置项都写到其中,则用户体验不好.
如果需要优化,则可以修改为yml文件
1.5.2 编辑application.yml
说明: 将application.properties的文件 修改名称为application.yml文件
基本语法:
# 基本语法
# 1.数据结构 key-value结构
# 2.写法 key: value 空格
# 3.有层级结构 注意缩进
server:
port: 8080
1.6 SpringBoot入门案例
1.6.1 业务需求
说明: 动态为属性赋值. 之后通过页面展现数据.
1.6.2 编辑msg.properties文件
msg=我是springboot的第一天
1.6.3 编辑HelloController
package com.jt.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@PropertySource(value = "classpath:/msg.properties",encoding = "UTF-8")
public class HelloController {
@Value("${msg}")
private String msg;
@GetMapping("/hello")
public String hello(){
return "您好SpringBoot"+msg;
}
}
1.6.4 页面效果展现
1.7 lombok插件介绍
1.7.1 lombok插件介绍
常规开发中POJO类必须手写get/set/toString/构造/…等方法,这类操作写起来鸡肋.但是又不得不写. 开发效率低.
所以可以引入lombok插件 自动生成上述的方法.
1.7.2 插件安装
1.引入jar包
<!--添加lombok的包-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
2.安装插件
3.安装lombok
官网路径整合idea;URL: https://plugins.jetbrains.com/plugin/6317-lombok
1.7.3 lombok的使用
package com.jt.pojo;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
@Data //Getter/Setter/RequiredArgsConstructor/ToString/EqualsAndHashCode
@Accessors(chain = true) //重写了set方法. 可以实现链式加载
public class User implements Serializable {
private Integer id;
private String name;
/* public User setId(Integer id){
this.id = id;
return this; //当前对象 运行期有效
}
public User setName(String name){
this.name = name;
return this; //当前对象 运行期有效
}*/
}
1.7.4 lombok 案例测试
测试set方法能否正常调用
2.1 Lombok用法
2.1.1 常用注解
2.1.2 关于Lombok使用说明
问题:Lombok的使用需要添加jar包和安装插件。未来项目在Linux服务器中进行部署。问 Linux服务器中是否需要安装lombok的插件
答案: 不需要 lombok插件编译器有效. xxx.java文件 编译为xxx.class文件
2.2 SpringBoot测试方法说明
2.2.1 方法说明
SpringBoot为了简化程序测试过程,springBoot针对于测试方法,开发了一个注解@SpringBootTest.
规则说明:
1. 当运行@Test注解标识的方式时,SpringBoot程序启动.
2. SpringBoot启动,内部Spring容器启动.基于IOC管理对象,DI注入对象
3. 可以在任意的测试类中注入想要的对象.
注意事项:
测试类的包路径,必须在主启动类的同包及子包中编辑.
2.2.2 测试方法说明
package com.jt;
import com.jt.controller.HelloController;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest //测试类启动,Spring容器启动
class SpringbootDemo1ApplicationTests {
@Autowired
private HelloController helloController;
//通过测试方法,可以自己调用,检查流程是否正常
@Test
void contextLoads() {
System.out.println(helloController.hello());
}
}
3. 前后端调用补充知识
3.1 Axios的简化写法
3.1.1 编辑WebController
package com.jt.controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@CrossOrigin //标识跨域的
@RequestMapping("/web")
public class WebController {
/**
* URL: http://localhost:8080/web/hello
* 类型: get
* 返回值: String
*/
@GetMapping("/hello")
public String hello(){
return "好好学习,天天向上";
}
}
3.1.2 编辑页面HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Axios测试</title>
<script src="../js/axios.js"></script>
</head>
<body>
<h1>Axios简化测试3</h1>
<script>
/* 简化方式1: 抽取后端服务器地址 */
axios.defaults.baseURL = "http://localhost:8080"
/* 简化方式2: 箭头函数使用 如果参数只有一个可以省略括号 */
axios.get("/web/hello")
.then( promise => {
alert(promise.data) //嵌套结构
})
/* 简化方式3: async await简化调用 重点
问题描述: 如果ajax如果嵌套的层级较多,则引发"回调地狱"问题
解决问题: 能否将axios中的then进行简化.
语法:
1. 使用async关键字标识函数
2. 通过await标识ajax请求
3. 必须同时出现
*/
async function getHello(){ //定义函数
//let {data: result,status: code} = await axios.get("/web/hello")
let {data: result} = await axios.get("/web/hello")
alert(result)
}
//调用函数
getHello()
/* let result1 = axios.get("/web/hello")
let result2 = axios.get("/web/hello",参数:result1)
let result3 = axios.get("/web/hello",参数:result2)
let result4 = axios.get("/web/hello",参数:result3) */
</script>
</body>
</html>
2.2 跨域问题
2.2.1 同源策略
要素:
1. 浏览器URL中的地址 例子:http://localhost:8848/webDemo/demo/3-axios.html
2. Ajax请求的URL地址 例子: http://localhost:8080/web/hello
要求: 上述要素必须满足 协议/域名/端口号都相同时.表示满足同源策略.
说明:
如果满足同源策略,则称之为 “同域访问” 反之称之为 “跨域访问” 跨域访问浏览器一般都会报错
2.2.2 关于跨域案例讲解
案例1:
浏览器地址: http://localhost:8080/xx/xxx
Ajax地址: https://localhost:8080/yy/yyyy 跨域请求:协议不同
案例2:
前提: 域名与IP地址对应
浏览器地址: http://www.jt.com:8080/xx/xxx
Ajax地址: http://10.4.5.2:8080/yy/yyyy 跨域请求:域名不同
案例3:
浏览器地址: http://www.jt.com/xx/xxx
Ajax地址: http://www.jt.com:80/yy/yyyy 同域请求:http端口号默认80
案例4:
浏览器地址: https://www.jt.com:443/xx/xxx
Ajax地址: https://www.jt.com:443/yy/yyyy 同域请求: https协议默认443端口
案例5:
迷惑: IP表示同一个网段
浏览器地址: http://192.168.10.2:80/xx/xxx
Ajax地址: http://192.168.11.2:80/yy/yy 跨域请求: 域名不匹配
2.2.3 跨域解决方案
1.jsonp 方式跨域 淘汰了.
2.CORS 跨域资源共享
跨源资源共享 (CORS) (或通俗地译为跨域资源共享)是一种基于HTTP 头的机制,该机制通过允许服务器标示除了它自己以外的其它origin(域,协议和端口),这样浏览器可以访问加载这些资源。跨源资源共享还通过一种机制来检查服务器是否会允许要发送的真实请求,该机制通过浏览器发起一个到服务器托管的跨源资源的"预检"请求。在预检中,浏览器发送的头中标示有HTTP方法和真实请求中会用到的头。
总结: CORS要求在服务器端标识哪个网址可以跨域