標籤:show port .hbm.xml 姓名 oca dem aging property pen
? 著作權聲明:本文為博主原創文章,轉載請註明出處
執行個體
1.項目結構
2.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion><groupId>org.hibernate</groupId><artifactId>Hibernate-Image</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>Hibernate-Image Maven Webapp</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><hibernate.version>5.1.6.Final</hibernate.version></properties><dependencies><!-- junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!-- hibernate --><dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version></dependency><!-- MySQL --><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.42</version></dependency></dependencies> <build> <finalName>Hibernate-Image</finalName> </build></project>
3.Student.java
package org.hibernate.model;import java.sql.Blob;import java.util.Date;public class Student {private long sid;// 學號private String sname;// 姓名private String gender;// 性別private Date birthday;// 生日private String address;// 性別private Blob image;// 頭像public Student() {}public Student(String sname, String gender, Date birthday, String address, Blob image) {this.sname = sname;this.gender = gender;this.birthday = birthday;this.address = address;this.image = image;}public long getSid() {return sid;}public void setSid(long sid) {this.sid = sid;}public String getSname() {return sname;}public void setSname(String sname) {this.sname = sname;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public Blob getImage() {return image;}public void setImage(Blob image) {this.image = image;}}
4.Student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" ><hibernate-mapping><class name="org.hibernate.model.Student" table="STUDENT"><id name="sid" type="java.lang.Long"><column name="SID"/><generator class="native"/></id><property name="sname" type="java.lang.String"><column name="SNAME"/></property><property name="gender" type="java.lang.String"><column name="GENDER"/></property><property name="birthday" type="date"><column name="BIRTHDAY"/></property><property name="address" type="java.lang.String"><column name="ADDRESS"/></property><property name="image" type="java.sql.Blob"><column name="IMAGE"/></property></class></hibernate-mapping>
5.hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><!-- SessionFactory配置 --><session-factory><!-- 資料庫連接配置 --><property name="connection.username">root</property><property name="connection.password">20121221</property><property name="connection.drvier_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql:///hibernate?useSSL=true&characterEncoding=UTF-8</property><!-- 基本配置 --><property name="hbm2ddl.auto">update</property><property name="show_sql">true</property><property name="format_sql">true</property><property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property><!-- 引用對應檔 --><mapping resource="hbm/Student.hbm.xml"/></session-factory></hibernate-configuration>
6.ImageTest.java
package org.hibernate.test;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.sql.Blob;import java.util.Date;import org.hibernate.Hibernate;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.model.Student;import org.junit.After;import org.junit.Before;import org.junit.Test;public class ImageTest {private SessionFactory sessionFactory;private Session session;private Transaction transaction;@Beforepublic void before() {Configuration config = new Configuration().configure();// 建立設定物件sessionFactory = config.buildSessionFactory();// 建立SessionFactory對象session = sessionFactory.openSession();// 建立session對象transaction = session.beginTransaction(); // 開啟事務}@Afterpublic void after() {transaction.commit();// 提交事務session.close();// 關閉sessionsessionFactory.close();// 關閉SessionFactory}@Testpublic void saveStudentWithImage() throws Exception {// 擷取圖片檔案File file = new File("C:/Users/chen/Pictures/demo.jpg");// 擷取圖片檔案的輸入資料流InputStream is = new FileInputStream(file);// 建立一個Blob對象Blob image = Hibernate.getLobCreator(session).createBlob(is, is.available());// 建立Student對象Student s = new Student("張三", "男", new Date(), "北京市", image);// 儲存對象session.save(s);}@Testpublic void readImage() throws Exception {Student s = session.get(Student.class, 1L);// 擷取圖片的Blob對象Blob image = s.getImage();// 擷取照片的輸入資料流InputStream is = image.getBinaryStream();// 建立輸出檔案File file = new File("C:/Users/chen/Pictures/test.jpg");// 擷取輸出資料流OutputStream os = new FileOutputStream(file);// 建立緩衝區byte[] buff = new byte[is.available()];// 將輸入資料流讀入到緩衝中is.read(buff);// 將緩衝區資料寫到輸出資料流中os.write(buff);// 關閉輸入資料流is.close();// 關閉輸出資料流os.close();}}
7.效果預覽
7.1 執行saveStudentWithImage()方法
7.2 執行readImage()方法
參考:http://www.imooc.com/video/7740
Hibernate學習四----------Blob