当前位置: 首页 > news >正文

【Springboot3+Mybatis】文件上传阿里云OSS 基础管理系统CRUD

文章目录

  • 一、需求&开发流程
  • 二、环境搭建&数据库准备
  • 三、部门管理
  • 四、员工管理
    • 4.1 分页(条件)查询
    • 4.2 批量删除员工
  • 五、文件上传
    • 5.1 介绍
    • 5.2 本地存储
    • 5.3 阿里云OSS
      • 1. 开通OSS
      • 2. 创建存储空间Bucket
    • 5.4 OSS快速入门
    • 5.5 OSS上传显示文件
  • 六、配置文件
    • 6.1 yml配置
    • 6.2 @ConfigurationProperties
  • 总结
    • 第三方服务-通用思路 SDK
    • yml配置文件全


一、需求&开发流程

Github项目:Springboot3+Mybatis 基础管理系统

  1. 部门管理
    • 查询部门列表
    • 删除部门
    • 新增部门
    • 修改部门
  2. 员工管理
    • 查询员工列表(分页、条件)
    • 删除员工
    • 新增员工
    • 修改员工
  3. 文件上传

1

  1. 查看页面原型明确需求

    • 根据页面原型和需求,进行表结构设计、编写接口文档
  2. 阅读接口文档

  3. 思路分析

  4. 功能接口开发

    • 就是开发后台的业务功能,一个业务功能,我们称为一个接口
  5. 功能接口测试

    • 功能开发完毕后,先通过Postman进行功能接口测试,测试通过后,再和前端进行联调测试
  6. 前后端联调测试

    • 和前端开发人员开发好的前端工程一起测试

二、环境搭建&数据库准备

