Hibernate实现Oracle BLOB的数据读写(1)

开发中文件上传到服务器,一般将文件保存在Web服务器的某个目录下,除非有特殊要求将文件存到数据库中保存。

本文主要基于学习的目的而作。

测试环境:Hibernate3.6.7,Oracle 10g Express,JDK7,Win7

1,数据库脚本

create table TUser  (
   ID                   char(32)                        not null,
   name               varchar(10char)                 not null,
   photo               blob,                              --头像
   constraint PK_TUser primary key (ID)
);
 

2,Hibernate配置文件,本文基于传统的hibernate.cfg.xml



	
		
			oracle.jdbc.driver.OracleDriver
	
		
			jdbc:oracle:thin:@localhost:1521:XE
	
		tanlan
		tanlan
		org.hibernate.dialect.Oracle10gDialect
		true
		
	

 
3,编写实体类User.java
package com.tanlan.hibernate.entity;

public class User {
	private String id;
	private String name;
	private byte[] photo;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public byte[] getPhoto() {
		return photo;
	}
	public void setPhoto(byte[] photo) {
		this.photo = photo;
	}
}
 4,编写对应的映射文件,User.hbm.xml


	
		
			
		
		
		
	
  5,测试代码
package com.tanlan.hibernate.test;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

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

import com.tanlan.hibernate.entity.User;

public class TestUser {

	public static void main(String[] args) {
		addUser();
		//getUserById();
		//update();
	}

	private static void addUser() {
		User user = new User();
		user.setName("谭岚");
		File photo = new File("I:\\1.jpg");
		try {
			FileInputStream is = new FileInputStream(photo);
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			byte[] temp = new byte[512];
			int i = 0;
			while ((i = is.read(temp, 0, temp.length)) != -1) {
				os.write(temp, 0, temp.length);
			}
			os.close();
			is.close();
			user.setPhoto(os.toByteArray());
		} catch (Exception e) {
			e.printStackTrace();
		}
		Session session = openSession();
		Transaction transaction = session.beginTransaction();
		session.save(user);
		transaction.commit();
	}

	private static void getUserById() {
		Session session = openSession();
		User user = (User) session.get(User.class,
				"402881e432993eae0132993eb7d20000");
		byte[] photo = user.getPhoto();
		try {
			FileOutputStream os = new FileOutputStream("E:\\111.jpg");
			os.write(photo);
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void update() {
		User user = new User();
		user.setId("402881e432993eae0132993eb7d20000");
		user.setName("新名");
		File photo = new File("I:\\2.jpg");
		try {
			FileInputStream is = new FileInputStream(photo);
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			byte[] temp = new byte[512];
			int i = 0;
			while ((i = is.read(temp, 0, temp.length)) != -1) {
				os.write(temp, 0, temp.length);
			}
			os.close();
			is.close();
			user.setPhoto(os.toByteArray());
		} catch (Exception e) {
			e.printStackTrace();
		}
		Session session = openSession();
		Transaction transaction = session.beginTransaction();
		session.update(user);
		transaction.commit();
	}

	private static Session openSession() {
		Configuration cfg = new Configuration().configure();
		return cfg.buildSessionFactory().openSession();
	}

}
 

你可能感兴趣的:(Hibernate,Database)