需求是这样的,数据库表字段是clob类型的,表结构大致如下:
需要使用hibernate3 往这个表里表插入Clob,Blob数据,
在此省略hibernate配置文件,实体映射文件和实体类代码如下:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Clob;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Client {
public static void main(String[] args) throws IOException {
//得到session
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
//得到图片的blob
InputStream in = new FileInputStream(“E://www.itxm.net.jpg”);
Blob blob = Hibernate.createBlob(in);
//得到简介的clob
Clob clob = Hibernate.createClob(“测试一下。#(*&#@¥%(*&@¥)(@#¥#¥”);
//创建图书对象
Book book = new Book();
book.setPhoto(blob);
book.setDescription(clob);
//存进数据库
session.beginTransaction();
session.save(book);
session.getTransaction().commit();
session.close();
}
}
使用hibernate的静态方法createBlob()得到Blob对象。createBlob方法需要一个InputStream对象作为参数。new一个FileInputStream作为它的参数,假设在F盘有一个图片文件名为4563123.jpg。
插入方法2:
import java.sql.Blob;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name = “T_FUNC_XT_YWDXSM”)
public class BlobTest implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private byte[] imgs;
private String mc;
private String id;
@Id
@GeneratedValue(generator = “system-uuid”)
@GenericGenerator(name = “system-uuid”, strategy = “uuid”)
@Column(name = “ID”)
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Column(name = “MC_DX”, nullable = true, length = 500)
public String getMc() {
return mc;
}
public void setMc(String mc) {
this.mc = mc;
}
@Lob
@Basic(fetch=FetchType.LAZY)
@Column(name=”IMAGE”, columnDefinition=”BLOB”, nullable=true)
public byte[] getImgs() {
return imgs;
}
public void setImgs(byte[] imgs) {
this.imgs = imgs;
}
}
public void blobTest() {
BlobTest b = new BlobTest();
try {
FileInputStream fis = new FileInputStream(“E://www.itxm.net.jpg”);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
b.setImgs(buffer);
b.setMc(“zhoushun”);
this.h.save(b);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// 定义文件读入流
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
使用hibernate的静态方法createClob()得到Clob对象。createClob方法直接传入字符串即可。
使用 hibernate3 从 book 表取出Clob,Blob数据
省略hibernate配置文件,实体映射文件和实体类代码,直接帖实现代码:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.sql.SQLException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Client {
public static void main(String[] args) throws IOException, SQLException {
//得到session
Configuration cfg = new Configuration().configure();
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
//从数据库里取值
session.beginTransaction();
Book book = (Book)session.get(Book.class,22);
session.getTransaction().commit();
session.close();
//把简历打印到控制台
Reader reader = book.getDescription().getCharacterStream();
int i = reader.read();
while(i!=-1){
System.out.print((char)i);
i=reader.read();
}
reader.close();
//把图片拷贝到D盘
InputStream in = book.getPhoto().getBinaryStream();
OutputStream out=new FileOutputStream(“E://www.itxm.net.jpg”);
byte[] b=new byte[1024];
while((i=in.read(b))!=-1){
out.write(b);
}
out.close();
in.close();
}
}
21-25行代码把数据从数据库中取出,封装到book对象中。
为了验证取值是否成功,第26-33行代码把Clob对象的内容打印到控制台,通过Clob对象的getCharacterStream()方法得到包含 CLOB 数据的 Java.io.Reader 对象,然后通过操作Reader对象把内容输出。
第34-42行代码把Blob对象包含的内容拷贝到d:/out.jpg文件中。通过Blob对象的getBinaryStream()方法得到包含 BLOB 数据的流,通过操作流把内容拷贝。
使用 hibernate4 往 book 表插入Clob,Blob数据
hibernate4相比hibernate3要复杂一些,如下:
public class Client {
public static void main(String[] args) throws IOException {
//得到session
Configuration cfg = new Configuration().configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sf = cfg.buildSessionFactory(sr);
Session session = sf.openSession();
//得到LobHelper
LobHelper lobHelper = session.getLobHelper();
//得到图片的blob
InputStream in = new FileInputStream(“E://www.itxm.net.jpg”);
Blob blob = lobHelper.createBlob(in, in.available());
//得到简介的clob
Clob clob = lobHelper.createClob(“测试一下。#(*&#@¥%(*&@¥)(@#¥#¥”);
//创建图书对象
Book book = new Book();
book.setPhoto(blob);
book.setDescription(clob);
//存进数据库
session.beginTransaction();
session.save(book);
session.getTransaction().commit();
session.close();
}
}
hibernate4中得到session对象的方法和hibernate3中略有不同,但用hibernate3的写法也是可以的。
在hibernate4中 hibernate.createBlob()方法和 hibernate.createClob()已经过时,取而代之
的是LobHelper.createBlob()和lobHelper.createClob()。可以通过session.getLobHelper();得到LobHerlper对象。
转载请注明来源网站:blog.ytso.com谢谢!
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/14688.html