基于hibernate框架在eclipse下的配置方法(必看篇)
一、ORM
O:object 對象
R:Realtion 關(guān)系(關(guān)系型數(shù)據(jù)庫)
M:Mapping 映射
ORM:對象關(guān)系型映射
目前流行的編程語言,如Java、C# ,它們都是面向?qū)ο蟮木幊陶Z言,而目前主流的數(shù)據(jù)庫產(chǎn)品例如Oracle、DB2等,依然是關(guān)系型數(shù)據(jù)庫。編程語言和底層數(shù)據(jù)庫發(fā)展的不協(xié)調(diào)(阻抗不匹配,例如數(shù)據(jù)庫中無法直接實現(xiàn)存儲繼承、多態(tài)、封裝等特征和行為),催生出了ORM框架。ORM框架可以作為面向?qū)ο笳Z言和關(guān)系型數(shù)據(jù)庫之間的橋梁。
二、Hibernate
Hibernate是一個開放源代碼的對象關(guān)系映射框架,它對JDBC進行了非常輕量級的對象封裝,使得Java程序員可以隨心所欲的使用對象編程思維來操縱數(shù)據(jù)庫。
三、hibernate框架在eclipse下的配置方法,這里我們以hibernate3.2為例,介紹一下hibernate3.2在eclipse里的配置方法:
(1)打開eclipse,設(shè)置其工作空間,點擊OK,進入eclipse主界面。
(2)首先我們創(chuàng)建一個java項目,F(xiàn)ile->new->java Project->創(chuàng)建項目名稱,這里我們以ones為例。

(3)導入我們所需要的JAR包,這里我們需要導入3類jar包,首先是hibernate3.jar,是使用hibernate時必備的庫。lib文件中的所有文件。數(shù)據(jù)庫連接jar包,這里以mysql數(shù)據(jù)庫文件,我們需要導入的jar包是mysql.jar。這里我們創(chuàng)建一個用戶自己的類庫,可以將我們的jar包直接導入user library中,當我們再建立其他的項目時,就避免了再重新一個一個的引入jar包。
創(chuàng)建步驟如圖所示:


(4)點擊Add External JARs... 以此導入上述jar包,點擊OK,finish完成操作。此時,項目名下可看到名為first的用戶自定義類庫。
(5)我們在src文件目錄下導入hibernate.cfg.xml文件。這里我們所需要更改的內(nèi)容為第7行,localhost/ones(ones更改為自己的數(shù)據(jù)庫名)
第9行為mysql用戶名,第10行為mysql數(shù)據(jù)庫的密碼。第14行代碼刪掉。
<!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="hibernate.connection.url">jdbc:mysql://localhost/ones</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">88888888</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <mapping resource="com/bjsxt/hibernate/User.hbm.xml"/> </session-factory> </hibernate-configuration>
(6)在src下建立用戶類以及映射文件。Src右鍵->New->Class->選擇類名,這里我們創(chuàng)建名為User的類。
(7)編寫用戶類代碼(這里eclipse支持批量自動寫入set/get方法)點擊Source->Generate Ftters and Setters 選擇全部,導入。User類已經(jīng)編寫完成,接下來我們編寫映射文件。
package ones;
public class User {
private String id;
private String name;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
(8)選擇User.hbm.xml文件,拷入Src文件夾下的包中,文件位置在hiberate\rg\org\hiberate\auction中,這里我們所要修改的代碼是第6行,org.hibernate.auction改為自己項目的包名。第八行代碼,可以只保留<Class name="User">,其余部分可以刪掉。第九行代碼刪掉。將第12行的native刪掉,native是配置整形數(shù)據(jù)的,我們之前設(shè)置的id為字符型,所以這里我們改為uuid,15行至50行,刪掉。在<class>中編寫屬性,屬性值等于User.java中定義的屬性(不包括id)。
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="org.hibernate.auction">
<class name="User" table="AuctionUser" lazy="true">
<comment>Users may bid for or sell auction items.</comment>
<id name="id">
<generator class="native"/>
</id>
<natural-id mutable="true">
<property name="userName"
length="10"/>
</natural-id>
<property name="password"
not-null="true"
length="15"
column="`password`"/>
<property name="email"/>
<component name="name">
<property name="firstName"
length="50"
not-null="true"/>
<property name="initial"
column="`initial`"/>
<property name="lastName"
length="50"
not-null="true"/>
</component>
<bag name="bids"
inverse="true"
cascade="save-update,lock">
<key column="bidder"/>
<one-to-many class="Bid"/>
</bag>
<bag name="auctions"
inverse="true"
cascade="save-update,lock">
<key column="seller"/>
<one-to-many class="AuctionItem"/>
</bag>
</class>
</hibernate-mapping>
(9)編寫后的User.hbm.xml文件如圖所示:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="ones"> <class name="User" > <id name="id"> <generator class="uuid"/> </id> <property name="name"></property> <property name="password"></property> </class> </hibernate-mapping>
(10)編寫導入類,建立名為ExportDB的類,直接產(chǎn)生它的主方法
package ones;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExportDB {
public static void main(String[] args) {
//讀取文件的配置
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(ture, ture);
}
}
(11)修改hibernate.cfg.xml中的第14行代碼,將路徑改為ones(包名)/User.hbm.xml
(12)在mysql數(shù)據(jù)庫 中建立測試表,運行eclipse中的ExportDB文件,右鍵->Run As->java Application

以上這篇基于hibernate框架在eclipse下的配置方法(必看篇)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
IntelliJ IDEA安裝目錄和設(shè)置目錄的說明(IntelliJ IDEA快速入門)
這篇文章主要介紹了IntelliJ IDEA安裝目錄和設(shè)置目錄的說明(IntelliJ IDEA快速入門),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
MybatisPlus EntityWrapper如何自定義SQL
這篇文章主要介紹了MybatisPlus EntityWrapper如何自定義SQL,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
使用Apache?POI和SpringBoot實現(xiàn)Excel文件上傳和解析功能
在現(xiàn)代企業(yè)應(yīng)用開發(fā)中,數(shù)據(jù)的導入和導出是一項常見且重要的功能需求,Excel?作為一種廣泛使用的電子表格工具,常常被用來存儲和展示數(shù)據(jù),下面我們來看看如何使用Apache?POI和SpringBoot實現(xiàn)Excel文件上傳和解析功能吧2025-01-01
詳解spring開發(fā)_JDBC操作MySQL數(shù)據(jù)庫
本篇文章主要介紹了spring開發(fā)_JDBC操作MySQL數(shù)據(jù)庫,具有一定的參考價值,有興趣的可以了解一下。2016-12-12

