Spring boot2.x 整合mybatis图片上传

一、pom 文件引入依赖引入依赖
   <!-- mysql -->
   <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
   </dependency>
   <!-- jdbc -->
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
   </dependency>
   <!-- mybatis -->
   <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.1.0</version>
   </dependency>
 

二、配置application.properties

#图片保存地址
file.uploadDir=D://Program Files/Apache Software Foundation/Tomcat 9.0/webapps/
#图片保存路径
file.uploadFolder=upload/
#配置调用图片地址,需要根据自己本地ip修改
host.url=http://192.168.0.105:8080

#链接数据库
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/kol?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
# 输出sql 语句 方便调试
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

三、定义数据库表和实体类

 

package com.kol.model;

public class Image {
    private int id;
    private String path;
    private String type;
    private String filename;
    private String date;
    private String host;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Image(int id, String path, String host, String type, String filename, String date) {
        this.id = id;
        this.path = path;
        this.host=host;
        this.type = type;
        this.filename = filename;
        this.date = date;
    }
}
四、定义map
package com.kol.service.impl;
import com.kol.model.Image;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;


@Mapper
@Component
public interface ImagerMaper {

    @Select("select * from images where id = #{id}")
    Image findByID();

    @Insert("insert into images(type,path,host,date) values (#{type},#{path},#{host},#{date})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    int insert(Image image);
}

五、定义对外接口 controller

package com.kol.controller;

import com.kol.domain.FileController;
import com.kol.model.Image;
import com.kol.service.impl.ImagerMaper;
import com.kol.utils.PathManager;
import com.kol.msg.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
public class UploadFileController {
    @Autowired
    FileController fileController;

    @Autowired
    ImagerMaper imagerMaper;
    /**
     * 单文件上传
     * @param multipartFile 上传文件参数
     * @return
     */
    @PostMapping("uploadFile")
    public Msg uploadFile(@RequestParam("head_img") MultipartFile multipartFile){
        Msg msg=fileController.SaveFile(multipartFile);
        if("1".equals(msg.getCode()))
        {
            imagerMaper.insert(new Image(1,msg.getSavePath().toString(),msg.getData().toString(),"1","aaa","1111"));
        }
        return  msg;
    }

    /**
     * 多文件上传
     * @param multipartFiles 文件列表参数
     * @return
     */
    @RequestMapping("uploadFiles")
    public List<Msg>  uploadFiles(@RequestParam("files") MultipartFile []multipartFiles){
        List<Msg> list =fileController.SaveFiles(multipartFiles);
        return  list;
    }
}

六、测试html页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form enctype="multipart/form-data" method="post" action="/uploadFile">
        请选择文件<input type="file" name="head_img">
        <input type="submit" value="上传">
    </form>
</body>
</html>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值