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的两种方法你自己捎带看看),都是完整代码,粘贴即可用,整理了两个多小时,希望对你有帮助!