本专题要讲述,Hibernate环境的搭建,本例使用Hibernate版本为:hibernate-release-4.3.8.Final。本例jar文件,提供下载路径为: http://download.csdn.net/detail/ma_hoking/8380545。需要的读者,可以自行下载使用。
将下载后的文件hibernate-release-4.3.8.Final.zip解压缩,hibernate-release-4.3.8.Final\lib\required目录下的文件,就是环境需要的基本jar包。至此,我们就获得了所需要的jar文件。
创建Java项目Hibernate4Learn,将必须的jar文件导入到项目中。由于本例需要连接本地的MySQL数据库,所以需要引入新的jar文件mysql-connector-java-5.1.7-bin.jar。
【转载使用,请注明出处:http://blog.csdn.net/mahoking】
首先说明,本机环境已经安装MySQL数据库,所以本文就不在着重说明其具体的安装步骤,读者可以参考此篇文章http://blog.csdn.net/mahoking/article/details/42921511。
接下来介绍主配置文件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>
</hibernate-configuration>
配置使用Driver
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/mhc</property>
指定数据库使用的SQL方言
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
指定当程序运行时是否在控制台输出SQL语句。
当show_sql属性为true时,表示在控制台输出SQL语句,默认为false。建议在调试程序时设为true,发布程序之前再改为false,因为输出SQL语句会影响程序的运行速度。
<property name="show_sql">true</property>
配置hbm2ddl.auto,供可选选项有:
create 每次加载hibernate,重新创建数据库表结构
create-drop 加载hibernate时创建,退出是删除表结构
update 加载hibernate自动更新数据库结构
validate 加载hibernate时,验证创建数据库表结构
<property name="hbm2ddl.auto">update</property>
指定对象管理映射文件(*.hbm.xml)
<mapping resource="com/mahaochen/hibernate/domain/User.hbm.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.username">root</property>
<property name="connection.password">root</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/mhc</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/mahaochen/hibernate/domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
主配置文件介绍,接下来我们需要手动编写HibernateUtil类,来获取SessionFactory对象,并管理Session,如开启与关闭Session的操作。首先在此说明,Hibernate3与Hibernate4获取SessionFactory的方式有所不同。由于Configuration的buildSessionFactory()方法在Hibernate4已经过时,而是用了Configuration.buildSessionFactory(ServiceRegistry serviceRegistry)方法。下面依次介绍Hibernate3Util和Hibernate4Util的具体实现。
Hibernate3Util
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* 供Hibernate V3版 使用
*
* @author mahc
*
*/
public class Hibernate3Util {
private static SessionFactory sessionFactory;
private static Session session;
static {
// 创建Configuration,该对象用于读取hibernate.cfg.xml,并完成初始化
Configuration config = new Configuration();
config.configure();
sessionFactory = config.buildSessionFactory();
}
/**
* 获取SessionFactory
*
* @return
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Session getCurrentSession() {
session = sessionFactory.openSession();
return session;
}
public static void closeSession(Session session) {
if (null != session) {
session.close();
}
}
}
Hibernate4Util
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
/**
* 供Hibernate V4.3.8版 使用
*
* @author mahc
*
*/
public class Hibernate4Util {
private static SessionFactory sessionFactory;
private static Session session;
static {
// 创建Configuration,该对象用于读取hibernate.cfg.xml,并完成初始化
Configuration config = new Configuration().configure();
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder()
.applySettings(config.getProperties());
StandardServiceRegistry ssr = ssrb.build();
sessionFactory = config.buildSessionFactory(ssr);
}
/**
* 获取SessionFactory
*
* @return
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Session getCurrentSession() {
session = sessionFactory.openSession();
return session;
}
public static void closeSession(Session session) {
if (null != session) {
session.close();
}
}
}
【转载使用,请注明出处:http://blog.csdn.net/mahoking】