用hibernate操作BlobClob

一,操作blob图像数据
数据表login2:
name type 约束
id number(5) 主健
username varchar2(20
password varchar2(20)
image blob
description clob

配置文件不在编写
java代码如下:
package dao;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.SQLException;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import entity.Login2;

public class Login2Dao {
	private Session session = null;
	private Transaction tran = null;
	private static final ThreadLocal<Session> s = new ThreadLocal<Session>();

	public Login2Dao() {
		SessionFactory factory = new Configuration().configure()
				.buildSessionFactory();
		this.session = s.get();
		if (this.session == null) {
			session = factory.openSession();
			s.set(session);
			System.out.println(session == null);
		}
		System.out.println(session == null);
		// session.close();
	}

	public int saveBlog(Login2 login, String imagePath) {
		// 读取图片的二进制数据
		try {
			InputStream in = this.getClass().getResourceAsStream(imagePath);
			System.out.println("输入流" + in == null);
			byte[] buffer = new byte[in.available()];
			in.read(buffer);
			in.close();

			login.setImage(Hibernate.createBlob(buffer));
			tran = session.beginTransaction();
			session.save(login);
			tran.commit();
			return login.getId();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	public void getBlob(int id, String targetPath) throws SQLException,
			IOException {
		// 使用hibernate读取blob字段
		Login2 login = (Login2) session.get(Login2.class, id);
		Blob image = login.getImage();
		// 使用javaio将blob内容复制到文件
		InputStream in = image.getBinaryStream();
		OutputStream out = new FileOutputStream(targetPath);

		int n = -1;
		while ((n = in.read()) != -1) {
			out.write(n);
		}

		in.close();
		out.close();
	}

}
//测试类
package test;

import java.io.IOException;
import java.sql.SQLException;

import dao.Login2Dao;
import entity.Login2;

public class Test2 {
public static void main(String[] args) throws SQLException, IOException {
	Login2 login=new Login2();
	login.setPassword("1111");
	login.setUsername("mada");
	int id=new Login2Dao().saveBlog(login,"../person.jpg");
	//System.out.println("id=="+id);
	//new Login2Dao().getBlob(id, "e:\\org.jpg");
	new Login2Dao().getBlob(3, "person2.jpg");
}
}


提示:在操作时,文件路径不能写绝对路径,看网上可以,还待试验
二。操作clob(针对的是字符数据)
package dao;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.SQLException;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import entity.Login2;

public class ClobLogin2Dao {
	//会话对象
	private Session session=null;
	//事务对象
	private Transaction tran=null;
	//threadLocal变量
	private static final ThreadLocal<Session> s=new ThreadLocal<Session>();
	
	//构造方法
	public ClobLogin2Dao(){
		Configuration config=new Configuration().configure();
		SessionFactory factory=config.buildSessionFactory();
		this.session=s.get();//怎么理解
		if(session==null){
			this.session=factory.openSession();
			s.set(session);
		}
	}
	//保存方法
	public int saveClob(Login2 login,String description){
		tran=this.session.beginTransaction();//开启事务
		login.setDescription(Hibernate.createClob(description));//把字符串转为CLOB
		session.save(login);
		tran.commit();
		return login.getId();
	}
	//输出
	public String getClob(int id) throws SQLException, IOException{
		String str="";
		Login2 login=(Login2)this.session.get(Login2.class, id);
		Clob desc=login.getDescription();
		//把clob转换成String
		Reader rd=desc.getCharacterStream();
		BufferedReader br=new BufferedReader(rd);
		/*while((str=br.readLine())==null){
			System.out.println(str);
		}*/
		 str=br.readLine();
		 return str;
	}
	}
//测试类
package test;

import java.io.IOException;
import java.sql.SQLException;

import dao.ClobLogin2Dao;
import entity.Login2;

public class TestClob {
public static void main(String[] args) throws SQLException, IOException {
	Login2 login=new Login2();
	login.setPassword("22222");
	login.setUsername("bbbs");
	String description="马达,成就优异";
	int id=new ClobLogin2Dao().saveClob(login, description);
	System.out.println("id=="+id);
	String desc=new ClobLogin2Dao().getClob(id);
	//new ClobLogin2Dao().getClob(17);
	System.out.println(desc);
}
}

注:clob斩时只实现到能存多行,取时只能取单行

你可能感兴趣的:(Hibernate)