一、Hibernate環境搭建、創建工程略。java
二、首先咱們新建一個User類,儲存一些用戶信息字段,在Java中photo字段要申明爲應該byte[]類型sql
User.java數據庫
public class User { private int id; private String name; private Integer age; private Date birthday; // 生日 private String desc; // 一大段說明 private byte[] photo; // 頭像圖片 //省略getter and setter Method... }
三、而後配置User的映射文件數組
User.hbm.xmlsession
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.itcast.c_hbm_property"> <!-- name屬性:哪一個類 table屬性:對應哪一個表,若是不寫,默認的表名就是類的簡單名稱 --> <class name="User" table="t_user"> <id name="id" type="int" column="id"> <generator class="native"/> </id> <!-- 普通的屬性(數據庫中的基本類型,如字符串、日期、數字等) name屬性:對象中的屬性名,必需要有。 type屬性:類型,若是不寫,Hibernate會自動檢測。 能夠寫Java中類的全名。 或是寫hibernate類型。 column屬性:對應表中的列名,若是沒有,默認爲屬性名。 length屬性:長度,不是全部的類型都有長度屬性,好比varchar有,但int沒有,若是不寫默認爲255 not-null屬性:非空約束,默認爲false --> <!-- <property name="name"/> --> <property name="name" type="string" column="name" length="20" not-null="true"/> <property name="age" type="int" column="age_"/> <property name="birthday" type="date" column="birthday_"/> <!-- 當列表與關鍵字衝突時,能夠經過column屬性指定一個其餘的列名。 或是使用反引號包圍起來。 指定使用text類型時,最好再指定length,以肯定生成的SQL類型是可以存放指定數量的字符的。 <property name="desc"> <column name="desc_" length="5000" sql-type="text"/> </property> --> <property name="desc" type="text" length="5000" column="`desc`" ></property> <!-- 頭像,二進制類型,最好指定長度 --> <property name="photo" type="binary" length="102400"></property> </class> </hibernate-mapping>
四、測試類測試新增User信息
app
photoTest.java函數
public class photoTest{ private static SessionFactory sessionFactory; static { sessionFactory = new Configuration()// .configure()// 讀取配置文件 .addClass(User.class)// .buildSessionFactory(); } @Test public void testSave() throws Exception { // 讀取圖片文件 InputStream in = new FileInputStream( "c:/test.png"); byte[] photo = new byte[in.available()]; //用inputStream對象的available()方法獲取流中可讀取的數據大小,一般咱們調用這個函數是在下載文件或者對文件進行其餘處理時獲取文件的總大小。 in.read(photo);// 從輸入流中讀取指定數量的字節,並將其存儲在緩衝區數組 b 中 in.close(); // 建立對象實例 User user = new User(); user.setName("張三"); user.setAge(20); user.setBirthday(new Date()); user.setDesc("一大段的說明,此處省略5000字……"); user.setPhoto(photo); // 保存 Session session = sessionFactory.openSession(); // 打開一個新的Session Transaction tx = session.beginTransaction(); // 開始事務 session.save(user); tx.commit(); // 提交事務 session.close(); // 關閉Session,釋放資源 } }
上面代碼就是把c盤根目錄的test.png圖片保存到了數據庫。測試
經過SQL查詢該字段可看到是二進制數據,那麼證實保存成功。ui
五、咱們再把圖片讀取出來放在D盤下,取名copy.pngspa
photoTest.testGet()方法
@Test public void testGet() throws Exception Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); User user = (User) session.get(User.class, 1); // 獲取 System.out.println(user.getId()); System.out.println(user.getName()); System.out.println(user.getDesc()); System.out.println(user.getPhoto()); OutputStream out = new FileOutputStream("D:/copy.png"); out.write(user.getPhoto()); out.close(); tx.commit(); session.close(); }
經測試成功運行...........