个人精简xml,实现mybatis存取blob类型数据(Mysq

作者: java架构师联盟 | 来源:发表于2020-10-16 14:37 被阅读0次

大纲

  相比于网上的其他教程,我觉得我的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: bws

* @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:  bws

* @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:  bws

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

   }

}

觉得不错的朋友帮忙点赞文章,关注我之后直接前往Github地址:传送门 即可查看我的资源库哦!

相关文章

网友评论

    本文标题:个人精简xml,实现mybatis存取blob类型数据(Mysq

    本文链接:https://www.haomeiwen.com/subject/jxpzpktx.html