这里写目录标题
day07
SpringBoot框架
IDEA创建项目
pom.xml文件说明
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>
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>
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>
开箱即用原理说明
介绍
springBoot将繁琐的配置封装到某些jar包中. 该jar包中的文件已经完成了配置.引入即可使用.只需要少量的配置就可以获取其功能的方式 叫做 “开箱即用”
开箱即用规则-启动项
启动项: spring-boot-starter-xxxx
说明: 包中已经将框架进行了整合.用户拿来就用
<!--springboot整合springmvc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
关于主启动类的说明
说明: @ComponentScan(“包路径!!!”)
规则: 当前包扫描的路径 默认就是主启动类所在的包路径…
注意事项: 以后写代码 必须在主启动类所在包路径的 同包及子包中编辑
开箱即用-自动配置注解
注解名称: @EnableAutoConfiguration 启动自动化的配置
规则: 该注解的作用用来加载springBoot-start-xxx的启动项.当主启动类执行时,则会开始加载启动项中的配置.则功能加载成功.
YML配置文件说明
properties文件说明
说明: SpringBoot项目中有多个配置文件.如果大量的重复的配置项都写到其中,则用户体验不好.
如果需要优化,则可以修改为yml文件
application.yml
说明: 将application.properties的文件 修改名称为application.yml文件
基本语法:
# 基本语法
# 1.数据结构 key-value结构
# 2.写法 key: value 空格
# 3.有层级结构 注意缩进
server:
port: 8080
入门案例
动态赋值
说明: 动态为属性赋值. 之后通过页面展现数据.
编辑msg.properties文件
msg=我是springboot的第一天
应用 编辑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;
}
}
lombok插件介绍
常规开发中POJO类必须手写get/set/toString/构造/…等方法,这类操作写起来鸡肋.但是又不得不写. 开发效率低.
所以可以引入lombok插件 自动生成上述的方法.
插件安装
- 引入jar包
<!--添加lombok的包-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
- 安装插件
- 网址安装lombok
URL: https://plugins.jetbrains.com/plugin/6317-lombok
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; //当前对象 运行期有效
}*/
}
lombok 案例测试
B站地址: 不二子阳
day08
SpringBoot用法
Lombok用法
常用注解
关于Lombok使用说明
问题:Lombok的使用需要添加jar包和安装插件。未来项目在Linux服务器中进行部署。问 Linux服务器中是否需要安装lombok的插件
答案: 不需要 lombok插件编译器有效. xxx.java文件 编译为xxx.class文件
SpringBoot测试方法说明
方法说明
SpringBoot为了简化程序测试过程,springBoot针对于测试方法,开发了一个注解**@SpringBootTest.**
规则说明:
- 当运行@Test注解标识的方式时,SpringBoot程序启动.
- SpringBoot启动,内部Spring容器启动.基于IOC管理对象,DI注入对象
- 可以在任意的测试类中注入想要的对象.
注意事项:
测试类的包路径,必须在主启动类的同包及子包中编辑.
测试方法说明
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());
}
}
前后端调用补充知识
Axios的简化写法
编辑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 "好好学习,天天向上";
}
}
编辑页面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>