`

hibernate中映射blob数据类型的一个例子

阅读更多
java 代码
  1. public class User  implements java.io.Serializable {   
  2.   
  3.   
  4.     // Fields       
  5.   
  6.      private long id;   
  7.      private String name;   
  8.      private String email;   
  9.      private String addr;   
  10.      //定义Blob的pthto   
  11.      private Blob photo;  
xml 代码
  1. <hibernate-mapping>  
  2.     <class name="org.tie.User" table="user" catalog="tie">  
  3.         <id name="id" type="long">  
  4.             <column name="id" />  
  5.             <generator class="identity" />  
  6.         </id>  
  7.         <property name="name" type="string">  
  8.             <column name="name" length="45" not-null="true" />  
  9.         </property>  
  10.         <property name="email" type="string">  
  11.             <column name="email" length="45" />  
  12.         </property>  
  13.         <property name="addr" type="string">  
  14.             <column name="addr" length="45" />  
  15.         </property>  
  16.         <!-- 映射blob类型 -->  
  17.         <property name="photo" type="blob">  
  18.             <column name="photo" />  
  19.         </property>  
  20.     </class>  
  21. </hibernate-mapping>  

两个测试方法:

java 代码
  1. public void testCreate(){   
  2.            
  3.         User user = new User();   
  4.         user.setName("linweiyang");   
  5.         user.setAddr("beijing");   
  6.         user.setEmail("linweiyang@163.com");   
  7.         Blob photo = null;   
  8.        
  9.         try {   
  10.             //将图片读进输入流   
  11.             FileInputStream fis = new FileInputStream("c:\\a.jpg");   
  12.             //转成Blob类型   
  13.             photo = Hibernate.createBlob(fis);   
  14.                
  15.         } catch (FileNotFoundException e) {   
  16.             e.printStackTrace();   
  17.         } catch (IOException e) {   
  18.             e.printStackTrace();   
  19.         }   
  20.                
  21.         user.setPhoto(photo);   
  22.            
  23.         Session session = factory.openSession();   
  24.         Transaction tr = session.beginTransaction();   
  25.         session.save(user);   
  26.         tr.commit();   
  27.         session.close();   
  28.   
  29.     }   
  30.        
  31.     public void testRerieve(){   
  32.            
  33.         Session session = factory.openSession();   
  34.         User user = (User)session.load(User.classnew Long(3));   
  35.         try {   
  36.             //从数据库中要读取出来   
  37.             InputStream is = user.getPhoto().getBinaryStream();   
  38.             //在把写到一个图片格式的文件里   
  39.             FileOutputStream fos = new FileOutputStream("c:\\linweihan.jpg");   
  40.                
  41.             byte[] buffer = new byte[1024];   
  42.             int len = 0;   
  43.             //从数据库中读取到指定的字节数组中   
  44.             while((len = is.read(buffer) )!= -1){   
  45.                 //从指定的数组中读取,然后输出来,所以这里buffer好象是连接inputStream和outputStream的一个东西   
  46.                 fos.write(buffer,0,len);   
  47.             }   
  48.         } catch (FileNotFoundException e) {   
  49.             e.printStackTrace();   
  50.         } catch (SQLException e) {   
  51.             e.printStackTrace();   
  52.         } catch (IOException  e){   
  53.             e.printStackTrace();   
  54.         }              
  55.         session.close();   
  56.     }  

这么理解输入输出流

读入流,自然要有读入的源头,

输出也要输出到某个地方,输出一般是先要输读入,

这里连接输入和输出的是一个在内存中的字节数组buffer.这样从数据库中读到这个数组里,输出流在从这个数组中输出到特定的文件格式里.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics