登录
首页 >  数据库 >  MySQL

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

来源:SegmentFault

时间:2023-01-11 16:44:13 184浏览 收藏

知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个数据库开发实战,手把手教大家学习《个人精简xml,实现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个人精简xml,实现mybatis存取blob类型数据(Mysql) imgBeans) throws Exception{
        return R.ok("result",uploadFileService.uploadImg(imgBeans));
    }

    @RequestMapping(value="/getTp", method = {RequestMethod.POST })
    @ResponseBody
    public R getTp(String tyshxym){
        List个人精简xml,实现mybatis存取blob类型数据(Mysql) 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个人精简xml,实现mybatis存取blob类型数据(Mysql) imgBeans) {
        int flag = 1;
        int insert = 1;
        for (int i = 0; i  getTp(String tyshxym) {
        List个人精简xml,实现mybatis存取blob类型数据(Mysql) 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个人精简xml,实现mybatis存取blob类型数据(Mysql) {

    @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个人精简xml,实现mybatis存取blob类型数据(Mysql) 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;
    }
}

关注公众号:Java架构师联盟,每日更新技术好文

文中关于mysql的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《个人精简xml,实现mybatis存取blob类型数据(Mysql)》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>