`
tanlan
  • 浏览: 202169 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

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

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="connection.driver_class">
			oracle.jdbc.driver.OracleDriver
	</property>
		<property name="connection.url">
			jdbc:oracle:thin:@localhost:1521:XE
	</property>
		<property name="connection.username">tanlan</property>
		<property name="connection.password">tanlan</property>
		<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
		<property name="show_sql">true</property>
		<mapping resource="com/tanlan/hibernate/entity/User.hbm.xml" />
	</session-factory>
</hibernate-configuration>
 
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
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.tanlan.hibernate.entity">
	<class name="User" table="TUser">
		<id name="id">
			<generator class="uuid" />
		</id>
		<property name="name" />
		<property name="photo" type="binary" />
	</class>
</hibernate-mapping>
 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();
	}

}
 
2
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics