从永远到永远-SpringCloud项目实战(二)

1、阿里云对象存储OOS
在这里插入图片描述在这里插入图片描述在这里插入图片描述

2、后台操作(略)
3、Java代码操作
1)获取Access Key
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2)导入依赖
3)配置application.yml
4)常量读取处理,注意下边初始化的方式,涉及源码后期待补

@Component
public class ConstantPropertiesUtils implements InitializingBean {
	@Value("${aliyun.oos.file.endpoint}")
	private String endpoint;
	@Value("${aliyun.oos.file.keyid}")
	private String keyId;
	@Value("${aliyun.oos.file.keysecret}")
	private String keySecret;
	@Value("${aliyun.oos.file.bucketname}")
	private String bucketName;

	public static String END_POINT;
	public static String KEY_ID;
	public static String KEY_SECRET;
	public static String BUCKET_NAME;
	@Override
	public void afterPropertiesSet() throws Exception {
		END_POINT=endpoint;
		KEY_ID=keyId;
		KEY_SECRET=keySecret;
		BUCKET_NAME=bucketName;
	}
}

5)具体上传代码见api文档。

@Service
public class OssServiceImpl implements OssService {
	@Override
	public String uploadFileAvatar(MultipartFile file) {
		// 工具类获取值
		String endpoint = ConstantPropertiesUtils.END_POINT;
		String accessKeyId = ConstantPropertiesUtils.KEY_ID;
		String accessKeySecret = ConstantPropertiesUtils.KEY_SECRET;
		String bucketName = ConstantPropertiesUtils.BUCKET_NAME;
		try {
			//创建OSS客户端
			OSS ossClient= new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
			//获取上传文件输入流
			InputStream inputStream = file.getInputStream();
			//获取文件名称
			String fileName = file.getOriginalFilename();
			//上传
			ossClient.putObject(bucketName,fileName,inputStream);
			// 关闭OSSClient。
			ossClient.shutdown();

			String url="";
			url="https://"+bucketName+"."+endpoint+"/"+fileName;
			return url;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
}

在这里插入图片描述
在这里插入图片描述

4、nginx
点我
5、头像上传(根据api改就可以)
1)引入外部组件
2)声明使用。
3)设置初始值
4)完善方法等

注意:this.属性的使用,现阶段理解(不一定对),不加this,会理解为当前方法的局部变量,所以就是没有定义
在这里插入图片描述

6、Easy Excel

在这里插入图片描述

1、导入依赖,底层也使用了poi,所以poi也要引入。要注意其有版本对应关系,引入对应版本:
easyexcel依赖如下,poi使用3.7版本

<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.1.1</version>
        </dependency>

2、创建实体类与excel对应

package com.scbg.edu.entity.excel;

import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;

/**
 * @program: education
 * @description: excel课程导入表格对应实体类
 * @author: WL
 * @create: 2020-05-25 22:58
 **/
@Data
public class SubjectData {
	@ExcelProperty(index = 0)
	private String oneSubjectName;
	@ExcelProperty(index = 1)
	private String twoSubjectName;
}

3、mvc
4、easy Excel的监听器。注意:该类未交由spring管理,所以要使用service实现类,需要手动注入。

package com.scbg.edu.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scbg.edu.entity.Subject;
import com.scbg.edu.entity.excel.SubjectData;
import com.scbg.edu.service.SubjectService;
import com.scbg.serviceBase.exception.EducationException;

/**
 * @program: education
 * @description: easyExcel使用的监听器
 * @author: WL
 * @create: 2020-05-25 23:07
 **/
public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {
	//因为该类没有交给spring容器管理,所以在此利用构造器手动注入service
	public SubjectService subjectService;

	public SubjectExcelListener(SubjectService subjectService) {
		this.subjectService = subjectService;
	}

	public SubjectExcelListener() {
	}

	//判断是否存在一级分类
	private Subject existOneSubject(SubjectService subjectService,String name){
		QueryWrapper<Subject> wrapper= new QueryWrapper<>();
		wrapper.eq("title",name);
		wrapper.eq("parent_id","0");
		Subject one = subjectService.getOne(wrapper);
		return one;
	}
	//判断是否存在二级分类
	private Subject existTwoSubject(SubjectService subjectService,String name,String parentId){
		QueryWrapper<Subject> wrapper= new QueryWrapper<>();
		wrapper.eq("title",name);
		wrapper.eq("parent_id",parentId);
		Subject one = subjectService.getOne(wrapper);
		return one;
	}

	@Override
	public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
		if (subjectData==null){
			throw new EducationException(20001,"文件数据为空");
		}
		Subject existOneSubject = this.existOneSubject(subjectService, subjectData.getOneSubjectName());
		if (existOneSubject==null){
			existOneSubject= new Subject();
			existOneSubject.setParentId("0");
			existOneSubject.setTitle(subjectData.getOneSubjectName());
			subjectService.save(existOneSubject);
		}

		Subject existTwoSubject = this.existTwoSubject(subjectService, subjectData.getTwoSubjectName(), existOneSubject.getId());
		if (existTwoSubject==null){
			existTwoSubject= new Subject();
			existTwoSubject.setParentId(existOneSubject.getId());
			existTwoSubject.setTitle(subjectData.getTwoSubjectName());
			subjectService.save(existTwoSubject);
		}
	}

	@Override
	public void doAfterAllAnalysed(AnalysisContext analysisContext) {

	}
}

7、课程分类展示后端

注意这种封装数据的思路
DTO分别对应1、2级数据。

8、课程分类展示前端
9、课程视频发布

mp工具类表对应代码生出来等。。。。

10、课程视频添加的前端

做成流程样式
1、添加路由

2、

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值