問題:在mysql中存儲base64,由於太長,基本就是幾百K,因此用longBlobjava
描述:在mysql中,LongBlob、blob算是二進制流文件了,因此用普通的數據格式是不行的,這裏用TypeHandler解決,有其餘以爲方案歡迎在下方留言mysql
解決:sql
Handler代碼apache
import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; import java.sql.*; public class MyBlobTypeHandler extends BaseTypeHandler<String> { // 指定字符集 private static final String DEFAULT_CHARSET = "utf-8"; @Override public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { ByteArrayInputStream bis; try { // 把String轉化成byte流 bis = new ByteArrayInputStream(parameter.getBytes(DEFAULT_CHARSET)); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Blob Encoding Error!"); } ps.setBinaryStream(i, bis, parameter.length()); } @Override public String getNullableResult(ResultSet rs, String columnName) throws SQLException { return getResult(rs.getBlob(columnName)); } @Override public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { return getResult(cs.getBlob(columnIndex)); } @Override public String getNullableResult(ResultSet rs, int columnName) throws SQLException { return getResult(rs.getBlob(columnName)); } private String getResult(Blob blob) throws SQLException { byte[] returnValue = null; if (null != blob) { returnValue = blob.getBytes(1, (int) blob.length()); } try { // 把byte轉化成string if (null != returnValue) { return new String(returnValue, DEFAULT_CHARSET); } } catch (UnsupportedEncodingException e) { throw new RuntimeException("Blob Encoding Error!"); } return null; } }
mybatis XML代碼mybatis
<result property="signature" column="signature" typeHandler="com.kenary.config.MyBlobTypeHandler"/>