1. 程式人生 > >hibernate_05_單表操作_對象類型

hibernate_05_單表操作_對象類型

exception doctype @override 1.0 服務註冊 dial 照片 dia []

本篇使用hibernate輸出一個對象(圖片)

先寫一個java類

 1 package com.imooc.hibernate;
 2 
 3 import java.sql.Blob;
 4 import java.util.Date;
 5 
 6 public class Students {
 7 
 8     private int sid;
 9     private String sname;
10     private String gender;
11     private Date birthday;
12     private String address;
13 private Blob picture; 14 15 public Blob getPicture() { 16 return picture; 17 } 18 19 public void setPicture(Blob picture) { 20 this.picture = picture; 21 } 22 23 public Students() {} 24 25 public Students(int sid, String sname, String gender, Date birthday, String address) {
26 super(); 27 this.sid = sid; 28 this.sname = sname; 29 this.gender = gender; 30 this.birthday = birthday; 31 this.address = address; 32 } 33 34 @Override 35 public String toString() { 36 return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday
37 + ", address=" + address + "]"; 38 } 39 40 public int getSid() { 41 return sid; 42 } 43 44 public void setSid(int sid) { 45 this.sid = sid; 46 } 47 48 public String getSname() { 49 return sname; 50 } 51 52 public void setSname(String sname) { 53 this.sname = sname; 54 } 55 56 public String getGender() { 57 return gender; 58 } 59 60 public void setGender(String gender) { 61 this.gender = gender; 62 } 63 64 public Date getBirthday() { 65 return birthday; 66 } 67 68 public void setBirthday(Date birthday) { 69 this.birthday = birthday; 70 } 71 72 public String getAddress() { 73 return address; 74 } 75 76 public void setAddress(String address) { 77 this.address = address; 78 } 79 }

創建Students.hbm.xml文件

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <!-- Generated 2017-5-23 0:24:09 by Hibernate Tools 3.5.0.Final -->
 5 <hibernate-mapping>
 6     <class name="com.imooc.hibernate.Students" table="STUDENTS">
 7         <id name="sid" type="int">
 8             <column name="SID" />
 9             <generator class="native" />
10         </id>
11         <property name="sname" type="java.lang.String">
12             <column name="SNAME" />
13         </property>
14         <property name="gender" type="java.lang.String">
15             <column name="GENDER" />
16         </property>
17         <property name="birthday" type="java.util.Date">
18             <column name="BIRTHDAY" />
19         </property>
20         <property name="address" type="java.lang.String">
21             <column name="ADDRESS" />
22         </property>
23         <property name="picture" type="java.sql.Blob">
24             <column name="PICTURE" />
25         </property>
26     </class>
27 </hibernate-mapping>

創建hibernate.cfg.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6     <session-factory>
 7         <property name="connection.username">root</property>
 8         <property name="connection.password">root</property>
 9         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
10         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property>
11         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
12         <property name="show_sql">true</property>
13         <property name="format_sql">true</property>
14         <property name="hbm2ddl.auto">update</property>
15         
16         <mapping resource="com/imooc/hibernate/Students.hbm.xml"/>
17     </session-factory>
18 </hibernate-configuration>

創建測試類

 1 package com.icoom.test;
 2 import java.io.File;
 3 import java.io.FileInputStream;
 4 import java.io.FileOutputStream;
 5 import java.io.InputStream;
 6 import java.io.OutputStream;
 7 import java.sql.Blob;
 8 import java.util.Date;
 9 
10 import org.hibernate.Hibernate;
11 import org.hibernate.Session;
12 import org.hibernate.SessionFactory;
13 import org.hibernate.Transaction;
14 import org.hibernate.cfg.Configuration;
15 import org.hibernate.service.ServiceRegistry;
16 import org.hibernate.service.ServiceRegistryBuilder;
17 import org.junit.After;
18 import org.junit.Before;
19 import org.junit.Test;
20 
21 import com.imooc.hibernate.Students;
22 
23 public class StudentsTest {
24     
25     private SessionFactory sessionFactory;
26     private Session session;
27     private Transaction transaction;
28 
29     @Before
30     public void init() {
31         // 1.創建配置對象
32         Configuration config = new Configuration().configure();
33         // 2.創建服務註冊對象
34         ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
35         // 3.創建會話工廠對象
36         sessionFactory = config.buildSessionFactory(serviceRegistry);
37         // 會話對象
38         session = sessionFactory.openSession();
39         // 開啟事務
40         transaction = session.beginTransaction();
41     }
42     @After
43     public void destory() {
44         transaction.commit(); //提交事務
45         session.close(); //關閉session
46         sessionFactory.close();//關閉會話工廠
47     }
48 
49     
50     /**
51      * 使用hibernate輸出對象
52      * @throws Exception
53      */
54     @Test
55     public void testReadBlob() throws Exception {
56         Students s = (Students)session.get(Students.class, 1);
57         // 獲得Blob對象
58         Blob image = s.getPicture();
59         // 獲得照片的輸入流
60         InputStream input = image.getBinaryStream();
61         // 創建輸出流
62         File f = new File("d:" + File.separator + "out龍貓.jpg");
63         // 獲得輸出流
64         OutputStream output = new FileOutputStream(f);
65         // 創建緩沖區
66         byte[] buff = new byte[input.available()];
67         input.read(buff);
68         output.write(buff);
69         input.close();
70         output.close();
71     }
72 }

執行成功,在D盤根目錄中就會出現out龍貓.jpg文件。

hibernate_05_單表操作_對象類型