国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

hibernate實(shí)踐

2019-11-18 13:56:05
字體:
供稿:網(wǎng)友

  hiberante是對(duì)數(shù)據(jù)庫持久層訪問的一種機(jī)制,hibernate的應(yīng)用可以使程序員將重點(diǎn)放到業(yè)務(wù)邏輯的實(shí)現(xiàn)上。hibernate的原理是將數(shù)據(jù)庫結(jié)構(gòu)封裝,使程序員可以像使用普通對(duì)象一樣調(diào)用數(shù)據(jù)庫的相關(guān)接口,從實(shí)現(xiàn)數(shù)據(jù)庫的相關(guān)操作。
下面就說一個(gè)例子,環(huán)境j2sdk1.4.2_04,tomcat4.1.27,eclipse3.0,hibernate2.1
數(shù)據(jù)庫的結(jié)果如下:

create table Users (
LogonID VARCHAR(255) not null,
EmailAddress VARCHAR(255),
LastLogon DATE,
PassWord VARCHAR(255),
Name VARCHAR(255),
PRimary key (LogonID)
);

create table Address2 (
ID VARCHAR(255) not null,
City VARCHAR(255),
State VARCHAR(255),
Zip VARCHAR(255),
primary key (ID)
);
create table Contacts (
ID BIGINT not null,
EmailAddress VARCHAR(255),
Name VARCHAR(255),
User_ID VARCHAR(255),
primary key (ID)
);
alter table Contacts add constraint FKE207C4735A7381EF foreign key (User_ID) references Users;
create index IXE207C4735A7381EF on Contacts (User_ID);

eclipse安裝好hibernate插件后,許多比較繁瑣,輕易出錯(cuò)的配置文件,eclipse都可以幫您很好的自動(dòng)生成,您的主要任務(wù)就是實(shí)現(xiàn)Dao,也就是eclipse針對(duì)每個(gè)表生成的數(shù)據(jù)訪問對(duì)象。比如本例的BaseAddress2DAO,ContactsDAO,UsersDAO,你可以修改這三個(gè)對(duì)象實(shí)現(xiàn)對(duì)Contacts,Users,Address2的相關(guān)操作。該例子的結(jié)構(gòu)如下:


其中Address2,Contacts,Users相當(dāng)于實(shí)體bean,實(shí)現(xiàn)對(duì)數(shù)據(jù)表的映射。由于表Users和Contacts存在one-to-many關(guān)系,所以在Users.java的實(shí)現(xiàn)中將Contacts也作為一個(gè)對(duì)象屬性。
private java.util.Set _contactsSet;
/**
* Return the value associated with the column: ContactsSet
*/
public java.util.Set getContactsSet () {
return this._contactsSet;
}

/**
* Set the value related to the column: ContactsSet
* @param _contactsSet the ContactsSet value
*/
public void setContactsSet (java.util.Set _contactsSet) {
this._contactsSet = _contactsSet;
}

public void addToContactsSet (Object obj) {
if (null == this._contactsSet) this._contactsSet = new java.util.HashSet();
this._contactsSet.add(obj);
}
一般情況下,hibernate實(shí)現(xiàn)數(shù)據(jù)庫的連接有兩種方式,一種是借助web服務(wù)器的連接池,一種是自己配置連接參數(shù)文件。這里只介紹第二種方式:
<hibernate-configuration>
<session-factory>
<!-- local connection properties -->
<!--數(shù)據(jù)庫路徑-->
<property name="hibernate.connection.url">
jdbc:jtds:sqlserver://192.198.64.168:1433;databasename=test;SelectMethod=Cursor
</property>
<!--連接驅(qū)動(dòng)-->
<property name="hibernate.connection.driver_class">
net.sourceforge.jtds.jdbc.Driver
</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">sasa</property>
<!-- property name="hibernate.connection.pool_size"></property -->
<!-- 數(shù)據(jù)庫方言 -->
<property name="dialect">
net.sf.hibernate.dialect.SQLServerDialect
</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.use_outer_join">true</property>
<!-- mapping 文件-->
<mapping resource="com/head/multi/Contacts.hbm" />
<mapping resource="com/head/multi/Users.hbm" />
<mapping resource="com/head/multi/Address2.hbm" />
</session-factory>
</hibernate-configuration>

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 长治县| 巍山| 安岳县| 库车县| 保德县| 剑河县| 偏关县| 昌乐县| 沂南县| 沾化县| 双流县| 丹凤县| 忻州市| 阿勒泰市| 寻甸| 紫云| 定西市| 虹口区| 华容县| 南投市| 东乡| 禹城市| 南宁市| 沈丘县| 遂昌县| 三门峡市| 鹰潭市| 会同县| 芦山县| 郯城县| 延边| 茂名市| 郓城县| 龙胜| 常州市| 安龙县| 中卫市| 柞水县| 榕江县| 潞西市| 措美县|