1、 简单概览
1.1、REST风格
REST(Representational State Transfer),表现形式状态转换,它是一种软件架构风格
在前后端分离的开发模式中,前后端开发人员都需要根据提前定义好的接口文档,来进行前后端功能的开发,而在前后端进行交互的时候,我们需要基于当前主流的REST风格的API接口进行交互
在REST风格的URL中,我们通过四种请求方式,来操作数据的增删改查。
1. GET : 查询
2. POST :新增
3. PUT :修改
4.DELETE :删除
1.2、 统一响应结果
前后端工程在进行交互时,使用统一响应结果 Result
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
private Integer code;//响应码,1 代表成功; 0 代表失败
private String msg; //响应码 描述字符串
private Object data; //返回的数据
//增删改 成功响应
public static Result success(){
return new Result(1,"success",null);
}
//查询 成功响应
public static Result success(Object data){
return new Result(1,"success",data);
}
//失败响应
public static Result error(String msg){
return new Result(0,msg,null);
}
}
1.3、 开发流程
我们在进行功能开发时,都是根据如下流程进行:
1. 查询页面原型明确需求
2. 阅读接口文档:来完成前后端统一,防止出现不统一造成的问题
3. 思路分析
4. 接口开发:就是开发后台的业务功能,一个业务功能,我们称为一个接口
5. 接口测试:功能开发完毕后,先通过Postman进行接口测试,测试通过后,和前端进行联调测试
6. 前后端联调测试:和前端开发人员开发好的前端工程一起测试
1.4.、 Mapper层与Mappers文件的不同
在Mapper层使用Mybatis的注解,主要是来完成一些简单的增删改查功能。如果需要实现复杂的SQL功能,建议使用XML来配置映射语句,也就是将SQL语句写在XML配置文件中。
1.4.、 一般的操作流程
以下代码就是一个增加员工操作的全部流程:
我们通常在阅读需求以后去Controller层接收并响应请求,用到的方法我们先写上
我们用Alt+Enter点击报红的方法,让他去Service层创建抽象方法
这时候ServiceImpl实现类会提示错误,自然也是Alt+Enter点击,自动重写方法。
重写的方法我们需要按需求修改,跟第一步一样,用到的方法我们先写上,然后继续交给Idea
最后再去自动生成的Mapper层完成与数据库的交互即可
2.5.2、 工具类
我们需要创建一个Utils工具类包,来存放AliOssTest文件,文件内容如下
package com.itheima.Utils;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.itheima.pojo.AliOSSProperties;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* 阿里云 OSS 工具类
*/
@Component
@Data
public class AliOSSUtils {
@Autowired
private AliOSSProperties aliOSSProperties;
/**
* 实现上传图片到OSS
*/
public String upload(MultipartFile multipartFile) throws IOException {
//数据设置
String endpoint = aliOSSProperties.getEndpoint();
String accessKeyId = aliOSSProperties.getAccessKeyId();
String accessKeySecret = aliOSSProperties.getAccessKeySecret();
String bucketName = aliOSSProperties.getBucketName();
// 获取上传的文件的输入流
InputStream inputStream = multipartFile.getInputStream();
// 避免文件覆盖
String fileName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss")) + multipartFile.getOriginalFilename();
//上传文件到 OSS
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
ossClient.putObject(bucketName, fileName, inputStream);
//文件访问路径
String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
// 关闭ossClient
ossClient.shutdown();
return url;// 把上传到oss的路径返回
}
}
2.5.3、 实体类
在pojo包下建立AliOSSProperties实体类
package com.itheima.pojo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "aliyun.oss") //指定配置文件
public class AliOSSProperties {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
}
2.5.4、 准备yml配置文件
我们首先应该把原本的application.properties配置文件里的内容替换为application.yml
1. 大小写敏感
2. 数值前边必须有空格,作为分隔符
3. 使用缩进表示层级关系,缩进时,不允许使用Tab键,只能用空格(idea中会自动将Tab转换为空格)
4. 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
5. 表示注释,从这个字符一直到行尾,都会被解析器忽略
具体改法如下:
#数据库驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#链接地址
spring.datasource.url=你的数据
#账号
spring.datasource.username=你的数据
#密码
spring.datasource.password=你的数据
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver #数据库驱动
url: 你的数据 #链接地址
username: 你的数据 #账号
password: 你的数据 #密码
其中阿里云配置如下:
aliyun: #阿里云oss配置
oss:
endpoint: 你自己的 #oss外网访问地域节点
accessKeyId: 你自己的 #ossAccessKeyId
accessKeySecret: 你自己的 #ossaccessKeySecret
bucketName: 你自己的 #ossBucket名称