Hibernate —— 概述與 HelloWorld,hibernatehelloworld

來源:互聯網
上載者:User

Hibernate —— 概述與 HelloWorld,hibernatehelloworld

一、Hibernate 概述

1.Hibernate 是一個持久化架構

(1)從狹義的角度來講,“持久化” 僅僅指把記憶體中的對象永久的儲存到硬碟中的資料庫中。

(2)從廣義的角度來講,“持久化” 包括和資料庫相關的各種操作。如:CRUD。

2.Hibernate 是一個 ORM 架構

ORM:對象關係映射。O 物件導向:類、對象、屬性。R 面向關係:表、行(記錄)、列(欄位)。M 映射。

ORM 思想:關聯式資料庫中的 行(記錄)映射一個對象,程式員可以把對資料庫的操作轉化為對對象的操作。

在 Hibernate 中,存在 對象關係對應檔用來描述對象與表記錄之間的映射關係。

3.Hibernate 設定檔:hibernate.cfg.xml ,設定資料庫串連、Hibernate 本身的一些資訊、對象關係映射。

4.Entity.hbm.xml 設定檔:對象關係對應檔。

5.Session 介面:Session 是應用程式與資料庫之間互動操作的一個單線程對象,是 Hibernate 運作的中心。Session 有一個一級緩衝,相當於 JDBC 中的 Connection。

6.SessionFactory 介面:根據配置產生 Session 的工廠。

7.Transaction :事務,可以通過 Session 來開啟事務。

二、HelloWorld

(1)在 Intellij Idea 下建立 hibernate.cfg.xml 和根據表建立 實體和 Entity.hbm.xml 檔案已經在前一篇文章中講過了。

(2)產生的 hibernate.cfg.xml 檔案。

<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Configuration DTD//EN"        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory>        <!-- 配置串連資料庫的基本資料 -->        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>        <!-- 配置 Hibernate 的基本資料 -->        <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>        <property name="show_sql">true</property>        <property name="format_sql">true</property>        <!-- 指定自動產生資料表的策略 -->        <property name="hbm2ddl.auto">update</property>        <mapping resource="com/nucsoft/hibernate/News.hbm.xml"/>    </session-factory></hibernate-configuration>

(2)建立的 news 表。

(3)產生的 News 實體和 News.hbm.xml 檔案。

/** * @author solverpeng * @create 2016-09-28-19:36 */public class News { private int id; private String title; private String author; private Date date; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } @Override public boolean equals(Object o) { if(this == o) { return true; } if(o == null || getClass() != o.getClass()) { return false; } News news = (News) o; if(id != news.id) { return false; } if(title != null ? !title.equals(news.title) : news.title != null) { return false; } if(author != null ? !author.equals(news.author) : news.author != null) { return false; } if(date != null ? !date.equals(news.date) : news.date != null) { return false; } return true; } @Override public int hashCode() { int result = id; result = 31 * result + (title != null ? title.hashCode() : 0); result = 31 * result + (author != null ? author.hashCode() : 0); result = 31 * result + (date != null ? date.hashCode() : 0); return result; }}News.java<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping> <class name="com.nucsoft.hibernate.News" table="news" schema="hibernate"> <id name="id"> <column name="id" sql-type="int(11)"/> </id> <property name="title"> <column name="title" sql-type="varchar(255)" not-null="true"/> </property> <property name="author"> <column name="author" sql-type="varchar(50)" length="50" not-null="true"/> </property> <property name="date"> <column name="date" sql-type="date" not-null="true"/> </property> </class></hibernate-mapping>News.hbm.xml

(4)測試

  • 擷取 SessionFactory 對象
  • 通過 SessionFactory 擷取 Session 對象
  • 通過 Session 開啟事務
  • 執行資料庫操作
  • 提交事務
  • 關閉資源
@Testpublic void test() {    // 1.建立 SessionFactory    SessionFactory sessionFactory = null;    Configuration configuration = new Configuration().configure();    ServiceRegistry serviceRegistry =            new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();    sessionFactory = configuration.buildSessionFactory(serviceRegistry);    // 2.建立 Session    Session session = sessionFactory.openSession();    // 3.開啟事務    Transaction transaction = session.beginTransaction();    // 4.執行資料庫操作    News news = new News();    news.setTitle("Title");    news.setAuthor("tom");    news.setDate(new Date(new java.util.Date().getTime()));    session.save(news);    // 5.提交事務    transaction.commit();    // 6.關閉 Session    session.close();    // 7.關閉 SessionFactory    sessionFactory.close();}

說明:

①SessionFactory 對象是通過 Configuration 對象來擷取的,Configuration 的 configure() 方法會預設取讀取類路徑下的 hibernate.cfg.xml 檔案。

從 hibernate.cfg.xml 中讀取的配置會向 ServiceRegistry 中註冊,最終產生的 SessionFactory 對象就是根據 hibernate.cfg.xml 配置產生的。

②事務的開啟:Session 的 beginTransaction() 方法。

③注意資源的關閉。

④事實上,在容器的環境下,我們不需要去關注 SessionFactory 的擷取,資源的關閉。

結果:

三、總結

從第一部分 Hibernate 概述中,確定了學習 Hibernate,要從以下幾個方面來學習:Hibernate的設定檔,對象關係對應檔(主要內容是關聯關係),Session API 以及緩衝,事務控制。

從第二部分 Helloworld 中,體驗了 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.