標籤:hibernate jdbc mysql session sessionfactory
本專題要講述,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】
Hibernate4環境搭建和HibernateUtil