数据库设置
CREATE DATABASE IMAGETEST; USE IMAGETEST; CREATE TABLE `photo` ( `id` int(11) NOT NULL AUTO_INCREMENT, `images` varchar(100) COLLATE utf8_czech_ci DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci |
- 依赖
注意: 数据库驱动版本要找自己对应的数据库驱动、我的是5.6
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 修改成我们所需要的mysql版本 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
- 配置SpringBoot数据库连接文件
spring: datasource: # 我们这里用户名和密码都不需要双引号 , username: root password: xxxx url: jdbc:mysql://localhost:3306/imagetest?userUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.jdbc.Driver
- 实体类的配置如下
package com.jsxs.pojo; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; import java.sql.Blob; /** * @Author Jsxs * @Date 2023/4/2 20:54 * @PackageName:com.jsxs.pojo * @ClassName: demo * @Description: TODO * @Version 1.0 */ @Data @AllArgsConstructor @NoArgsConstructor public class photo implements Serializable { @TableId(type = IdType.AUTO) private int id; private String images; }
- Mapper层需要继承一个MybatisPlus接口
package com.jsxs.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.jsxs.pojo.photo; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; /** * @Author Jsxs * @Date 2023/4/3 14:45 * @PackageName:com.jsxs.mapper * @ClassName: userMapper * @Description: TODO * @Version 1.0 */ @Mapper @Repository public interface userMapper extends BaseMapper<photo> { }
- 文件上传
@Resource private userMapper mapper; //mybatisplus 自带类 // 文件上传 @Test void testUpload() throws IOException { // 被上传的文件 - 真实开发中我们通过html获取被上传的路径 String path="E:/加速器/2.jpg"; // 读取被上传的文件 FileInputStream fileInputStream = new FileInputStream(path); String path2="E:/Ideal源码/Test-image/src/main/java/com/jsxs/upload"; // 读入到的文件位置 String replace = UUID.randomUUID().toString().replace("-", ""); // UUID防止名字重复 String realPath=path2 + "/" + replace + ".jpg"; // 真实被放去的位置 FileOutputStream fileOutputStream = new FileOutputStream(realPath); // 数据库进行插入的操作 photo p = new photo(); p.setImages(realPath); mapper.insert(p); byte[] bytes = new byte[1024]; //缓冲区 int count=0; while ((count=fileInputStream.read(bytes))!=-1){ fileOutputStream.write(bytes,0,count); // 上传到我们的 upload路径 } fileInputStream.close(); // 关闭文件 fileOutputStream.close(); }
- 文件下载
@Resource private userMapper mapper; //mybatisplus 自带类 //文件下载 @Test void contextLoads() throws Exception { photo p = mapper.selectById(2); // 查找数据库-通过ID // 读去文件 FileInputStream fileInputStream = new FileInputStream(p.getImages()); // 读取我们查到的路径 FileOutputStream fileOutputStream = new FileOutputStream("E:/加速器/4.jpg"); // 设置我们下载的路径 byte[] bytes = new byte[20]; // 设置缓存区 int count=0; while ((count=fileInputStream.read(bytes))!=-1){ fileOutputStream.write(bytes,0,count); // 文件下载到哪里? } fileInputStream.close(); // 关闭资源 fileOutputStream.close(); }