1、hibernate4下创建SessionFactory
package com.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateBuilder {
private final static Class<HibernateBuilder> LOCK = HibernateBuilder.class;
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactoryInstance() {
synchronized (LOCK) {
if(sessionFactory != null) {
return sessionFactory;
}
try {
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
e.printStackTrace();
}
}
return sessionFactory;
}
public static Session getSession() {
if (sessionFactory == null) {
getSessionFactoryInstance();
}
return sessionFactory.openSession();
}
}
2、hibernate.cfg.xml文件配置(如上方法会默认寻找根路径下的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>
<session-factory>
<!-- 1 数据库部分配置 必须-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://ip:3306/demo</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<!-- 配置c3p0连接池 -->
<!-- <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> -->
<!-- 2 hibernate基本配置 可选 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!--
创建数据库表,需要配置实现
create-drop: 如果数据库存在相同的表,把表删除再创建
create:如果数据库存在相同的表,再创建相同字段的表
update:如果数据库存在相同的表,把表更新,如果没有表,创建表
validate:如果数据库存在相同的表,把表更新,如果没有表,创建表;多个校验功能:数据库表字段必须和实体类属性完全一样
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!--
配置数据库方言
mysql做分页使用limit关键字,limit关键字只能使用在mysql里面
oracle使用rownum,只能使用oracle里面
-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 与线程绑定 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 3 引入映射文件 必须-->
<mapping resource="com/hibernate/entity/People.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3、创建实体类与xml文件
package com.hibernate.entity;
import java.io.Serializable;
import java.sql.Blob;
public class People implements Serializable {
/**
*
*/
private static final long serialVersionUID = -2516745270873057482L;
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
private String name;
private Integer age;
private String personIntro;
private Blob personFile;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getPersonIntro() {
return personIntro;
}
public void setPersonIntro(String personIntro) {
this.personIntro = personIntro;
}
public Blob getPersonFile() {
return personFile;
}
public void setPersonFile(Blob personFile) {
this.personFile = personFile;
}
}
<?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 package="com.hibernate.entity">
<class name="People" table="PEOPLE">
<id name="id" column="ID" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="name" column="NAME"/>
<property name="age" column="AGE" type="java.lang.Integer"/>
<property name="personIntro" column="PERSON_INTRO" type="java.lang.String"/>
<property name="personFile" column="PERSON_FILE" type="java.sql.Blob"/>
</class>
</hibernate-mapping>
4、向数据库存储大文件
Session session = HibernateBuilder.getSession();
session.beginTransaction();
People people = new People();
people.setAge(21);
people.setName("孙策");
try {
FileInputStream input = new FileInputStream("1.wav");
Blob blob = Hibernate.getLobCreator(session).createBlob(input,input.available());
people.setPersonFile(blob);
session.save(people);
} catch (IOException e) {
e.printStackTrace();
} finally {
session.getTransaction().commit();
session.close();
}
5、从数据库读取大文件
Session session = HibernateBuilder.getSession();
session.beginTransaction();
Query query = session.createQuery("from People");
List<People> result = query.list();
if(result.size() > 0) {
Blob pf = result.get(0).getPersonFile();
try {
OutputStream output = new FileOutputStream("2.wav");
//将blob格式转换为输入流
InputStream input = pf.getBinaryStream();
byte[] bytes = new byte[1024];
while(input.read(bytes) > 0) {
output.write(bytes, 0, bytes.length);
output.flush();
}
output.close();
input.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
session.getTransaction().commit();
session.close();
测试代码 没有合理try catch