1.用Map查询单条数据。

<select id="" resultType="java.util.Map">
    sql语句
</select>

2.将数据强转为blob对象

BLOB blob= (BLOB) map.get("数据列名"); 

3.将blob对象转成byte[]

private byte[] blobToBytes(BLOB blob) {
    BufferedInputStream is = null;
    try {
        is = new BufferedInputStream(blob.getBinaryStream());
        byte[] bytes = new byte[(int) blob.length()];
        int len = bytes.length;
        int offset = 0;
        int read = 0;

        while (offset < len && (read = is.read(bytes, offset, len - offset)) >= 0) {
            offset += read;
        }
        return bytes;
    } catch (Exception e) {
        return null;
    } finally {
        try {
            is.close();
            is = null;
        } catch (IOException e) {
            return null;
        }
    }
}

4.将byte[] 对象转成String

byte[] result = blobToBytes(blob);
String swe=new String(result);
Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