-
查詢BLOB類型數據
-
定義一個字節數組接收
比如說可以定義一個接收的實體類
@Data public class KnowInfoDto { /** * Id */ private String id; /** * 內容 */ private String legalContent; /** * 內容byte */ private byte[] legalContentByte; }
-
再到xml里寫一個resultMap
<resultMap id="queryBaseResultMap" type="dto.KnowInfoDto" > <id column="id" property="id" jdbcType="VARCHAR" /> <result column="legalContentByte" property="legalContentByte" jdbcType="BLOB" typeHandler="org.apache.ibatis.type.BlobTypeHandler"/> </resultMap>
需要注意的就是這個typeHandler ,要是BlobTypeHandler類型的。之后用這個byte[]z字段去接收。
-
在轉化為String類型
接收到之后需要轉換成String,可能會亂碼。所以我們需要判斷編碼類型,默認為utf-8.
/** * 獲取文件編碼類型 * * @param bytes 文件bytes數組 * @return 編碼類型 */ public static String getEncoding(byte[] bytes) { String defaultEncoding = "UTF-8"; UniversalDetector detector = new UniversalDetector(null); detector.handleData(bytes, 0, bytes.length); detector.dataEnd(); String encoding = detector.getDetectedCharset(); detector.reset(); if (encoding == null) { encoding = defaultEncoding; } return encoding; }
再用new String()z轉成String。
try { KnowInfoDto.setLegalContent(new String(KnowInfoDto.getLegalContentByte(),getEncoding(KnowInfoDto.getLegalContentByte()))); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }
-
-
-
查詢CLOB類型數據
-
實體類中用String接收就可以了
@Data public class KnowInfoDto { /** * Id */ private String id; /** * 內容 */ private String legalContent; }
-
xml 里要設置一下typeHandler
<resultMap id="queryBaseResultMap" type="dto.KnowInfoDto" > <id column="id" property="id" jdbcType="VARCHAR" /> <result column="legalContent" property="legalContent" jdbcType="CLOB" typeHandler="org.apache.ibatis.type.ClobTypeHandler"/> </resultMap>
-