Dict.CN 在线词典, 英语学习, 在线翻译

都市淘沙者

荔枝FM Everyone can be host

统计

留言簿(23)

积分与排名

优秀学习网站

友情连接

阅读排行榜

评论排行榜

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

 1java 代码
 2public class User  implements java.io.Serializable {    
 3   
 4   
 5    // Fields        
 6   
 7     private long id;    
 8     private String name;    
 9     private String email;    
10     private String addr;    
11     //定义Blob的pthto    
12     private Blob photo;   
13
14xml 代码
15<hibernate-mapping>   
16    <class name="org.tie.User" table="user" catalog="tie">   
17        <id name="id" type="long">   
18            <column name="id" />   
19            <generator class="identity" />   
20        </id>   
21        <property name="name" type="string">   
22            <column name="name" length="45" not-null="true" />   
23        </property>   
24        <property name="email" type="string">   
25            <column name="email" length="45" />   
26        </property>   
27        <property name="addr" type="string">   
28            <column name="addr" length="45" />   
29        </property>   
30        <!-- 映射blob类型 -->   
31        <property name="photo" type="blob">   
32            <column name="photo" />   
33        </property>   
34    </class>   
35</hibernate-mapping>   
36
37两个测试方法:
38
39java 代码
40public void testCreate(){    
41            
42        User user = new User();    
43        user.setName("linweiyang");    
44        user.setAddr("beijing");    
45        user.setEmail("linweiyang@163.com");    
46        Blob photo = null;    
47        
48        try {    
49            //将图片读进输入流    
50            FileInputStream fis = new FileInputStream("c:\\a.jpg");    
51            //转成Blob类型    
52            photo = Hibernate.createBlob(fis);    
53                
54        }
 catch (FileNotFoundException e) {    
55            e.printStackTrace();    
56        }
 catch (IOException e) {    
57            e.printStackTrace();    
58        }
    
59                
60        user.setPhoto(photo);    
61            
62        Session session = factory.openSession();    
63        Transaction tr = session.beginTransaction();    
64        session.save(user);    
65        tr.commit();    
66        session.close();    
67   
68    }
    
69        
70    public void testRerieve(){    
71            
72        Session session = factory.openSession();    
73        User user = (User)session.load(User.classnew Long(3));    
74        try {    
75            //从数据库中要读取出来    
76            InputStream is = user.getPhoto().getBinaryStream();    
77            //在把写到一个图片格式的文件里    
78            FileOutputStream fos = new FileOutputStream("c:\\linweihan.jpg");    
79                
80            byte[] buffer = new byte[1024];    
81            int len = 0;    
82            //从数据库中读取到指定的字节数组中    
83            while((len = is.read(buffer) )!= -1){    
84                //从指定的数组中读取,然后输出来,所以这里buffer好象是连接inputStream和outputStream的一个东西    
85                fos.write(buffer,0,len);    
86            }
    
87        }
 catch (FileNotFoundException e) {    
88            e.printStackTrace();    
89        }
 catch (SQLException e) {    
90            e.printStackTrace();    
91        }
 catch (IOException  e){    
92            e.printStackTrace();    
93        }
               
94        session.close();    
95    }
   

这么理解输入输出流

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

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

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

posted on 2007-12-17 16:25 都市淘沙者 阅读(2000) 评论(1)  编辑  收藏 所属分类: Hibernate/ORM

评论

# Podbor Klyuchevyh Slov 2009-05-18 10:34 Podbor Klyuchevyh Slov

Badly need your help. A smiling face is half the meal.
I am from Saudi and , too, and now am writing in English, please tell me right I wrote the following sentence: "Algorithm analysis, web community relationship analysis, seo practices and techniques, industry news, etc.Miami seo by unlimitedwebdesigns, south florida seo."

Thanks :-D. Dempsey.  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: