Hibernate學習的第一個例子

來源:互聯網
上載者:User

標籤:

這是本人學習Hibernate的第一個例子:

1,先設定資料庫驅動,點擊myeclipse的myeclipse Database Explore,選擇相應的資料庫,進行配置,完成之後退出。

2,給項目添加支援Hibernate的功能,使用到的jar包有如下的:

 

進行了一系列的選項之後,到sessionfactory這一項,不選。然後點擊完成。

3,配置表到java類的映射。點擊右上方的myeclipse中的myeclipse database expore視圖,選擇資料驅動,選中要操作的表,然後右擊,選擇Hibernate reverse engineering,選擇實體類所在的包,點Next,接著點擊完成即可,這樣就完成了表到java類中的對應映射關係。

4,編寫POJO,封裝資料庫中的表。

package com.entity;

import java.util.Date;

//封裝類
public class User {
public String id;
public String name;
public String password;
public Date createTime;// 建立日期
public Date expireTime;// 到期日

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;
}

public Date getCreateTime() {
return createTime;
}

public void setCreateTime(Date createTime) {
this.createTime = createTime;
}

public Date getExpireTime() {
return expireTime;
}

public void setExpireTime(Date expireTime) {
this.expireTime = expireTime;
}

}

5,編寫DAO,

(1)獲得設定檔 

(2)建立sessionfactory

(3)從sessionfactory中獲得開啟session,一個session對應資料庫一條記錄

(4)開啟session的事務

(5)執行資料庫操作,

(6)提交事務,

(7)之前的最好是使用try catch,這樣,如果有異常的話,這裡可以進行事務的復原

(8)關閉事務。

package com.test;

import java.util.Date;

import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
import org.hibernate.Session;

import com.entity.User;

public class Test {
public static void main(String[] args) {
// 讀取hibernate.cnf.xml設定檔,獲得資料庫的相關配置
Configuration cfg = new Configuration().configure();
// 建立sessionfactory,在設定檔裡面建立獲得sessionfactory
SessionFactory sfactory = cfg.buildSessionFactory();
// 在sessionfactory中取出session,即開啟事務
Session session = null;
try {
for (int i = 0; i < 10000; i++) {
session = sfactory.openSession();
// 開啟事物
session.beginTransaction();
// 通過user.hb.xml檔案,把資料庫的表抽象成為java的類,方便使用對象編程
// 通過封裝的類中的屬性來操作資料庫中表中的欄位
User user = new User();
// 通過setter方法來設定user類中的屬性的值,這樣就間接操作了資料庫中的表的欄位
user.setId("001" + i);
user.setName("xywei" + i);
user.setPassword("1234" + i);
user.setCreateTime(new Date());
user.setExpireTime(new Date());

// 通過儲存user對象的方法相當於插入一條記錄到資料庫裡面
session.save(user);
// 提交事務
session.getTransaction().commit();
}

} catch (Exception e) {
e.printStackTrace();
// 復原事務
session.getTransaction().rollback();

} finally {
if (session != null) {
if (session.isOpen()) {
// 關閉事務
session.close();
System.out.println("====事務已經關閉!====");
}
}
}

}

}

 

Hibernate學習的第一個例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.