https://blog.csdn.net/q3dxdx/article/details/51014357
1.简介:
BLOB,二进制大对象(字节流)。可以用来存储图片,声音和视频等二进制文件。没有字符集的说法。
TEXT,文本大对象(字符流)。可以用来存储大量的字符串,可以理解为超大的char或者varchar类型。由于是存储字符,所以有字符集的说法。
并且blob和text类型是无法设置默认值的。
2.存储限制:
tinyblob,tinytext,最大存储限制为28-1=255字节;
blob,text,最大存储限制为 216-1=64k-1字节;
mediumblob,mediumtext,最大存储限制为 224-1=16M-1字节;
longblob,longtext,最大存储限制为232-1=4G-1字节。
那么后台又如何使用呢。
3.后台代码
写个转换器,然后mybatis用此类把blob转了字符串。javaBean用String接收
package com.bootdo.seal.utils;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.sql.*;
/**
* Created by jq on 2019/2/21.
*/
public class MyBlobTypeHandler extends BaseTypeHandler<String> {
private static final Log LOGGER = LogFactory
.getLog(MyBlobTypeHandler.class);
// 指定字符集
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 {
Blob blob = rs.getBlob(columnName);
byte[] returnValue = null;
String result = null;
if (null != blob) {
returnValue = blob.getBytes(1, (int) blob.length());
}
try {
if (null != returnValue) {
// 把byte转化成string
result = new String(returnValue, DEFAULT_CHARSET);
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Blob Encoding Error!");
}
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(1, (int) blob.length());
}
try {
if (null != returnValue) {
result = new String(returnValue, DEFAULT_CHARSET);
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Blob Encoding Error!");
}
return result;
}
/**
* @param arg0
* @param arg1
* @return
* @throws SQLException
* @Description:
* @see org.apache.ibatis.type.BaseTypeHandler#getNullableResult(java.sql.ResultSet,
* int)
*/
@Override
public String getNullableResult(ResultSet rs, int columnName)
throws SQLException {
LOGGER.debug("enter function");
String result = null;
Blob blob = rs.getBlob(columnName);
byte[] returnValue = null;
if (null != blob) {
returnValue = blob.getBytes(1, (int) blob.length());
}
try {
// 把byte转化成string
if (null != returnValue) {
result = new String(returnValue, DEFAULT_CHARSET);
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Blob Encoding Error!");
}
LOGGER.debug("exit function");
return result;
}
}
<resultMap id="resultMap" type="com.bootdo.xx">
<id property="id" column="id"/>
<result property="returnImg" column="return_img" typeHandler="com.bootdo.seal.utils.MyBlobTypeHandler"/>
</resultMap>