hibernate存儲blob
① hibernate如何保存blob數據
首先你得搞清楚一點BLOB是二進制大對象,是ORACLE的數據類型,它對應到java中有兩種方式:
byte[] 和java.sql.Blob(先搞清楚這重點哦)
我給你直接復制重點代碼,希望可以幫到你
1 資料庫中定義成BLOB類型,這個你自己定義吧(表名叫bigobject),~~~~~不過給你截個圖吧
2 綜上,把oracle資料庫中的BLOB映射到java中有兩種情況的,即java.sql.Blob和byte[],下面先說byte[]的映射
++++++++++++++++++++++++Bigobject.hbm.xml映射文件+++++++++++++++++++++++
<hibernate-mapping>
<class name="entity.Bigobject" table="BIGOBJECT" >
<id name="id" type="java.lang.Integer">
<column name="ID" precision="6" scale="0" />
<generator class="native" />
</id>
<property name="tclob" type="java.lang.String">
<column name="TCLOB" />
</property>
<property name="tblob" type="byte[]"> //!!!!!注意,這里是byte[]
<column name="TBLOB" />
</property>
</class>
</hibernate-mapping>
++++++++++++++++++++以下是bigobject實體類(用hibernate映射的)+++++++++++
public class Bigobject implements java.io.Serializable {
// Fields
private Integer id;
private String tclob;
private byte[] tblob; //!!!!!注意,這里是byte[]
// Constructors
/** default constructor */
public Bigobject() {
}
/** full constructor */
public Bigobject(String tclob, byte[] tblob) {
this.tclob = tclob;
this.tblob = tblob;
}
御謹 // Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
鎮冊基 this.id = id;
}
public String getTclob() {
return this.tclob;
}
public void setTclob(String tclob) {
this.tclob = tclob;
}
public byte[] getTblob() {
return this.tblob;
}
public void setTblob(byte[] tblob) {
this.tblob = tblob;
}
========================控制台測試代碼(讀取圖片到資料庫,
再從資料庫讀取圖片到特定路徑下)=======================================
/**
* 按大對象數據類型BLOB的byte[]類型
* CLOB的java.lang.String
* 映射 並插入數據
* @author Administrator
*
*/
public class Test {
Session session=null;
Transaction tx=null;
/**
* 持久化數據,讀取本地圖片到資料庫
*/
public void get1(){
姿態 try {
session=HibernateSessionFactory.getSession();
//前提是文件必須放在src路徑下,讀取的是當前項目的根目錄
// InputStream input=this.getClass().getResourceAsStream("/file.txt");
//載入任意路徑下的圖片、大文件、視屏等(括弧里的參數圖片是絕對路徑)
InputStream input=new FileInputStream("G:/在線拍賣/page/images/gou1.jpg");
tx=session.beginTransaction();
byte[] byteArray=new byte[input.available()];
input.read(byteArray);
input.close();
Bigobject b=new Bigobject();
b.setId(1);
b.setTblob(byteArray);
b.setTclob("一條狗");
session.save(b);
tx.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 從資料庫bigObject表中按主鍵讀取一條數據
* @throws IOException
*/
public void get2() throws IOException{
session=HibernateSessionFactory.getSession();
Bigobject b=(Bigobject)session.get(Bigobject.class, 1);
System.out.println("文本內容是:"+b.getTclob());
//吧位元組數組數據通過位元組流,輸出到當前工程根目錄下2.jpg中
if(b.getTblob()!=null){
FileOutputStream out=new FileOutputStream("dog1.jpg");
out.write(b.getTblob());
out.close();
}
}
public static void main(String[] args) throws IOException {
test6 t=new test6();
t.get1();
t.get2();
}
===================hibernate.cfg.xml的代碼頁給你貼一下吧,不過這都是自動生成的,你自己動手生成吧,一下是我自己的,想用的話得改參數的=======================
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:accp7 //!!!資料庫名稱
</property>
<property name="connection.username">scott</property>//!!!!!用戶名
<property name="connection.password">accp</property>//!!!!!!密碼
<property name="connection.driver_class">
oracle.jdbc.OracleDriver
</property>
<property name="myeclipse.connection.profile">scott</property>//!!!!!資料庫實例名
<mapping resource="entity/Bigobject.hbm.xml" />
</session-factory>
</hibernate-configuration>
3 上面介紹了BLOB的byte[]存儲,下面介紹另一種方式java.lang.Blob方式,還是直接粘貼代碼
++++++++++++++++++++資料庫還是上面圖片上的,保持不變++++++++++++++++++++++
====================Bigobject.hbm.xml映射文件======================
<hibernate-mapping>
<class name="bean.Bigobject" table="BIGOBJECT">
<id name="id" type="java.lang.Integer">
<column name="ID" precision="6" scale="0" />
<generator class="native" />
</id>
<property name="tclob" type="java.sql.Clob">//這里用的是Clob的另一種方式,有疑問再問
<column name="TCLOB" />
</property>
<property name="tblob" type="java.sql.Blob">//!!!!!注意,這里是java.sql.Blob
<column name="TBLOB" />
</property>
</class>
</hibernate-mapping>
========================以下是bigobject實體類=======================
import java.sql.Blob;
import java.sql.Clob;
/**
* Bigobject entity. @author MyEclipse Persistence Tools
*/
public class Bigobject implements java.io.Serializable {
// Fields
private Integer id;
private Clob tclob;
private Blob tblob;
// Constructors
/** default constructor */
public Bigobject() {
}
/** full constructor */
public Bigobject(Clob tclob, Blob tblob) {
this.tclob = tclob;
this.tblob = tblob;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Clob getTclob() {
return this.tclob;
}
public void setTclob(Clob tclob) {
this.tclob = tclob;
}
public Blob getTblob() {
return this.tblob;
}
public void setTblob(Blob tblob) {
this.tblob = tblob;
}
}
==========================對應的控制台測試代碼===========================
/**
* 講字元串大對象聲明為java.sql.Clob類型,二進制大對象聲明為java.sql.Blob類型
* @author Administrator
*
*/
public class test7 {
Session session=null;
Transaction tx=null;
public void get1(){
try {
session=HibernateSessionFactory.getSession();
//前提是文件必須放在src路徑下
InputStream input=this.getClass().getResourceAsStream("/upload.txt");
//載入任意路徑下的圖片、大文件、視屏等
// InputStream input=new FileInputStream("F:/1.jpg");
tx=session.beginTransaction();
byte[] byteArray=new byte[input.available()];
input.read(byteArray);
input.close();
Bigobject b=new Bigobject();
//依據二進制數據創建一個Blob對象 !!!!!重點 務必看清楚
b.setTblob(Hibernate.createBlob(byteArray));
//依據字元串數據創建一個Clob對象 !!!!!重點務必看清楚
b.setTclob(Hibernate.createClob("上傳圖片"));
session.save(b);
tx.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void get2(){
session=HibernateSessionFactory.getSession();
Bigobject obj=(Bigobject)session.get(Bigobject.class, 131);
//把Clob對象通過字元流讀入到內存,並輸出
try {
if(obj.getTclob()!=null){
Reader read=obj.getTclob().getCharacterStream();
char[] chArray=new char[1];
StringBuilder sb=new StringBuilder();
while(read.read(chArray)!=-1){
sb.append(new String(chArray));
}
System.out.println(sb.toString().trim());
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//把Blob對象通過位元組流讀輸出,並保存到當前工程根目錄下,取名為upload.txt
try {
if(obj.getTblob()!=null){
InputStream in=obj.getTblob().getBinaryStream();
FileOutputStream fos=new FileOutputStream("upload.txt");
int b=-1;
while((b=in.read())!=-1){
fos.write(b);
}
fos.close();
in.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
test7 t=new test7();
t.get1();
t.get2();
}
}
++++++++++++++++++++hibernate.cfg.xml同上++++++++++++++++++++++++++++
===================================================================
綜上,Blob與Clob(字元串大對象)的寫入讀出的兩種方法都有了(Clob的兩種方法你自己捎帶看看),都是完整代碼,粘貼即可用,整理了兩個多小時,希望對你有幫助!