Mybatis查询Blob类型数据转为String

场景:Java将图标以Base64格式存储到数据库,字段使用Blob类型,然后通过Mybatis查询出来,直接在页面上显示图片

优点:因为使用的是小图标,1K左右的大小,所以直接以Base64格式放在数据库中,速度快,不用再去服务器上读取。

Blob类型的数据,通过Mybatis查询回来不能在页面显示,而且查询回来的数据也不是数据库中的数据,需要把Blob类型数据转换成String类型,能直接通过<img>标签显示

Mybatis查询的时候需要用到类型转换typeHandler

自定义BlobTypeToStringHandler 继承 org.apache.ibatis.type.BaseTypeHandler

在resultMap中使用:

<resultMap id="TestResult" type="java.util.HashMap">

    <result property="icon"  column="rec_icon"  typeHandler="包.BlobTypeToStringHandler ">

</resultMap>

自定义BlobTypeToStringHandler类:

import java.io.ByteArrayInputStream;

import java.io.UnsupportedEncodingException;

import java.sql.Blob;

import java.sql.CallableStatement;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;

import org.apache.ibatis.type.JdbcType;

/***@Author jianjian 

*@Date 2018年7月19日 

*@Time 下午5:59:22 

*@ClassName BlobTypeToStringHandler.java 

*/

public class BlobTypeToStringHandler extends BaseTypeHandler{

    //指定字符集

    private static final String DEFAULT_CHARSET = "utf-8";

    @Override

    public void setNonNullParameter(PreparedStatement ps,

                int i, String parameter, JdbcType paramJdbcType)

                throws SQLException {

    try {

            ByteArrayInputStream bis = new ByteArrayInputStream(parameter.getBytes(DEFAULT_CHARSET));

            ps.setBinaryStream(i, bis, parameter.length());

    } catch (UnsupportedEncodingException e) {

            e.printStackTrace();

    }

    }

    @Override

    public String getNullableResult(ResultSet rs, String columnName)

                throws SQLException {

    Blob blob = rs.getBlob(columnName);

    byte[] returnValue = null;

    String result = null;

    if (null != blob) {

        returnValue = blob.getBytes(1L, (int)blob.length());

    }

    try {

        if (returnValue != null) {

            result = new String(returnValue, DEFAULT_CHARSET);

        }

    } catch (UnsupportedEncodingException e) {

        e.printStackTrace();

    }

    return result;

    }

    @Override

    public String getNullableResult(ResultSet rs, int columnIndex)

                throws SQLException {

    Blob blob = rs.getBlob(columnIndex);

    byte[] returnValue = null;

    String result = null;

    if (null != blob) {

        returnValue = blob.getBytes(1L, (int)blob.length());

    }

    try {

        if (returnValue != null) {

            result = new String(returnValue, DEFAULT_CHARSET);

        }

    } catch (UnsupportedEncodingException e) {

        e.printStackTrace();

    }

    return result;

    }

    @Override

    public String getNullableResult(CallableStatement cs,

            int columnIndex) throws SQLException {

    Blob blob = cs.getBlob(columnIndex);

    byte[] returnValue = null;

    String result = null;

    if (null != blob) {

        returnValue = blob.getBytes(1L, (int)blob.length());

    }

    try {

        if (returnValue != null) {

            result = new String(returnValue, DEFAULT_CHARSET);

        }

    } catch (UnsupportedEncodingException e) {

        e.printStackTrace();

    }

    return result;

    }

}

然后查询回来的Blob数据能使用<img>标签直接显示了

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一. Java基础部分.................................................
    wy_sure阅读 3,840评论 0 11
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,780评论 18 399
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • 前言 最先接触编程的知识是在大学里面,大学里面学了一些基础的知识,c语言,java语言,单片机的汇编语言等;大学毕...
    oceanfive阅读 3,167评论 0 7
  • 2007年12月,我的第一个孩子出生,取名轩轩,一眨眼轩轩今年十岁了。2015年7月,老二YY出生,今年刚满两岁多...
    去K书银姐阅读 535评论 0 0