mybatis存取blob類型數據(Mysql)

大綱

  相比於網上的其餘教程,我以爲個人xml文件是相對簡單的。不信看個人Mapper。總而言之,存成BLOB時用Byte[]。從數據庫中取出來用String接收就能夠了。和其餘人分析的不同。可是我這樣成功了。java

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

2git

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

4github

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


本文分享自微信公衆號 - 小遷不禿頭(LQQ016076)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。web

相關文章
相關標籤/搜索