日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
HibernateBlob數(shù)據(jù)類型映射的一個(gè)例子

以下為Hibernate Blob數(shù)據(jù)類型映射的一個(gè)例子,通過例子來把握Hibernate Blob數(shù)據(jù)類型映射。

Hibernate Blob:Java 代碼:

 
 
 
  1. public class User  implements 
  2. Java.io.Serializable {       
  3.        // Fields           
  4.      private long id;    
  5.      private String name;    
  6.      private String email;    
  7.      private String addr;    
  8.      //定義Blob的pthto    
  9.      private Blob photo;  

Hibernate Blob:xml 代碼:

 
 
 
  1.    
  2.      name="org.tie.User" table="user" catalog="tie">   
  3.          name="id" type="long">   
  4.              name="id" />   
  5.              class="identity" />   
  6.            
  7.          name="name" type="string">   
  8.              name="name" length="45" not-null="true" />   
  9.            
  10.          name="email" type="string">   
  11.              name="email" length="45" />   
  12.            
  13.          name="addr" type="string">   
  14.              name="addr" length="45" />   
  15.            
  16.            
  17.          name="photo" type="blob">   
  18.              name="photo" />   
  19.            
  20.        
  21.  

兩個(gè)測(cè)試方法:

Java 代碼:

 
 
 
  1.     public void testCreate(){                
  2.     User user = new User();    
  3.     user.setName("linweiyang");    
  4.     user.setAddr("beijing");    
  5.     user.setEmail("linweiyang@163.com");    
  6.     Blob photo = null;            
  7.     try {    
  8.         //將圖片讀進(jìn)輸入流    
  9.         FileInputStream fis = new FileInputStream("c:\\a.jpg");    
  10.         //轉(zhuǎn)成Blob類型    
  11.         photo = Hibernate.createBlob(fis);                    
  12.     } catch (FileNotFoundException e) {    
  13.         e.printStackTrace();    
  14.     } catch (IOException e) {    
  15.         e.printStackTrace();    
  16.     }                    
  17.     user.setPhoto(photo);               
  18.     Session session = factory.openSession();    
  19.     Transaction tr = session.beginTransaction();    
  20.     session.save(user);    
  21.     tr.commit();    
  22.     session.close();       
  23. }            
  24. public void testRerieve(){                
  25.     Session session = factory.openSession();    
  26.     User user = (User)session.load(User.class, new Long(3));    
  27.     try {    
  28.         //從數(shù)據(jù)庫中要讀取出來    
  29.         InputStream is = user.getPhoto().getBinaryStream();    
  30.         //在把寫到一個(gè)圖片格式的文件里    
  31.         FileOutputStream fos = new FileOutputStream("c:\\linweihan.jpg");                
  32.         byte[] buffer = new byte[1024];    
  33.         int len = 0;    
  34.         //從數(shù)據(jù)庫中讀取到指定的字節(jié)數(shù)組中    
  35.         while((len = is.read(buffer) )!= -1){    
  36.             //從指定的數(shù)組中讀取,然后輸出來,
  37.            所以這里buffer好象是連接inputStream和outputStream的一個(gè)東西    
  38.             fos.write(buffer,0,len);    
  39.         }    
  40.     } catch (FileNotFoundException e) {    
  41.         e.printStackTrace();    
  42.     } catch (SQLException e) {    
  43.         e.printStackTrace();    
  44.     } catch (IOException  e){    
  45.         e.printStackTrace();    
  46.     }               
  47.     session.close();    
  48. }   

這么理解輸入輸出流,讀入流自然要有讀入的源頭,輸出也要輸出到某個(gè)地方,輸出一般是先要輸讀入,這里連接輸入和輸出的是一個(gè)在內(nèi)存中的字節(jié)數(shù)組buffer.這樣從數(shù)據(jù)庫中讀到這個(gè)數(shù)組里,輸出流在從這個(gè)數(shù)組中輸出到特定的文件格式里。以上便是Hibernate Blob數(shù)據(jù)類型映射的一個(gè)例子。


網(wǎng)站標(biāo)題:HibernateBlob數(shù)據(jù)類型映射的一個(gè)例子
標(biāo)題鏈接:http://www.5511xx.com/article/cdogdos.html