? Copyright NOTICE: This article is the original blogger article, reproduced please indicate the source
Instance
1. Project structure
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> 3.student.java
Package Org.hibernate.model;import Java.sql.blob;import Java.util.date;public class Student {private Long sid;// School Number private string sname;//name private string gender;//sex private Date birthday;//birthday private string address;//sex private Blob image;//Avatar Public Student () {}public Student (string sname, String gender, Date birthday, string address, Blob image) {thi S.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 ">
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 "> 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 ();//create config object sessionfactory = config.buildsessionfactory ();// Create Sessionfactory Object session = Sessionfactory.opensession ();//create Session object transaction = Session.begintransaction (); Open transaction} @Afterpublic void After () {transaction.commit ();//COMMIT Transaction session.close ();//close Sessionsessionfactory.close ();// Close Sessionfactory} @Testpublic VOID Savestudentwithimage () throws Exception {//get picture Files file = new file ("C:/users/chen/pictures/demo.jpg");// Gets the input stream of the picture file InputStream is = new FileInputStream (file);//Creates a Blob object blob image = Hibernate.getlobcreator (session). Createblob (IS, is.available ());//Create Student object Student s = new Student ("Zhang San", "male", New Date (), "Beijing", image);//Save Object Session.s Ave (s);} @Testpublic void Readimage () throws Exception {Student s = session.get (Student.class, 1L);//Get a picture of blob object blob image = S.get Image ();//Gets the input stream of the photo inputstream is = Image.getbinarystream ();//creates the output file, filename File = new file ("c:/users/chen/pictures/ Test.jpg ");//Get output stream outputstream OS = new FileOutputStream (file);//create buffer byte[] buff = new byte[is.available ()];//read input stream into Cache Is.read (buff);//write buffer data to the output stream os.write (buff);//Close input stream is.close ();//Close output stream os.close ();}}7. Preview of effects
7.1 Execute Savestudentwithimage () method
7.2 Execute Readimage () method
Reference: http://www.imooc.com/video/7740
Hibernate learning four----------blobs