Mysql存儲二進制對象數(shù)據(jù)問題
Mysql存儲二進制對象數(shù)據(jù)
首先數(shù)據(jù)庫存儲一個Object對象
需要在數(shù)據(jù)庫表中定義一個blob類型的字段
與數(shù)據(jù)庫對應的實體類
編寫一個操作二進制的工具類
import java.io.*; /** * byte[]類型操作類 */ public class BlobUtil { /** * 把object對象序列化為二進制字節(jié)數(shù)組 * @param object * @return */ public static byte[] setObject(Object object) { ByteArrayOutputStream baos = null; ObjectOutputStream out = null; try { baos = new ByteArrayOutputStream(); out = new ObjectOutputStream(baos); out.writeObject(object); } catch (Exception e) { e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } if (baos != null) { try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } } return baos.toByteArray(); } /** * 把二進制字節(jié)數(shù)組反序列化為object對象 * object當中的每個javaBean對象都必須實現(xiàn)序列化 * 最外層的類必須生成一個序列化ID * @param bytes * @return */ public static Object getObject(byte[] bytes) { Object obj = null; ByteArrayInputStream bais = null; ObjectInputStream in = null; try { bais = new ByteArrayInputStream(bytes); in = new ObjectInputStream(bais); obj = in.readObject(); } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (bais != null) { try { bais.close(); } catch (IOException e) { e.printStackTrace(); } } } return obj; } }
需要轉(zhuǎn)換為byte[]的對象必須實現(xiàn)序列化和生成一個序列化ID,生成一個序列化ID是為了解決類當中一但有修改,反序列化時序列化ID就會對應不上,如下圖:
類中如果有其他類為變量也需要實現(xiàn)序列化,否則從數(shù)據(jù)庫中取出數(shù)據(jù)然后反序列化成Object對象的時候會直接報錯
對user的數(shù)據(jù)進行操作
import java.io.Serializable; /** * @author * @description * @date */ public class UserVO implements Serializable { private static final long serialVersionUID = 1L; private String userId; private String userName; private String password; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
調(diào)用二進制工具類來轉(zhuǎn)換數(shù)據(jù)然后進行存取就可以了。
測試功能
public class TestMain { public static void main(String[] args) { UserVO user = new UserVO(); user.setUserId("123456"); user.setUserName("張三"); byte[] dataValue = BlobUtil.setObject(user); System.out.println("=====對象轉(zhuǎn)換成blob類型數(shù)據(jù)====="+Arrays.toString(dataValue)); UserVO userVO = (UserVO) BlobUtil.getObject(dataValue); System.out.println("=====blob類型數(shù)據(jù)轉(zhuǎn)換成對象====="+ JSONObject.toJSONString(userVO)); } }
打印輸出
=====對象轉(zhuǎn)換成blob類型數(shù)據(jù)=====[-84, -19, 0, 5, 115, 114, 0, 53, 99, 111, 109, 46, 100, 111, 108, 112, 104, 105, 110, 46, 109, 111, 100, 117, 108, 101, 115, 46, 116, 109, 115, 46, 100, 111, 109, 97, 105, 110, 46, 99, 117, 115, 116, 111, 109, 101, 114, 115, 101, 114, 118, 105, 99, 101, 46, 85, 115, 101, 114, 86, 79, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 3, 76, 0, 8, 112, 97, 115, 115, 119, 111, 114, 100, 116, 0, 18, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 59, 76, 0, 6, 117, 115, 101, 114, 73, 100, 113, 0, 126, 0, 1, 76, 0, 8, 117, 115, 101, 114, 78, 97, 109, 101, 113, 0, 126, 0, 1, 120, 112, 112, 116, 0, 6, 49, 50, 51, 52, 53, 54, 116, 0, 6, -27, -68, -96, -28, -72, -119]
=====blob類型數(shù)據(jù)轉(zhuǎn)換成對象====={"userId":"123456","userName":"張三"}
Mysql存儲二進制大型對象類型對照
MySql MediumBlob——MySql的Bolb四種類型
MySQL中,BLOB是一個二進制大型對象,是一個可以存儲大量數(shù)據(jù)的容器,它能容納不同大小的數(shù)據(jù)。
BLOB類型實際是個類型系列(TinyBlob、Blob、MediumBlob、LongBlob),除了在存儲的最大信息量上不同外,他們是等同的。
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
MYSQL數(shù)據(jù)庫使用UTF-8中文編碼亂碼的解決辦法
這篇文章主要介紹了MYSQL數(shù)據(jù)庫使用UTF-8中文編碼亂碼的解決辦法,需要的朋友可以參考下2015-10-10MySQL報錯1118,數(shù)據(jù)類型長度過長問題及解決
在使用MySQL過程中,常見的一個問題是報錯1118,這通常發(fā)生在創(chuàng)建表時,錯誤提示為“Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual2024-10-10mysql允許外網(wǎng)訪問以及修改mysql賬號密碼實操方法
這篇文章主要介紹了mysql允許外網(wǎng)訪問以及修改mysql賬號密碼實操方法,有需要的朋友們可以參考學習下。2019-08-08