亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Mysql存儲(chǔ)二進(jìn)制對(duì)象數(shù)據(jù)問題

 更新時(shí)間:2023年03月14日 16:32:45   作者:碼農(nóng)的誕生  
這篇文章主要介紹了Mysql存儲(chǔ)二進(jìn)制對(duì)象數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Mysql存儲(chǔ)二進(jìn)制對(duì)象數(shù)據(jù)

首先數(shù)據(jù)庫存儲(chǔ)一個(gè)Object對(duì)象

需要在數(shù)據(jù)庫表中定義一個(gè)blob類型的字段

與數(shù)據(jù)庫對(duì)應(yīng)的實(shí)體類

編寫一個(gè)操作二進(jìn)制的工具類

import java.io.*;
 
/**
 * byte[]類型操作類
 */
public class BlobUtil {
 
    /**
     * 把object對(duì)象序列化為二進(jìn)制字節(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();
    }
 
    /**
     * 把二進(jìn)制字節(jié)數(shù)組反序列化為object對(duì)象
     * object當(dāng)中的每個(gè)javaBean對(duì)象都必須實(shí)現(xiàn)序列化
	 * 最外層的類必須生成一個(gè)序列化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[]的對(duì)象必須實(shí)現(xiàn)序列化和生成一個(gè)序列化ID,生成一個(gè)序列化ID是為了解決類當(dāng)中一但有修改,反序列化時(shí)序列化ID就會(huì)對(duì)應(yīng)不上,如下圖:

類中如果有其他類為變量也需要實(shí)現(xiàn)序列化,否則從數(shù)據(jù)庫中取出數(shù)據(jù)然后反序列化成Object對(duì)象的時(shí)候會(huì)直接報(bào)錯(cuò)

對(duì)user的數(shù)據(jù)進(jìn)行操作

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)用二進(jìn)制工具類來轉(zhuǎn)換數(shù)據(jù)然后進(jìn)行存取就可以了。

測(cè)試功能

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("=====對(duì)象轉(zhuǎn)換成blob類型數(shù)據(jù)====="+Arrays.toString(dataValue));
        UserVO userVO = (UserVO) BlobUtil.getObject(dataValue);
        System.out.println("=====blob類型數(shù)據(jù)轉(zhuǎn)換成對(duì)象====="+ JSONObject.toJSONString(userVO));
    }
}

打印輸出

=====對(duì)象轉(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)換成對(duì)象====={"userId":"123456","userName":"張三"}

Mysql存儲(chǔ)二進(jìn)制大型對(duì)象類型對(duì)照

MySql MediumBlob——MySql的Bolb四種類型

MySQL中,BLOB是一個(gè)二進(jìn)制大型對(duì)象,是一個(gè)可以存儲(chǔ)大量數(shù)據(jù)的容器,它能容納不同大小的數(shù)據(jù)。

BLOB類型實(shí)際是個(gè)類型系列(TinyBlob、Blob、MediumBlob、LongBlob),除了在存儲(chǔ)的最大信息量上不同外,他們是等同的。

在這里插入圖片描述

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論