用springboot,vue,mybatis和elementUI实现上传PDF文件到数据库,需要上传到数据库的内容有文件名,文件上传时间,文件路径,上传者的编号,上传者编号为varchar型。请告诉我springboot配置文件,domain代码,controller代码,service代码,前端vue和element代码,mybatis代码和数据库建表语句,并用注释讲解
时间: 2023-05-20 19:06:36 浏览: 86
很高兴为您提供解答。以下是您所需的代码和注释:
1. SpringBoot配置文件
application.properties
```
# 数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# MyBatis配置
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.demo.domain
# 文件上传配置
spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring.servlet.multipart.file-size-threshold=2KB
```
2. Domain代码
File.java
```
package com.example.demo.domain;
import java.util.Date;
public class File {
private Integer id;
private String fileName;
private Date uploadTime;
private String filePath;
private String uploaderId;
// getter和setter方法省略
}
```
3. Controller代码
FileController.java
```
package com.example.demo.controller;
import com.example.demo.domain.File;
import com.example.demo.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
@RestController
@RequestMapping("/file")
public class FileController {
@Autowired
private FileService fileService;
@PostMapping("/upload")
public String upload(@RequestParam("file") MultipartFile multipartFile, @RequestParam("uploaderId") String uploaderId) {
// 获取文件名
String fileName = multipartFile.getOriginalFilename();
// 获取当前时间
Date uploadTime = new Date();
// 生成文件路径
String filePath = "D:/upload/" + new SimpleDateFormat("yyyyMMdd").format(uploadTime) + "/" + fileName;
// 创建文件对象
File file = new File(filePath);
// 判断目录是否存在,不存在则创建
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
// 将文件保存到磁盘
multipartFile.transferTo(file);
// 将文件信息保存到数据库
File fileEntity = new File();
fileEntity.setFileName(fileName);
fileEntity.setUploadTime(uploadTime);
fileEntity.setFilePath(filePath);
fileEntity.setUploaderId(uploaderId);
fileService.save(fileEntity);
return "上传成功";
} catch (IOException e) {
e.printStackTrace();
return "上传失败";
}
}
}
```
4. Service代码
FileService.java
```
package com.example.demo.service;
import com.example.demo.domain.File;
public interface FileService {
void save(File file);
}
```
FileServiceImpl.java
```
package com.example.demo.service.impl;
import com.example.demo.dao.FileDao;
import com.example.demo.domain.File;
import com.example.demo.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class FileServiceImpl implements FileService {
@Autowired
private FileDao fileDao;
@Override
public void save(File file) {
fileDao.save(file);
}
}
```
5. 前端Vue和Element代码
```
<template>
<div>
<el-upload
class="upload-demo"
action="/file/upload"
:headers="{ 'Content-Type': 'multipart/form-data' }"
:data="{ uploaderId: uploaderId }"
:on-success="handleSuccess"
:on-error="handleError"
:before-upload="beforeUpload"
:file-list="fileList"
multiple>
<el-button size="small" type="primary">点击上传</el-button>
<div slot="tip" class="el-upload__tip">只能上传PDF文件,且不超过10MB</div>
</el-upload>
</div>
</template>
<script>
export default {
data() {
return {
uploaderId: '123',
fileList: []
}
},
methods: {
handleSuccess(response, file, fileList) {
this.$message.success('上传成功');
this.fileList = fileList;
},
handleError(error, file, fileList) {
this.$message.error('上传失败');
},
beforeUpload(file) {
const isPDF = file.type === 'application/pdf';
const isLt10M = file.size / 1024 / 1024 < 10;
if (!isPDF) {
this.$message.error('只能上传PDF文件');
}
if (!isLt10M) {
this.$message.error('文件大小不能超过10MB');
}
return isPDF && isLt10M;
}
}
}
</script>
```
6. MyBatis代码
FileDao.java
```
package com.example.demo.dao;
import com.example.demo.domain.File;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface FileDao {
@Insert("INSERT INTO file(file_name, upload_time, file_path, uploader_id) VALUES(#{fileName}, #{uploadTime}, #{filePath}, #{uploaderId})")
void save(File file);
}
```
7. 数据库建表语句
```
CREATE TABLE `file` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`file_name` varchar(255) NOT NULL,
`upload_time` datetime NOT NULL,
`file_path` varchar(255) NOT NULL,
`uploader_id` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
希望这些代码和注释能够帮助您实现上传PDF文件到数据库的功能。
阅读全文