美文网首页
MyBatis用String处理blob

MyBatis用String处理blob

作者: kingbo1587 | 来源:发表于2020-08-08 15:03 被阅读0次
    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;
        }
    
    }
    

    相关文章

      网友评论

          本文标题:MyBatis用String处理blob

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