Hibernate是一个开放源码的、非常优秀、成熟的O/R Mapping框架。它提供了强大、高性能的Java对象和关系数据的持久化和查询功能。
Hibernate 只是一个将持久化类与数据库表相映射的工具,每个持久化类实例均对应于数据库表中的一条数据行。可以使用面向对象的方法操作此持久化类实例,完成对数据库表的插入、删除、修改等操作。
利用Hibernate操作数据库,我们通过应用程序经过Hibernate持久层来访问数据库,其实Hibernate完成了以前JDBC的功能,不过Hibernate使用面向对象的方法操作数据库。
Hibernate入门示例:
第1步: 先建一个Java工程导入使用Hibernate最小必要包。可以到网站下载Hibernate最新的包,如果访问数据库,则需要导入数据库驱动包。
第2步:在src创建配置文件hibernate.cfg.xml,放置在src目录中。
第3步:编写一个会话工厂类。通过会话工厂类产生一个会话Session对象。Session对象是Hibernate的核心。任何对数据库操作都在会话中进行的。
第4步:编写POJO类以及映射文件(两者在同一文件夹下)。
第5步:编写测试文件
hibernate.cfg.xml文件如下:
<span style="color:#6666cc;"><?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://127.0.0.1:3306/hib
</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<mapping resource="cn/hncu/domain/Student.hbm.xml"/>
<mapping resource="cn/hncu/domain/Dept.hbm.xml"/>
</session-factory>
</hibernate-configuration></span>
Dept.hbm.xml
<span style="color:#6666cc;"><?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.hncu.domain">
<class name="Dept" table="depts" catalog="hib">
<id name="deptId" type="java.lang.String">
<column name="id" length="8"></column>
</id>
<property name="deptName" type="java.lang.String">
<column name="name" length="30"></column>
</property>
<set name="students" table="students" inverse="true" cascade="all">
<key >
<column name="dept" length="8"></column>
</key>
<one-to-many class="Student" />
</set>
</class>
</hibernate-mapping></span>
Student.hbm.xml
<span style="color:#6666cc;"><?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.hncu.domain">
<class name="Student" table="students" catalog="hib">
<id name="studId" type="java.lang.String">
<column name="id" length="8"></column>
</id>
<property name="studName" type="java.lang.String">
<column name="name" length="30"></column>
</property>
<property name="age" type="java.lang.Integer">
<column name="age" />
</property>
<many-to-one name="dept" class="Dept" fetch="select">
<column name="dept" length="8"></column>
</many-to-one>
</class>
</hibernate-mapping></span>
HibernateSessionFactory类核心代码:
<span style="color:#3366ff;">import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static final String configFile = "/hibernate.cfg.xml";
private static Configuration config = new Configuration();
private static SessionFactory sessionFactory = null;
private static final ThreadLocal<Session> t = new ThreadLocal<Session>();
static {
try {
config.configure(configFile);
sessionFactory = config.buildSessionFactory();
} catch (HibernateException e) {
e.printStackTrace();
}
}
public static Session getSession() throws HibernateException {
Session session = t.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory!=null)?sessionFactory.openSession():null;
t.set(session);
}
return session;
}
private static void rebuildSessionFactory() {
try {
config.configure(configFile);
sessionFactory = config.buildSessionFactory();
} catch (HibernateException e) {
e.printStackTrace();
}
}
public static void close(){
Session session = t.get();
t.set(null);
if(session!=null && session.isOpen()){
session.close();
}
}
}</span><span style="color:#006600;">
</span>