mybatis存取blob类型数据(Mysql)

大纲

  相比于网上的其他教程,我觉得我的xml文件是相对简单的。不信看我的Mapper。总而言之,存成BLOB时用Byte[]。从数据库中取出来用String接收就可以了。和其他人分析的不一样。但是我这样成功了。

Controller

package com.qust.shbz.util.controller;

import com.qust.shbz.util.ImgBean;
import com.qust.shbz.util.R;
import com.qust.shbz.util.service.UploadFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/upload")
public class UploadFile {

    @Autowired
    private UploadFileService uploadFileService;

    @RequestMapping(value="/upload", method = {RequestMethod.POST })
    @ResponseBody
    public R upload(@RequestBody List<ImgBean> imgBeans) throws Exception{
        return R.ok("result",uploadFileService.uploadImg(imgBeans));
    }

    @RequestMapping(value="/getTp", method = {RequestMethod.POST })
    @ResponseBody
    public R getTp(String tyshxym){
        List<ImgBean> tp = uploadFileService.getTp(tyshxym);

        return R.ok("result",tp);
    }


}

Service

package com.qust.shbz.util.service.impl;

import com.qust.shbz.util.ImgBean;
import com.qust.shbz.util.service.UploadFileService;
import com.qust.shbz.util.mapper.UploadFileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: UploadFileServiceImpl
 * @Description:  上传文件的业务层
 * @Author: ZhaoHualuo
 * @Date: 2019/6/9 22:36
 * @Version: 1.0
 */
@Service
public class UploadFileServiceImpl implements UploadFileService {

    @Autowired
    private UploadFileMapper uploadFileMapper;

    @Override
    public int uploadImg(List<ImgBean> imgBeans) {
        int flag = 1;
        int insert = 1;
        for (int i = 0; i < imgBeans.size(); i++) {
            insert = uploadFileMapper.insertImg(imgBeans.get(i));
            if (insert != 1) {
                flag = 0;
            }
        }
        return flag;
    }

    @Override
    public List<ImgBean> getTp(String tyshxym) {
        List<ImgBean> tp = uploadFileMapper.getTp(tyshxym);
        return tp;
    }
}

Mapper

package com.qust.shbz.util.mapper;

import com.github.abel533.mapper.Mapper;
import com.qust.shbz.util.ImgBean;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @ClassName: UploadFileMapper
 * @Description:  文件上传数据库操作
 * @Author: ZhaoHualuo
 * @Date: 2019/6/9 22:38
 * @Version: 1.0
 */
public interface UploadFileMapper extends Mapper<ImgBean> {

    @Insert({"insert into img(id,name,base64) values(#{id},#{name},#{base64Byte})"})
    public int insertImg(ImgBean imgBean);

    @Select({"select * from img where id = #{tyshxym}"})
    public List<ImgBean> getTp(String tyshxym);
}

Bean

package com.qust.shbz.util;

import java.io.UnsupportedEncodingException;
import java.sql.Blob;

/**
 * @ClassName: ImgBean
 * @Description:  图片上传实体类
 * @Author: ZhaoHualuo
 * @Date: 2019/6/9 21:56
 * @Version: 1.0
 */
public class ImgBean {
    //主键,自动递增
    private int key;

    //文件名
    private String name;

    //公司标志,图片属于该公司
    private String id;

    //图片转换的base64字符,取的时候用这个
    private String base64;

    //byte[]形式的base64格式,BLOB,存的时候用这个
    private byte[] base64Byte;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

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

    public int getKey() {
        return key;
    }

    public void setKey(int key) {
        this.key = key;
    }

    public String getBase64() {
        return base64;
    }

    public void setBase64(String base64) throws UnsupportedEncodingException {
        this.base64 = base64;
        this.base64Byte = base64.getBytes("UTF8");
    }

    public byte[] getBase64Byte() {
        return base64Byte;
    }

    public void setBase64Byte(byte[] base64Byte) {
        this.base64Byte = base64Byte;
    }
}
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值