註:本文所用到的版本 MySql 8.0.28 SpringBoot 2.7.2 準備工作 :建表 、pom.xml導入依賴 、application.yml 配置 建表 CREATE TABLE `rna` ( `id` int NOT NULL AUTO_INCREMENT, `name` va ...
註:本文所用到的版本
MySql 8.0.28
SpringBoot 2.7.2
準備工作 :建表 、pom.xml導入依賴 、application.yml 配置
- 建表
CREATE TABLE `rna` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`card_img` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`card` varchar(18) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`sex` varchar(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`phone` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`check` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`check_date` datetime DEFAULT NULL,
`result` varchar(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
- pom.xml導入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 生成get set -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- msql資料庫 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid數據源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.11</version>
</dependency>
<!-- mybatis啟動器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!-- pageHelper分頁插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.2</version>
</dependency>
<!-- 文件上傳 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
- application.yml 配置
server:
port: 8100
spring:
datasource:
url: jdbc:mysql://localhost:3306/db03
username: 你的用戶名
password: 你的密碼
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
mybatis:
mapper-locations: mapper/*.xml
type-aliases-package: com.lyt.pojo
目錄結構
上傳文件工具類
package com.lyt.util;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
/**
* @author liuyutao
* @date 2022/07/27
*/
public class UploadUtil {
//上傳文件的路徑首碼
public static String uploadPathPrefix = "D:\\upload\\images\\";
public static String upload(MultipartFile uploadFile, String oldPath) {
String uploadFullPath;//上傳文件的完整路徑
String saveSqlPartFilePath;//返回該屬性,保存到資料庫部分文件路徑
if (ObjectUtils.isEmpty(oldPath)) {
String originalFilename = uploadFile.getOriginalFilename();//上傳文件原始文件名
assert originalFilename != null;
String fileType = originalFilename.substring(originalFilename.lastIndexOf("."));//文件類型
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy\\M\\");
String format = sdf.format(date);
String uuid = UUID.randomUUID().toString().replace("-", "");
String saveDiskFileName = uuid + fileType;// 保存到磁碟的文件名
saveSqlPartFilePath = format + saveDiskFileName;
uploadFullPath = uploadPathPrefix + saveSqlPartFilePath;//上傳文件的完整路徑: D:\\upload\\images\\ yyyy\\M\\uuid文件名
} else {
uploadFullPath = uploadPathPrefix + oldPath;
saveSqlPartFilePath = oldPath;
}
File file = new File(uploadFullPath);
try {
if (!file.exists()) {
file.mkdirs();
}
uploadFile.transferTo(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
return saveSqlPartFilePath;
}
}
SpringBoot 虛擬路徑映射
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/upload/**").addResourceLocations("file:D:/upload/images/");
}
}
新增 + 上傳文件
@PostMapping("/addRna")
public int addRna(@RequestParam("file") MultipartFile file, @RequestPart Rna rna) {
String cardImg = UploadUtil.upload(file, null);
rna.setCardImg(cardImg);
return rnaService.insertSelective(rna);
}
思路:
- 文件 和 對象數據 同時訪問一個介面
- 上傳文件後返迴文件的部分路徑,非全路徑
- 設置到rna對象,一同添加到資料庫
編輯 + 上傳文件
@PostMapping("/updateRna")
public int updateRna(@RequestParam("file") MultipartFile file, @RequestPart Rna rna) {
if (!file.isEmpty()) {
Rna rnaById = rnaService.selectByPrimaryKey(rna.getId());
UploadUtil.upload(file, rnaById.getCardImg());
}
return rnaService.updateByPrimaryKeySelective(rna);
}
-
這裡不是刪除原來文件,而是同路徑同文件名,覆蓋原文件
-
有上傳新文件,則覆蓋原文件,編輯其他欄位數據
-
沒有上傳文件,編輯其他欄位數據
查詢
- PageHelper 插件分頁
- 拼接文件的完整url訪問路徑
刪除
- 刪除資料庫數據 + 刪除文件