av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

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è)測試方法:

Java 代碼:

 
 
 
  1.     public void testCreate(){                
  2.     User user = new User();    
  3.     user.setName("linweiyang");    
  4.     user.setAddr("beijing");    
  5.     user.setEmail("[email protected]");    
  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://uogjgqi.cn/article/cdogdos.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們在微信上24小時(shí)期待你的聲音

解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流