1
实体类:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept implements Serializable {private Integer id;private String name;private LocalDateTime createTime;private LocalDateTime updateTime;private static final long serialVersionUID = 1L;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp implements Serializable {private Integer id;private String username;private String password;private String name;private Integer gender;private String image;private Integer job;private LocalDate entrydate; //LocalDateprivate Integer deptId;private LocalDateTime createTime;//LocalDateTime private LocalDateTime updateTime;//LocalDateTime private static final long serialVersionUID = 1L;
}

步骤: 【SpringBoot3+Mybatis】框架快速搭建

  1. 准备数据库表(dept、emp)
  2. 创建springboot工程,引入对应的起步依赖(web、mybatis、mysql驱动、lombok)
  3. 准备基本工具类 utils.
  4. 配置文件application.properties中引入mybatis的配置信息,准备对应的实体类
  5. 准备对应的Mapper、Service(接口、实现类)、Controller基础结构

接口使用REST风格:【SpringMVC】RESTFul风格设计和实战 第三期

http://localhost:8080/users/1  GET:查询id为1的用户
http://localhost:8080/users    POST:新增用户
http://localhost:8080/users    PUT:修改用户
http://localhost:8080/users/1  DELETE:删除id为1的用户

通过URL定位要操作的资源,通过HTTP动词(请求方式)来描述具体的操作。

注意事项:

  • REST是风格,是约定方式,约定不是规定,可以打破
  • 描述模块的功能通常使用复数,也就是加s的格式来描述,表示此类资源,而非单个资源。如:users、emps、books…

三、部门管理

原型和需求:
1
1
LocalDateTime

四、员工管理

4.1 分页(条件)查询

Vo:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class EmpVo {private String name;private Short gender;private LocalDate begin;//LocalDate  2024-01-01private LocalDate end;//LocalDateprivate Integer page;private Integer pageSize;
}

controller

@RestController
@RequestMapping("emps")
public class EmpController {@Autowiredprivate EmpService empService;@GetMappingpublic Result queryPage(EmpVo empVo){Result result = empService.queryPage(empVo);return result;}
}

service:

@Service
public class EmpServiceImpl implements EmpService {@Autowiredprivate EmpMapper empMapper;@Overridepublic Result queryPage(EmpVo empVo) {if (empVo.getPage() == null || empVo.getPageSize()== null) {PageHelper.startPage(1, 10);}else{PageHelper.startPage(empVo.getPage(),empVo.getPageSize());}List<Emp> empList = empMapper.selectBySelective(empVo);PageInfo<Emp> empPageInfo = new PageInfo<>(empList);Map map = new HashMap();map.put("total",empPageInfo.getTotal());map.put("rows",empList);if (empList.isEmpty()){return Result.error("无");}return Result.success(map);}
}

mapperxml

    <select id="selectBySelective" resultType="com.wake.pojo.Emp">select *from emp<where><if test="empVo.name != null">name like concat('%',#{empVo.name},'%')</if><if test="empVo.gender != null">and gender=#{empVo.gender}</if><if test="empVo.end != null and empVo.begin != null ">and entrydate between #{empVo.begin} and #{empVo.end}</if></where></select>

4.2 批量删除员工

1
controller

    /*** 批量删除员工的数据信息* @param ids* @return*/@DeleteMapping("{ids}")public Result deleteByIds(@PathVariable Integer[] ids){Result result = empService.deleteByIds(ids);return result;}

service

    @Overridepublic Result deleteByIds(Integer[] ids) {int rows = empMapper.deleteByIds(ids);if (rows > 0) {return Result.success(null);}return Result.error("为空");}

mapperxml

    <delete id="deleteByIds">delete from emp where id in<foreach collection="ids" open="(" close=")" separator="," item="id">#{id}</foreach></delete>

五、文件上传

5.1 介绍

Spring中提供了一个API:MultipartFile,使用这个API就可以来接收到上传的文件
1

5.2 本地存储

MultipartFile 常见方法:
1

@Slf4j
@RestController
public class UploadController {@PostMapping("/upload")public Result upload(String username, Integer age, MultipartFile image) throws IOException {log.info("文件上传:{},{},{}",username,age,image);//获取原始文件名String originalFilename = image.getOriginalFilename();//构建新的文件名String extname = originalFilename.substring(originalFilename.lastIndexOf("."));//文件扩展名String newFileName = UUID.randomUUID().toString()+extname;//随机名+文件扩展名//将文件存储在服务器的磁盘目录image.transferTo(new File("E:/images/"+newFileName));return Result.success();}
}

上传大文件时报错:
1
添加文件上传的容量配置:

spring:servlet:multipart:max-file-size: 10MB #配置单个文件最大上传大小max-request-size: 100MB #配置单个请求最大上传大小(一次请求可以上传多个文件)

本地存储的问题:
1

如果直接存储在服务器的磁盘目录中,存在以下缺点:

  • 不安全:磁盘如果损坏,所有的文件就会丢失
  • 容量有限:如果存储大量的图片,磁盘空间有限(磁盘不可能无限制扩容)
  • 无法直接访问

为了解决上述问题呢,通常有两种解决方案:

  • 自己搭建存储服务器,如:fastDFS 、MinIO
  • 使用现成的云服务,如:阿里云,腾讯云,华为云

5.3 阿里云OSS

1

1. 开通OSS

阿里云——官网
1
1

2. 创建存储空间Bucket

1
1

5.4 OSS快速入门

阿里云OSS使用官方文档

阿里依赖,具体看官方使用文档:

        <dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.15.1</version></dependency><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>2.3.1</version></dependency><dependency><groupId>javax.activation</groupId><artifactId>activation</artifactId><version>1.1.1</version></dependency><!-- no more than 2.3.3--><dependency><groupId>org.glassfish.jaxb</groupId><artifactId>jaxb-runtime</artifactId><version>2.3.3</version></dependency>

案例代码:

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import java.io.File;public class Demo {public static void main(String[] args) throws Exception {// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();// 填写Bucket名称,例如examplebucket。String bucketName = "examplebucket";// 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。String objectName = "exampledir/exampleobject.txt";// 填写本地文件的完整路径,例如D:\\localpath\\examplefile.txt。// 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件。String filePath= "D:\\localpath\\examplefile.txt";// 创建OSSClient实例。OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);try {// 创建PutObjectRequest对象。PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new File(filePath));// 如果需要上传时设置存储类型和访问权限,请参考以下示例代码。// ObjectMetadata metadata = new ObjectMetadata();// metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());// metadata.setObjectAcl(CannedAccessControlList.Private);// putObjectRequest.setMetadata(metadata);// 上传文件。PutObjectResult result = ossClient.putObject(putObjectRequest);           } catch (OSSException oe) {System.out.println("Caught an OSSException, which means your request made it to OSS, "+ "but was rejected with an error response for some reason.");System.out.println("Error Message:" + oe.getErrorMessage());System.out.println("Error Code:" + oe.getErrorCode());System.out.println("Request ID:" + oe.getRequestId());System.out.println("Host ID:" + oe.getHostId());} catch (ClientException ce) {System.out.println("Caught an ClientException, which means the client encountered "+ "a serious internal problem while trying to communicate with OSS, "+ "such as not being able to access the network.");System.out.println("Error Message:" + ce.getMessage());} finally {if (ossClient != null) {ossClient.shutdown();}}}
}

1
1

5.5 OSS上传显示文件

在新增员工的时候,上传员工的图像,而之所以需要上传员工的图像,是因为将来我们需要在系统页面当中访问并展示员工的图像。而要想完成这个操作,需要做两件事:

  1. 需要上传员工的图像,并把图像保存起来(存储到阿里云OSS)
  2. 访问员工图像(通过图像在阿里云OSS的存储地址访问图像)
    • OSS中的每一个文件都会分配一个访问的url,通过这个url就可以访问到存储在阿里云上的图片。所以需要把url返回给前端,这样前端就可以通过url获取到图像。

工具类:(引入外部文件注入,调用get)

package com.wake.utils;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;@Component
public class AliOSSUtils {//注入配置参数实体类对象@Autowiredprivate AliOSSProperties aliOSSProperties;/*** 实现上传图片到OSS*/public String upload(MultipartFile multipartFile) throws IOException {// 获取上传的文件的输入流InputStream inputStream = multipartFile.getInputStream();// 避免文件覆盖String originalFilename = multipartFile.getOriginalFilename();String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));//上传文件到 OSSOSS ossClient = new OSSClientBuilder().build(aliOSSProperties.getEndpoint(),aliOSSProperties.getAccessKeyId(), aliOSSProperties.getAccessKeySecret());ossClient.putObject(aliOSSProperties.getBucketName(), fileName, inputStream);//文件访问路径String url =aliOSSProperties.getEndpoint().split("//")[0] + "//" + aliOSSProperties.getBucketName() + "." + aliOSSProperties.getEndpoint().split("//")[1] + "/" + fileName;// 关闭ossClientossClient.shutdown();return url;// 把上传到oss的路径返回}
}

controller:

@RestController
public class UploadController {@Autowiredprivate AliOSSUtils aliOSSUtils;@PostMapping("/upload")public Result upload(MultipartFile image) throws IOException {//调用阿里云OSS工具类,将上传上来的文件存入阿里云String url = aliOSSUtils.upload(image);//将图片上传完成后的url返回,用于浏览器回显展示return Result.success(url);}
}

1
1

六、配置文件

6.1 yml配置

yml配置文件中:

aliyun: #以下参数全部修改成自己的oss:endpoint: https://oss-cn-fuzhou.aliyuncs.comaccessKeyId: LTAI5t6Av5GLDxX  #假的修改accessKeySecret: C1IrHzKZKvcotD4d5Tc #假的修改bucketName: web-wake-work

创建实体类存放字段属性:
1
直接使用注解 @ConfigurationProperties(prefix = "aliyun.oss")
实体类中的属性名和配置文件当中key名字必须要一致

package com.wake.utils;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Data
@Component
@ConfigurationProperties(prefix = "aliyun.oss") //这样不用一个一个属性挂载@Value
public class AliOSSProperties {private String endpoint;private String accessKeyId;private String accessKeySecret;private String bucketName;
}

6.2 @ConfigurationProperties

添加注解出现红色提示,添加依赖即可
1

        <!--   @ConfigurationProperties 注解--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId></dependency>
  • @Value注解只能一个一个的进行外部属性的注入。
  • @ConfigurationProperties可以批量的将外部的属性配置注入到bean对象的属性中。
  • 通过 configuration properties 批量的将外部的属性配置直接注入到 bin 对象的属性当中。
  • 在其他的类当中,我要想获取到注入进来的属性,我直接注入 bin 对象,然后调用 get 方法,就可以获取到对应的属性值了

总结

在这里插入图片描述

第三方服务-通用思路 SDK

在这里插入图片描述
SDK:Software Development Kit 的缩写,软件开发工具包,包括辅助软件开发的依赖(jar包)、代码示例等,都可以叫做SDK。

.
简单说,sdk中包含了我们使用第三方云服务时所需要的依赖,以及一些示例代码。我们可以参照sdk所提供的示例代码就可以完成入门程序。

yml配置文件全

server:servlet:context-path: /spring:datasource:# 连接池类型type: com.alibaba.druid.pool.DruidDataSource  # 使用Druid连接池# Druid的其他属性配置 springboot3整合情况下,数据库连接信息必须在Druid属性下!druid:url: jdbc:mysql://localhost:3306/db01_mybatisusername: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driverservlet:multipart:max-file-size: 10MB #配置单个文件最大上传大小max-request-size: 100MB #配置单个请求最大上传大小(一次请求可以上传多个文件)mybatis:configuration:  # setting配置auto-mapping-behavior: full # 开启resultMap自动映射 设置映射等级full 复杂情况也能映射 多表联查相关map-underscore-to-camel-case: true # true开启属性字段驼峰命名自动映射,将xxx_xxx这样的列名自动映射到xxXxx这样驼峰式命名的属性名log-impl: org.apache.ibatis.logging.stdout.StdOutImpl#    log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpltype-aliases-package: com.wake.pojo # 配置别名 批量将包下的类,设置别名都为首字母小写mapper-locations: classpath:/mappers/*.xml # mapperxml位置aliyun: #以下参数全部修改成自己的oss:endpoint: https://oss-cn-fuzhou.aliyuncs.comaccessKeyId: LTAI5t9ZK8iq5T2Av6GLDxX  #假的修改accessKeySecret: C0IrHKqU8S8YQcevcotD3Zd5Tc #假的修改bucketName: web-wake-work

相关文章:

  • draw.io 去除箭头
  • 【黄金手指】windows操作系统环境下使用jar命令行解压和打包Springboot项目jar包
  • Javascript中的严格模式 “use strict“
  • 华为ensp中rip动态路由协议原理及配置命令(详解)
  • 29-5 webshell 流量分析 - 菜刀
  • 前端性能优化:防抖与节流
  • 【python开发】并发编程和网络编程的结合+并发和并行概念区别+单例模式通过类来实现
  • 【TypeScript系列】三斜线指令
  • 关于Mysql表中使用‘utf8mb4_unicode_ci’字符集问题
  • 鸿蒙OpenHarmony开发实战:【MiniCanvas】
  • MySQL多表联查会重复查找记录
  • 利用免费 GPU 部署体验大型语言模型推理框架 vLLM
  • 实用推荐!盘点2024年六款顶尖客户管理软件,助力企业发展!
  • 2024年华为OD机试真题-部门人力分配-Python-OD统一考试(C卷)
  • Spring Boot1
  • [微信小程序] 使用ES6特性Class后出现编译异常
  • 【跃迁之路】【477天】刻意练习系列236(2018.05.28)
  • 345-反转字符串中的元音字母
  • android 一些 utils
  • Angular2开发踩坑系列-生产环境编译
  • emacs初体验
  • JavaScript/HTML5图表开发工具JavaScript Charts v3.19.6发布【附下载】
  • JavaScript对象详解
  • Java应用性能调优
  • JSONP原理
  • Laravel 中的一个后期静态绑定
  • MySQL数据库运维之数据恢复
  • overflow: hidden IE7无效
  • pdf文件如何在线转换为jpg图片
  • PyCharm搭建GO开发环境(GO语言学习第1课)
  • Python - 闭包Closure
  • Python爬虫--- 1.3 BS4库的解析器
  • Redis在Web项目中的应用与实践
  • sessionStorage和localStorage
  • zookeeper系列(七)实战分布式命名服务
  • 解决jsp引用其他项目时出现的 cannot be resolved to a type错误
  • 浅谈Kotlin实战篇之自定义View图片圆角简单应用(一)
  • 容器服务kubernetes弹性伸缩高级用法
  • 使用Envoy 作Sidecar Proxy的微服务模式-4.Prometheus的指标收集
  • 听说你叫Java(二)–Servlet请求
  • 小程序、APP Store 需要的 SSL 证书是个什么东西?
  • 《码出高效》学习笔记与书中错误记录
  • scrapy中间件源码分析及常用中间件大全
  • 阿里云服务器如何修改远程端口?
  • 国内唯一,阿里云入选全球区块链云服务报告,领先AWS、Google ...
  • #laravel 通过手动安装依赖PHPExcel#
  • #stm32驱动外设模块总结w5500模块
  • #绘制圆心_R语言——绘制一个诚意满满的圆 祝你2021圆圆满满
  • (1)(1.11) SiK Radio v2(一)
  • (Matlab)遗传算法优化的BP神经网络实现回归预测
  • (二)学习JVM —— 垃圾回收机制
  • (附源码)springboot炼糖厂地磅全自动控制系统 毕业设计 341357
  • (附源码)springboot助农电商系统 毕业设计 081919
  • (三)elasticsearch 源码之启动流程分析
  • (十)c52学习之旅-定时器实验