Hibernate(2.0)_Hibernate框架的配置

操作平臺

MyEclipse Enterprise Workbench 2015 Stable 2.0
Java Development kit 1.6
Hibernate 4.1.4
MySQL Server 5.7
Navicat Premium 11.2.7
Window10 專業(yè)版 64-bit
(所有文章無特殊說明均在此平臺下操作)

MyEclipse中創(chuàng)建帶Hibernate項目

然后Next->

得到一個正常的Web項目,然后加入Hibernate框架
  在項目上點擊右鍵->Myeclipse->Project Facets [Capabilities]->Install Hibernate Facet

然后再彈出的對話框中開始進行簡單的框架相關(guān)設(shè)置


  選擇默認設(shè)置


  默認設(shè)置之下,需要指定包名,如果已經(jīng)創(chuàng)建了包,點擊Browse選擇已有包


  如果沒有就點擊New進行新建,不再贅述
  然后,如果像筆者這樣使用已經(jīng)搭建好的外部MySQL數(shù)據(jù)庫,此時直接點擊Finish即可完成創(chuàng)建,如果需要使用MyEclipse自帶的數(shù)據(jù)庫創(chuàng)建工具創(chuàng)建數(shù)據(jù)庫則點擊Next進行下一步設(shè)置
  (一般很少有人用MyEclipse自帶工具搭建數(shù)據(jù)庫,所以此處直接跳過如何用自帶工具創(chuàng)建的過程)
  點擊Finish之后彈出是否進入Hibernate設(shè)置的對話框,點擊Cancel,之后我們自己再手動配置
此時可以看到項目目錄里面,Hibernate相關(guān)的庫已經(jīng)被添加到項目中

然后開始配置Hibernate配置文件
  打開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">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
    <!-- 數(shù)據(jù)庫登陸名 -->
    <property name="connection.username">root</property>
    <!-- 數(shù)據(jù)庫登陸密碼 -->
    <property name="connection.password">root</property>
    <!-- 數(shù)據(jù)庫連接URL -->
    <property name="connection.url">
        jdbc:mysql://127.0.0.1:3306/sunjob4hibernate
    </property>
    <!-- 數(shù)據(jù)庫驅(qū)動文件 -->
    <property name="connection.driver_class">
        com.mysql.jdbc.Driver
    </property>
    <!-- 設(shè)置Hibernate方言 -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <!-- 設(shè)置Hibernate顯示sql語句 -->
    <property name="show_sql">true</property>
    
    <!-- 指定pojo映射文件 -->
    <mapping resource="com/pojo/Student.hbm.xml"/>
</session-factory>

</hibernate-configuration>

注:

關(guān)于其中的屬性,前面四個不再贅述
  方言(dialect)
  對于不同的數(shù)據(jù)庫,其中的數(shù)據(jù)庫的操作語句也不同,在Hibernate也要指定不同的配置文件,于是由

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

這個語句來指定,其中支持絕大多數(shù)主流的數(shù)據(jù)庫,具體支持情況如下表(來源截止至2016.9.6官方文檔)

Cache71  Support for the Caché database, version 2007.1
  CUBRID  Support for the CUBRID database, version 8.3. May work with later versions.
  DB2  Support for the DB2 database
  DB2390  Support for DB2 Universal Database for OS/390, also known as DB2/390.
  DB2400  Support for DB2 Universal Database for iSeries, also known as DB2/400.
  DerbyTenFive  Support for the Derby database, version 10.5
  DerbyTenSix  Support for the Derby database, version 10.6
  DerbyTenSeven  Support for the Derby database, version 10.7
  Firebird  Support for the Firebird database
  FrontBase  Support for the Frontbase database
  H2  Support for the H2 database
  HSQL  Support for the HSQL (HyperSQL) database

Informix  Support for the Informix database
  Ingres  Support for the Ingres database, version 9.2
  Ingres9  Support for the Ingres database, version 9.3. May work with newer versions
  Ingres10  Support for the Ingres database, version 10. May work with newer versions
  Interbase  Support for the Interbase database.
  JDataStore  Support for the JDataStore database
  McKoi  Support for the McKoi database
  Mimer  Support for the Mimer database, version 9.2.1. May work with newer versions
  MySQL5  Support for the MySQL database, version 5.x
  MySQL5InnoDB  Support for the MySQL database, version 5.x preferring the InnoDB storage engine when exporting tables.
  MySQL57InnoDB  Support for the MySQL database, version 5.7 preferring the InnoDB storage engine when exporting tables. May work with newer versions
  Oracle8i  Support for the Oracle database, version 8i
  Oracle9i  Support for the Oracle database, version 9i
  Oracle10g  Support for the Oracle database, version 10g
  Pointbase  Support for the Pointbase database
  PostgresPlus  Support for the Postgres Plus database
  PostgreSQL81  Support for the PostgrSQL database, version 8.1
  PostgreSQL82  Support for the PostgreSQL database, version 8.2
  PostgreSQL9  Support for the PostgreSQL database, version 9. May work with later versions.
  Progress  Support for the Progress database, version 9.1C. May work with newer versions.
  SAPDB  Support for the SAPDB/MAXDB database.
  SQLServer  Support for the SQL Server 2000 database
  SQLServer2005  Support for the SQL Server 2005 database
  SQLServer2008  Support for the SQL Server 2008 database
  Sybase11  Support for the Sybase database, up to version 11.9.2
  SybaseAnywhere  Support for the Sybase Anywhere database
  SybaseASE15  Support for the Sybase Adaptive Server Enterprise database, version 15
  SybaseASE157  Support for the Sybase Adaptive Server Enterprise database, version 15.7. May work with newer versions.
  Teradata  Support for the Teradata database
  TimesTen  Support for the TimesTen database, version 5.1. May work with newer versions

具體用法為:

<property name="dialect">org.hibernate.dialect.(這里寫對應(yīng)數(shù)據(jù)庫的名字)</property>

顯示sql語句(show_sql)
  因為Hibernate隱藏了數(shù)據(jù)庫具體語句的實現(xiàn),所以我們當然也看不到隱藏的sql語句,通過設(shè)置這個屬性,可以在控制臺打印出正在使用的sql語句,幫助開發(fā)定位問題,當執(zhí)行相關(guān)增刪改查的操作的時候,相應(yīng)的sql語句就會被打印出來


最后再把使用的數(shù)據(jù)庫驅(qū)動文件加進項目中(我用的是MySQL)


  Hibernate框架配置完成,接下來就可以開始寫小的測試項目了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容