將對象存爲數據庫二進制數據的處理方法

Oracle數據庫對應字段BLOB類型,實體對應字節數組類型(byte[]、@Type(type="org.springframework.orm.hibernate3.support.BlobByteArrayType"))。java

1、存spring

一、把數據列表(List<Object>),放入Map<String,Object>中;sql

二、將Map對象轉換爲字節數據set到實體中數據庫

2、取數組

一、從實體中取出字節數組轉化對象,而後強制轉化爲Map類型,用get(Object key)拿到存儲在Map中的對象,最後轉化爲List就 Ok啦。。。spa

entity:
@Column(name = "JOB_DATA")
    @Type(type="org.springframework.orm.hibernate3.support.BlobByteArrayType")
    private byte[] jobData;

save:

List<AcmsInternetgatewaydevice> jobs = CollectUtils.newArrayList();
        if (StringUtils.isNotEmpty(deviceIds)) {
            jobs = acmsInternetgatewaydeviceService.getAcmsInternetgatewaydevicesByDeviceIds(StrUtils.splitToLong(deviceIds));
        }
        Map<String, Object> jobNes = new HashMap<String, Object>();
        jobNes.put(JobUtils.NE_KEY, jobs);
        entity.setJobData(Bytes.objct2Bytes(jobNes));


get:

List<AcmsInternetgatewaydevice> selectNes = null;
        if(jobData!=null)
            selectNes = (List<AcmsInternetgatewaydevice>) ((Map<String,Object>)Bytes.bytes2Objct(jobData)).get(JobUtils.NE_KEY);

JobUtils:
public class JobUtils {
    public static final String QUARTS_SCHEDULER_KEY = "quartzScheduler";
    
    public static final String NE_KEY = "JOB_NES";
}


Bytes:
package com.gloryscience.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Blob;
import java.sql.SQLException;

public class Bytes {
    /** 
     * 因爲String.subString對漢字處理存在問題(把一個漢字視爲一個字節),所以在 包含漢字的字符串時存在隱患,現調整以下: 
     *  
     * @param src 要截取的字符串 
     * @param start_idx 開始座標(包括該座標) 
     * @param end_idx 截止座標(包括該座標) 
     * @return 
     */  
    public static String substring(String src, int start_idx, int end_idx) {  
        byte[] b = src.getBytes();  
        String tgt = "";  
        for (int i = start_idx; i <= end_idx; i++) {  
            tgt += (char) b[i];  
        }  
        return tgt;  
    }  

    public static byte[] objct2Bytes(java.lang.Object obj) {
        if(obj == null){
            return null;
        }else{
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            try {
                ObjectOutputStream ops = new ObjectOutputStream(
                        byteArrayOutputStream);
                ops.writeObject(obj);
                byte[] byteA = byteArrayOutputStream.toByteArray();
                return byteA;
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
        return null;
    }

    public static java.lang.Object bytes2Objct(byte[] byteA) {
        InputStream in = new ByteArrayInputStream(byteA);
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            return ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static java.lang.Object blobToObjct(Blob blob) {
        ObjectInputStream ois = null;
        try {
            InputStream in = blob.getBinaryStream();
            ois = new ObjectInputStream(in);
            return ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    
}
相關文章
相關標籤/搜索