springboot上传文件写入数据库

springboot上传文件写入数据库,第1张

首先导入了相应的jar包

<!--thymeleaf--><dependency>

<groupId>org.thymeleaf</groupId>

<artifactId>thymeleaf-spring5</artifactId></dependency><dependency>

<groupId>org.thymeleaf.extras</groupId>

<artifactId>thymeleaf-extras-java8time</artifactId></dependency><!--数据库连接--><dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId></dependency><!--mybatis整合springboot--><dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>2.1.2</version></dependency><!--druid数据源--><dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid</artifactId>

<version>1.1.22</version></dependency>1234567891011121314151617181920212223242526272829

对数据源,mybatis,和上传文件进行配置

spring:

datasource:

type: com.alibaba.druid.pool.DruidDataSourceusername: rootpassword: shw123zxcurl: jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTCdriver-class-name: com.mysql.cj.jdbc.Driver servlet:

multipart:

max-request-size: 10MB #上传文件的最大总大小

max-file-size: 10MB #上传单个文件的最大大小mybatis:

type-aliases-package: cn.codewei.pojo mapper-locations: classpath:/mapper/*.xml1234567891011121314

然后写一个文件上传的html,注意表单的==enctype属性要设置为multipart/form-data==

<!DOCTYPE html><html lang="en"><head>

<meta charset="UTF-8">

<title>Title</title></head><body>

<h1>文件上传</h1>

<form method="post" enctype="multipart/form-data" action="/upload">闷肢穗

<input type="file" name="file"饥举>

<input type="submit" value="上传">

</form></body></蚂卜html>1234567891011121314

然后写一个Mapper和对应的Mapper.xml和service

@Mapper@Repositorypublic interface PhotoMapper {

// 向数据库中添加图片

public int addPhoto(Photo photo)

// 从数据库中取出图片

public Photo getPhotoById(@Param("id") int id)}123456789

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="cn.codewei.mapper.PhotoMapper">

<insert id="addPhoto" parameterType="photo">

insert into image values (#{id},#{photo},#{photo_name},#{photo_type})</insert>

<select id="getPhotoById" resultType="photo">

select * from image where id=#{id}</select></mapper>123456789101112

在Controller中进行调用

上传

@Autowiredprivate PhotoService photoService@Autowiredprivate Photo photo@PostMapping("/upload")@ResponseBodypublic String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {

byte[] bytes = file.getBytes()

photo.setPhoto(bytes)

photo.setPhoto_name(file.getOriginalFilename())

photo.setPhoto_type(".jpg")

photoService.addPhoto(photo)

return "上传成功!"}123456789101112131415161718

取出,在页面中显示

@RequestMapping("/getPhoto")public String getImage(HttpServletResponse response) throws IOException {

Photo photo = photoService.getPhotoById(1)

byte[] photo1 = photo.getPhoto()

ServletOutputStream os = response.getOutputStream()

os.write(photo1)

os.close()

return ""}123456789

<!DOCTYPE html><html lang="en"><head>

<meta charset="UTF-8">

<title>Title</title></head><body>

<h1>首页</h1>

<img src="/getPhoto" width="200px" height="200px"></body></html>

MyBatis中,巧正可以使用Generator自动生成代码,包括DAO层、 MODEL层 、MAPPING SQL映射文件。

第一步:下载MyBatis的Generator工具

htt p:/ /mybati s.gith ub.i o/generator/

第二步:配置自动生成代码所需的XML配置文件,例如(generator.xml)

将这个文件保存至你下载的mybatis-generator-core-1.3.2文件夹下

第三步:进入XML配置文件(generator.xml)所在的的目录并执行命令:

Dos代码

java -jar E:\mybatis-generator-core-1.3.2\lib\mybatis-generator-core-1.3.2.j ar -configfile generator.xml -overwrite

mybatis generator eclipse插件的安装

打开eclipse,点击Help>Software Update

选择 "Available Software" 标签,点击 "Add Site" 按钮

输入以下信息:

Location:htt p:/ /mybatis.googleco de.c om/svn/sub-projects/gen erator/trunk/eclipse/UpdateSite/

点击ok,自动进入"mybatis generator Feature"

点击逗install地按钮进行安装。。。。mybatis generator 插件安装完成

配置Mybatis Generator不要生成Example类

Mybatis Generator默认设置会生成一大堆罗哩罗嗦的Example类,主要是用各种不同的条件来 *** 作数据库,大部困咐分是用不到的,用到的时候手工修改mapper和接口文件就行了。

<</code>table

schema="general"

tableName="tb_table_name"

domainObjectName="EntityName"

enableCountByExample="false"

enableUpdateByExample="false"

enableDeleteByExample="false"汪宽纯

enableSelectByExample="false"

selectByExampleQueryId="false"

>

name="useActualColumnNames"

value="true"/>

</</code>table>

这样生成的mapper和dao接口就清爽多了。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12208622.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存