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 imgBeans) throws Exception{
        return R.ok("result",uploadFileService.uploadImg(imgBeans));
    }

    @RequestMapping(value="/getTp", method = {RequestMethod.POST })
    @ResponseBody
    public R getTp(String tyshxym){
        List 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 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 getTp(String tyshxym) {
        List 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 {

    @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 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;
    }
}

你可能感兴趣的:(ssm,blob)