MyBatis用String处理blob

2020-08-08  本文已影响0人  kingbo1587
package com.wh.starboot.model;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.io.ByteArrayInputStream;
import java.sql.*;

/**
 * handle Blob as String
 * <p>
 * Created by kingbo on 2016/11/24.
 */
public class StringBlobTypeHandler extends BaseTypeHandler<String> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
        byte[] bytes = parameter.getBytes();
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ps.setBinaryStream(i, bis, bytes.length);
    }

    @Override
    public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
        Blob blob = rs.getBlob(columnName);
        String returnValue = null;
        if (null != blob) {
            returnValue = new String(blob.getBytes(1L, (int) blob.length()));
        }
        return returnValue;
    }

    @Override
    public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        Blob blob = rs.getBlob(columnIndex);
        String returnValue = null;
        if (null != blob) {
            returnValue = new String(blob.getBytes(1L, (int) blob.length()));
        }
        return returnValue;
    }

    @Override
    public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        Blob blob = cs.getBlob(columnIndex);
        String returnValue = null;
        if (null != blob) {
            returnValue = new String(blob.getBytes(1L, (int) blob.length()));
        }
        return returnValue;
    }

}
上一篇下一篇

猜你喜欢

热点阅读