美文网首页
Mybatis查询Blob类型数据转为String

Mybatis查询Blob类型数据转为String

作者: wuli见见 | 来源:发表于2018-07-19 20:54 被阅读0次

场景: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>标签直接显示了

相关文章

网友评论

      本文标题:Mybatis查询Blob类型数据转为String

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