用springboot,vue,mybatis和elementUI实现上传PDF文件到数据库,需要上传到数据库的内容有文件名,文件上传时间,文件路径,上传者的编号。请告诉我springboot配置文件,domain代码,controller代码,service代码,前端vue和element代码,mybatis代码和数据库建表语句,并用注释讲解
时间: 2023-05-20 22:06:20 浏览: 135
很高兴为您提供解答。以下是您所需的代码和注释:
1. 配置文件 application.properties
```
# 数据库连接配置
spring.datasource.url=jdbc:mysql://localhost:3306/db_name?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
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 代码
```
package com.example.demo.domain;
import java.util.Date;
public class PdfFile {
private Integer id;
private String fileName;
private Date uploadTime;
private String filePath;
private Integer uploaderId;
// getter 和 setter 方法省略
}
```
3. Controller 代码
```
package com.example.demo.controller;
import com.example.demo.domain.PdfFile;
import com.example.demo.service.PdfFileService;
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.util.Date;
@RestController
@RequestMapping("/pdf")
public class PdfFileController {
@Autowired
private PdfFileService pdfFileService;
@PostMapping("/upload")
public String uploadPdfFile(@RequestParam("file") MultipartFile file, @RequestParam("uploaderId") Integer uploaderId) {
if (file.isEmpty()) {
return "上传失败,请选择文件";
}
String fileName = file.getOriginalFilename();
String filePath = "D:/pdf_files/" + fileName;
File dest = new File(filePath);
try {
file.transferTo(dest);
PdfFile pdfFile = new PdfFile();
pdfFile.setFileName(fileName);
pdfFile.setUploadTime(new Date());
pdfFile.setFilePath(filePath);
pdfFile.setUploaderId(uploaderId);
pdfFileService.addPdfFile(pdfFile);
return "上传成功";
} catch (IOException e) {
e.printStackTrace();
}
return "上传失败!";
}
}
```
4. Service 代码
```
package com.example.demo.service;
import com.example.demo.domain.PdfFile;
public interface PdfFileService {
void addPdfFile(PdfFile pdfFile);
}
```
```
package com.example.demo.service.impl;
import com.example.demo.dao.PdfFileMapper;
import com.example.demo.domain.PdfFile;
import com.example.demo.service.PdfFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class PdfFileServiceImpl implements PdfFileService {
@Autowired
private PdfFileMapper pdfFileMapper;
@Override
public void addPdfFile(PdfFile pdfFile) {
pdfFileMapper.addPdfFile(pdfFile);
}
}
```
5. 前端 Vue 和 Element 代码
```
<template>
<div>
<el-upload
class="upload-demo"
action="/pdf/upload"
:data="{ uploaderId: uploaderId }"
:on-success="handleSuccess"
:before-upload="beforeUpload"
:file-list="fileList"
multiple>
<el-button size="small" type="primary">点击上传</el-button>
<div slot="tip" class="el-upload__tip">只能上传 PDF 文件</div>
</el-upload>
</div>
</template>
<script>
export default {
data() {
return {
uploaderId: 1, // 上传者的编号
fileList: []
}
},
methods: {
beforeUpload(file) {
const isPdf = file.type === 'application/pdf';
if (!isPdf) {
this.$message.error('只能上传 PDF 文件');
}
return isPdf;
},
handleSuccess(response, file, fileList) {
if (response === '上传成功') {
this.$message.success(response);
} else {
this.$message.error(response);
}
}
}
}
</script>
```
6. Mybatis 代码
```
package com.example.demo.dao;
import com.example.demo.domain.PdfFile;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface PdfFileMapper {
@Insert("insert into pdf_file(file_name, upload_time, file_path, uploader_id) values(#{fileName}, #{uploadTime}, #{filePath}, #{uploaderId})")
void addPdfFile(PdfFile pdfFile);
}
```
7. 数据库建表语句
```
CREATE TABLE `pdf_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` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
希望以上代码和注释能够帮助您实现上传 PDF 文件到数据库的功能。
阅读全文