Hibernate Blob数据类型映射的一个例子,从BLOB中取值
2012-12-14 10:22
423 查看
以下为Hibernate Blob数据类型映射的一个例子,通过例子来把握Hibernate Blob数据类型映射。
Hibernate Blob:Java 代码:
Hibernate Blob:xml 代码:
两个测试方法:
Java 代码:
这么理解输入输出流,读入流自然要有读入的源头,输出也要输出到某个地方,输出一般是先要输读入,这里连接输入和输出的是一个在内存中的字节数组buffer.这样从数据库中读到这个数组里,输出流在从这个数组中输出到特定的文件格式里。以上便是Hibernate Blob数据类型映射的一个例子。
原文地址:http://developer.51cto.com/art/200906/130231.htm
Hibernate Blob:Java 代码:
public class User implements Java.io.Serializable { // Fields private long id; private String name; private String email; private String addr; //定义Blob的pthto private Blob photo;
Hibernate Blob:xml 代码:
<Hibernate-mapping> <class name="org.tie.User" table="user" catalog="tie"> <id name="id" type="long"> <column name="id" /> <generator class="identity" /> </id> <property name="name" type="string"> <column name="name" length="45" not-null="true" /> </property> <property name="email" type="string"> <column name="email" length="45" /> </property> <property name="addr" type="string"> <column name="addr" length="45" /> </property> <!-- 映射blob类型 --> <property name="photo" type="blob"> <column name="photo" /> </property> </class> </Hibernate-mapping>
两个测试方法:
Java 代码:
public void testCreate(){ User user = new User(); user.setName("linweiyang"); user.setAddr("beijing"); user.setEmail("linweiyang@163.com"); Blob photo = null; try { //将图片读进输入流 FileInputStream fis = new FileInputStream("c:\\a.jpg"); //转成Blob类型 photo = Hibernate.createBlob(fis); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } user.setPhoto(photo); Session session = factory.openSession(); Transaction tr = session.beginTransaction(); session.save(user); tr.commit(); session.close(); } public void testRerieve(){ Session session = factory.openSession(); User user = (User)session.load(User.class, new Long(3)); try { //从数据库中要读取出来 InputStream is = user.getPhoto().getBinaryStream(); //在把写到一个图片格式的文件里 FileOutputStream fos = new FileOutputStream("c:\\linweihan.jpg"); byte[] buffer = new byte[1024]; int len = 0; //从数据库中读取到指定的字节数组中 while((len = is.read(buffer) )!= -1){ //从指定的数组中读取,然后输出来, 所以这里buffer好象是连接inputStream和outputStream的一个东西 fos.write(buffer,0,len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); } session.close(); }
这么理解输入输出流,读入流自然要有读入的源头,输出也要输出到某个地方,输出一般是先要输读入,这里连接输入和输出的是一个在内存中的字节数组buffer.这样从数据库中读到这个数组里,输出流在从这个数组中输出到特定的文件格式里。以上便是Hibernate Blob数据类型映射的一个例子。
原文地址:http://developer.51cto.com/art/200906/130231.htm
相关文章推荐
- hibernate中映射blob数据类型的一个例子
- Hibernate Blob数据类型映射的一个例子
- 收藏一个关于数据库存储BLOB类型数据的方法
- 对于orale中的表数据类型的一个简单例子
- 一个类数据类型的STL例子
- 一个简单但详细的解释Windows文件映射读取数据文件的例子
- 使用template扩展已有的DDX_Text函数,使扩展能够同时支持各种数据类型的一个例子
- 一个简单但详细的解释Windows文件映射读取数据文件的例子
- Java8Map示例:一个略复杂的数据映射聚合例子及代码重构
- 使用template扩展已有的DDX_Text函数,使扩展能够同时支持各种数据类型的一个例子
- 一个简单但详细的解释Windows文件映射读取数据文件的例子
- 一个用于网络传输,转换数据类型到byte的例子
- 一个例子说明java数据类型包括对象是赋值还是引用!!!
- Qt QHash 和QMap 区别时间: 2010-11-17 / 浏览次数: 75 views / 1个评论 发表评论QMap提供了一个从类项为key的键到类项为T的直的映射,通常所存储的数据类型是一个键对应一个直,并且按照Key的次序存储数据,
- C++中提供了多种基本的数据类型。实际上,这些远不能满足我们的需求,如复数(第10章的例子大多是处理虚数的),再如分数。本任务将设计一个简单的分数类,完成对分数的几个运算。一则巩固基于对象编程的方法,
- oracle blob数据类型存储读取的小例子
- 一个简单但详细的解释Windows文件映射读取数据文件的例子
- JAVA 各数据类型转换二进制数组的很好例子!
- BLOB数据类型文件[PDF]的存取(Spring+Mybatis+Db2+Maven)
- 基本数据类型--取值范围