hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
Person.hbm.xml
<?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>
<class name="com.messi.model.Person" table="_person">
<id name="id" column="id" type="int">
<generator class="increment"></generator>
</id>
<property name="username" type="string"></property>
<property name="password" type="string"></property>
<property name="gender" type="character"></property>
<property name="birthday" type="date"></property>
<property name="graduation" type="boolean"></property>
<property name="telephone" type="long"></property>
<property name="marryTime" type="timestamp"></property>
<property name="file" type="binary"></property>
</class>
</hibernate-mapping>
Person.java
package com.messi.model;
import java.sql.Date;
import java.sql.Timestamp;
public class Person {
private int id;
private String username;
private String password;
private char gender;
private Date birthday;
private boolean graduation;
private long telephone;
private Timestamp marryTime;
private byte[] file;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public boolean isGraduation() {
return graduation;
}
public void setGraduation(boolean graduation) {
this.graduation = graduation;
}
public long getTelephone() {
return telephone;
}
public void setTelephone(long telephone) {
this.telephone = telephone;
}
public Timestamp getMarryTime() {
return marryTime;
}
public void setMarryTime(Timestamp marryTime) {
this.marryTime = marryTime;
}
public byte[] getFile() {
return file;
}
public void setFile(byte[] file) {
this.file = file;
}
}
Test.java
package com.messi.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Timestamp;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.messi.model.Person;
@SuppressWarnings("deprecation")
public class Test {
public static void main(String[] args) {
SessionFactory sf;
Transaction tx = null;
try {
sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
tx = session.beginTransaction();
Person person = new Person();
person.setUsername("zhangsan");
person.setPassword("123");
person.setGender('男');
person
.setBirthday(new java.sql.Date(new java.util.Date()
.getTime()));
person.setGraduation(false);
person.setTelephone(1234567890);
person.setMarryTime(new Timestamp(new java.util.Date().getTime()));
InputStream is = new FileInputStream("d:/html_zh_CN.zip");
byte[] buffer = new byte[is.available()];
is.read(buffer);
person.setFile(buffer);
session.save(person);
tx.commit();
tx = session.beginTransaction();
Query query = session.createQuery("from Person");
List<Person> list = (List<Person>) query.list();
for (Person p : list) {
System.out.println(p.getId());
FileOutputStream fos = new FileOutputStream("c:/html_zh_CN"
+ p.getId() + ".zip");
fos.write(p.getFile());
}
tx.commit();
} catch (Exception e) {
if (null != tx) {
tx.rollback();
}
e.printStackTrace();
}
}
}