您的位置:首页 > Web前端

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

2012-01-16 14:47 375 查看
Java 代码

publicclass User  implements java.io.Serializable {   
  
  
    // Fields      
  
     privatelong id;   
     private String name;   
     private String email;   
     private String addr;   
     //定义Blob的pthto  
     private Blob photo;  

xml 代码

<hibernate-mapping>  
    <classname="org.tie.User"table="user"catalog="tie">  
        <idname="id"type="long">  
            <columnname="id"/>  
            <generatorclass="identity"/>  
        </id>  
        <propertyname="name"type="string">  
            <columnname="name"length="45"not-null="true"/>  
        </property>  
        <propertyname="email"type="string">  
            <columnname="email"length="45"/>  
        </property>  
        <propertyname="addr"type="string">  
            <columnname="addr"length="45"/>  
        </property>  
        <!-- 映射blob类型 -->  
        <propertyname="photo"type="blob">  
            <columnname="photo"/>  
        </property>  
    </class>  
</hibernate-mapping>  

两个测试方法:

java 代码

publicvoid 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();   
  
     }   
       
    publicvoid 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.这样从数据库中读到这个数组里,输出流在从这个数组中输出到特定的文件格式里.

来源:http://hi.baidu.com/networkcrazy/blog/item/1398b155f0ef1350d0090602.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息