1. 程式人生 > >Hibernate下載與環境搭建教程

Hibernate下載與環境搭建教程

作者:叄念

注意:觀看本教程需要先設定好JAVA開發環境以及MySQL資料庫,如果你還沒有該環境,可以參考以下文章

1.Hibernate是什麼?

下列是來自百度百科的解釋:

  • Hibernate (開放原始碼的物件關係對映框架)
  • Hibernate是一個開放原始碼的物件關係對映框架,它對JDBC進行了非常輕量級的物件封裝,它將POJO
    與資料庫表建立對映關係,是一個全自動的orm(物件關係對映)框架,hibernate可以自動生成SQL語句,自動執行,使得Java程式設計師可以隨心所欲的使用物件程式設計思維來操縱資料庫。
  • Hibernate可以應用在任何使用JDBC的場合,既可以在Java的客戶端程式使用,也可以在Servlet/JSP的
    Web應用中使用,最具革命意義的是,Hibernate可以在應用EJB的J2EE架構中取代CMP,完成資料持久化的重任。

當然,對於初學者來說以上概念可能很難理解,不要緊,通俗的將我們可以將之理解為如下:

  • 是用來幫我們完成資料庫操作
  • Hibernate它是一個輕量級的jdbc封裝,也就是說,我們可以使用hibernate來完成原來我們使用jdbc完成操
    作,就是與資料庫的互動操作(增刪查改等)。
  • 它是在dao層去使用的。
  • 使用 hibernate的好處是:操作資料庫的時候,可以以面向物件的方式來完成.不需要書寫SQL語句,而是使用配置
    檔案如xml或者其他一些手段,將物件的資訊和資料庫中的表進行對應,完成操作
  • 可以說Hibernate是完全面向物件操作資料庫

2.Hibernate的組成?

主要由三部分組成:

  • 持久化類
  • 對映檔案:用來描述類與表之間的關係,資料庫有多少張表,至少應該有多少個對映檔案 xxx.hbm.xml
  • 配置檔案:完成資料庫連線資訊的填寫,一般只有一個 hibernate.cfg.xml

3.Hibernate框架下載

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

至此,我們應該下載了以下三個檔案:

這裡寫圖片描述

2.Hibernate的環境搭建與簡單示例

這裡我們先用普通java專案作為案例,具體步驟如下:

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

接下來我們需要建立一個數據庫以及對應的表格,具體如下:

這裡寫圖片描述

CREATE TABLE `cst_customer`
( `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客戶編號(主鍵)', `cust_name` varchar(32) NOT NULL COMMENT '客戶名稱(公司名稱)', `cust_user_id` bigint(32) DEFAULT NULL COMMENT '負責人id', `cust_create_id` bigint(32) DEFAULT NULL COMMENT '建立人id', `cust_source` varchar(32) DEFAULT NULL COMMENT '客戶資訊來源', `cust_industry` varchar(32) DEFAULT NULL COMMENT '客戶所屬行業', `cust_level` varchar(32) DEFAULT NULL COMMENT '客戶級別', `cust_linkman` varchar(64) DEFAULT NULL COMMENT '聯絡人', `cust_phone` varchar(64) DEFAULT NULL COMMENT '固定電話', `cust_mobile` varchar(16) DEFAULT NULL COMMENT '行動電話', PRIMARY KEY (`cust_id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

繼續回到我們的eclipse,來建立實體類:

這裡寫圖片描述

實體類程式碼如下:

package com.bean;

public class Customer {
    /** 客戶編號(主鍵) */
    private long cust_id;
    /** 客戶名稱(公司名稱) */
    private String cust_name;
    /** 負責人id */
    private long cust_user_id;
    /** 建立人id */
    private long cust_create_id;
    /** 客戶資訊來源 */
    private String cust_source;
    /** 客戶所屬行業 */
    private String cust_industry;
    /** 客戶級別 */
    private String cust_level;
    /** 聯絡人 */
    private String cust_linkman;
    /** 固定電話 */
    private String cust_phone;
    /** 行動電話 */
    private String cust_mobile;

    public long getCust_id() {
        return cust_id;
    }

    public void setCust_id(long cust_id) {
        this.cust_id = cust_id;
    }

    public String getCust_name() {
        return cust_name;
    }

    public void setCust_name(String cust_name) {
        this.cust_name = cust_name;
    }

    public long getCust_user_id() {
        return cust_user_id;
    }

    public void setCust_user_id(long cust_user_id) {
        this.cust_user_id = cust_user_id;
    }

    public long getCust_create_id() {
        return cust_create_id;
    }

    public void setCust_create_id(long cust_create_id) {
        this.cust_create_id = cust_create_id;
    }

    public String getCust_source() {
        return cust_source;
    }

    public void setCust_source(String cust_source) {
        this.cust_source = cust_source;
    }

    public String getCust_industry() {
        return cust_industry;
    }

    public void setCust_industry(String cust_industry) {
        this.cust_industry = cust_industry;
    }

    public String getCust_level() {
        return cust_level;
    }

    public void setCust_level(String cust_level) {
        this.cust_level = cust_level;
    }

    public String getCust_linkman() {
        return cust_linkman;
    }

    public void setCust_linkman(String cust_linkman) {
        this.cust_linkman = cust_linkman;
    }

    public String getCust_phone() {
        return cust_phone;
    }

    public void setCust_phone(String cust_phone) {
        this.cust_phone = cust_phone;
    }

    public String getCust_mobile() {
        return cust_mobile;
    }

    public void setCust_mobile(String cust_mobile) {
        this.cust_mobile = cust_mobile;
    }

}

接下來書寫orm元資料(物件與表的對映配置檔案 - XML格式),其格式為—— 類名.hbm.xml
這裡寫圖片描述

類名.hbm.xml檔案內容如下:

<?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.bean.Customer" table="cst_customer">
        <id name="cust_id" column="cust_id">
            <generator class="native"></generator>
        </id>
        <property name="cust_name"></property>
        <property name="cust_user_id"></property>
        <property name="cust_create_id"></property>
        <property name="cust_source"></property>
        <property name="cust_industry"></property>
        <property name="cust_level"></property>
        <property name="cust_linkman"></property>
        <property name="cust_phone"></property>
        <property name="cust_mobile"></property>
    </class>
</hibernate-mapping>

類名.hbm.xml檔案屬性解釋:

<?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">
<!-- 配置表與實體的關係 -->
<!-- package屬性:填寫一個包名,在元素內部凡是需要書寫完整類名的屬性,就可以直接寫簡單類名 -->
<hibernate-mapping package="">
    <!-- 
        class元素:配置實體與表的關係
            name:完整類名
            table:資料庫表名
     -->
    <class name="" table="">
        <!-- 
            id元素:配置主鍵對映的屬性
                name:填寫主鍵對應的屬性名
                column(可選):填寫表中的主鍵列名,預設值:屬性名
                type(可選):填寫列(屬性)的型別,hibernate會自動檢測屬性型別
                    每個屬性型別有三種寫法:java|hibernate|sql|
                not-null(可選):配置該屬性是否不能為空,預設值:fasle
                length(可選):配置資料庫中列的長度,預設值使用的資料型別的最大長度
         -->
        <id name="" column="">
            <!-- generator:主鍵生成策略 -->
            <generator class="native"></generator>
        </id>

        <!-- 
            property:除了id之外的普通屬性對映
         -->
        <property name="cust_name"></property>

    </class>

</hibernate-mapping>

接下來書寫主配置檔案,它是hibernate框架核心配置檔案。[核心配置檔案],其格式為——hibernate.cfg.xml

類名.hbm.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">
<hibernate-configuration>
    <!-- 
    書寫方式可以參照:hibernate-release-5.0.7.Final\project\etc\hibernate.properties 
    -->
    <session-factory>
        <!-- 資料庫驅動 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- 資料庫url -->
        <property name="hibernate.connection.url">jdbc:mysql:///Hibernate</property>
        <!-- 資料庫連線使用者名稱 -->
        <property name="hibernate.connection.username">root</property>
        <!-- 資料庫連線密碼 -->
        <property name="hibernate.connection.password">183686</property>
        <!-- 資料庫方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- 把hibernate執行sql語句列印到控制檯 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 把生成的sql格式化一下,方便閱讀  -->
        <property name="hibernate.format_sql">true</property>


        <!--    自動建表
                #hibernate.hbm2ddl.auto create-drop     自動建表,每次框架執行會先刪除之前的表,再建表,。最後會把表也刪除
                #hibernate.hbm2ddl.auto create          自動建表,每次框架執行會先刪除之前的表。最後資料會保留
                #hibernate.hbm2ddl.auto update          (推薦使用)不會建表,如果已存在才會進行操作,不存在也不會報錯
                #hibernate.hbm2ddl.auto validate        驗證,如果語法輸入錯誤會報錯!
         -->
        <property name="hibernate.hbm2ddl.auto">update</property>

        <!-- 
            引入ORM元資料配置,即載入對映檔案
            路徑填寫:src目錄下路徑
         -->
        <mapping resource="com/bean/Customer.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

請將結構改為如下:
這裡寫圖片描述
書寫程式碼測試:

package com.test;

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

import com.bean.Customer;

public class Test {
    public static void main(String[] args) {
        // 儲存一個數據到資料庫
        Configuration conf = new Configuration().configure();
        SessionFactory sessionFactory = conf.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        // -------------------------------------------
        Customer customer = new Customer();
        customer.setCust_name("SanNian");
        session.save(customer);
        // -------------------------------------------
        transaction.commit();
        session.close();
        sessionFactory.close();
    }
}

執行結果演示:

這裡寫圖片描述

這裡寫圖片描述

接下來繼續看一下如何實現增刪查改:

    public static void main(String[] args) {
        // 1.建立配置物件
        Configuration conf = new Configuration().configure();
        // 2.session工廠
        SessionFactory sessionFactory = conf.buildSessionFactory();
        // 3.獲得session
        Session session = sessionFactory.openSession();

        // 獲得事務
        // session.getTransaction();//獲得一個事務物件
        Transaction transaction = session.beginTransaction();// 獲得並啟動一個事務(推薦)

        // --------------------------
        // 1.增加
        Customer customer = new Customer();
        customer.setCust_name("yujie");
        session.save(customer);

        //2.查
        Customer customer1 = session.load(Customer.class, 2l);
        System.out.println(customer1.getCust_name());
        //3.改
        Customer customer2 = session.load(Customer.class, 2l);
        customer2.setCust_name("temp");
        session.update(customer2);
        //4.刪
        Customer customer3 = session.load(Customer.class, 2l);
        session.delete(customer3);

        // --------------------------
        transaction.commit(); // 事務提交
//      transaction.rollback(); // 事務回滾
        session.close();
        sessionFactory.close();
    }

相關推薦

Hibernate下載環境搭建教程

作者:叄念 注意:觀看本教程需要先設定好JAVA開發環境以及MySQL資料庫,如果你還沒有該環境,可以參考以下文章 1.Hibernate是什麼? 下列是來自百度百科的解釋: Hibernate (開放原始碼的物件關係對映框架) Hiberna

Android studio開發環境搭建教程軟體安裝教程(從零開始學android)

學習Android開發的第一步是java環境搭建和android studio軟體的安裝。本文主要講解如何從零開始學android。 Android程式開發用的是java語言,所以我們要先在電腦上配置jdk(java development kit)環境,也即java開

PyQT5速成教程-1 簡介環境搭建

本文由 沈慶陽 所有,轉載請與作者取得聯絡! PyQt簡介 一個良好的介面是人機互動中十分重要的一環。 Python作為指令碼語言,起初並未擁有GUI開發的部分。但隨著其開放的擴充套件性,使得Python不斷壯大,有PyQt、Tkinter,PyGUI

mybatis介紹環境搭建

mybatis一、不用純jdbc的原因,即缺點。1、數據庫理解,使用時創建,不用時釋放,會對數據庫進行頻繁的鏈接開啟和關閉,造成數據庫的資源浪費,影響數據庫的性能。設想:使用數據庫的連接池。2、將sql語句硬編碼到java代碼中,不利於系統維護。設想:將sql放到配置文件中。3、向preparedstatem

RabbitMQ環境搭建教程收集(待實踐)

net zhang logs http unix 教程 .cn share blog 先收集,後續再實踐。 http://blog.csdn.net/zyz511919766/article/details/41896823 http://blog.chinaunix.

第一章 Linux系統介紹環境搭建準備

桌面 tcp 伸縮性 搭建 應用程序 另一個 核心 x86 pan   1、操作系統:   Operating System,簡稱OS,它是應用程序運行以及用戶操作必備的基礎環境支撐,是計算機系統的核心。   操作系統就是處於用戶與計算機系統硬件之間用於傳遞信息的系統程序軟

iOS項目——微信H5棋牌牛牛鬥牛出租平臺項目開發環境搭建教程

兩個 char 啟動圖 導致 主循環 window 還需 board 詳解 在開發項目之前,我們需要做一些準備工作,了解iOS擴展——微信H5棋牌牛牛鬥牛出租平臺(h5.fanshubbs.com聯系Q_1687054422),學習iOS學習——Xcode9上傳項目到Git

sybase數據庫環境搭建教程

lang set 增加 搭建 密碼 local 數據 解壓 glibc 本案例為centos6.5操作系統,sybase數據庫版本為15.7。安裝操作系統不做詳解裝完之後在root下配置IP,改為動態獲取,使其能鏈接網絡vi /etc/sysconfig/network-s

持續集成-jenkins介紹環境搭建

erl 地址 作業 工作流 var pull 腳本 由於 一次 什麽是持續集成?   轉自:https://blog.csdn.net/tanshizhen119/article/details/80328523   持續集成,俗稱CI, 大師Martin Fowler對持

Smobiler資料準備環境搭建——C# 或.NET Smobiler例項開發手機app(一)

目錄 一、 前言 二、 關於"選擇" 三、 資料準備 1、 Smobiler介紹 2、 三款開源軟體原始碼下載 3、 控制元件使用例項 四、 環境搭建到實現HelloWorld 1、 安裝VS2015及以上 2、 安裝Designer 3、 實現Hello

Python3.5開發1 - 基本型別環境搭建

1 Python基本型別與環境搭建 知識點: Python3.5環境搭建 基本型別 型別轉換 演示: Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:3

簡單的vscode C++編譯環境搭建教程

vscode & C++ 參考自:https://www.zhihu.com/question/30315894 請確保您能看懂Visual Studio Code如何編寫執行C、C++?這個連結,本文是基於該文章並且進行簡化操作。 這裡使用了llvm+ming

JAVA WEB環境搭建教程

需要的軟體安裝包下載地址:連結:https://pan.baidu.com/s/1po2YlEXLHYMTYU_-mK21aw 提取碼:t4ep 一、安裝JDK(若已經安裝配置好環境變數的的可以忽略此步) (1) 雙擊執行jdk安裝檔案 (2)直接下一步,等待安裝完成後關閉

linux基本介紹環境搭建

1.1 Linux版本 linux kernel 2.2、2.4、2.6、3.0、4.0...發行商:Red hat、CentOS、Ubuntu選擇適合的Linux系統學習桌面系統:Ubuntu穩定伺服器系統:CentOS土豪(有售後):Red Hat對安全性有更高要求:Debian、FreeBSD新技術,

MySQL資料庫下載安裝詳細教程

https://blog.csdn.net/qq_36868342/article/details/78816740 這個博主寫的非常詳細。按照她的流程走差不多就很OK了。 最後我出現了一個access denied for user root @localhost這個錯誤。 解決方

Django簡單介紹環境搭建

Django:MVC(model:主要封裝對資料庫層的訪問,對資料庫中的資料進行增刪改查操作。view:用於封裝結果,生成頁面展示的html內容。controller:用於接受請求請求,處理業務邏輯,與Model和View互動,返回結果)核心思想:解耦,讓不同的程式碼塊之間降低耦合,增強程式

[work] 最爽的GPU深度學習環境搭建教程

背景介紹 GPU 在以下方面有別於 CPU: CPU和GPU之所以大不相同,是由於其設計目標的不同,它們分別針對了兩種不同的應用場景。CPU需要很強的通用性來處理各種不同的資料型別,同時邏輯判斷又會引入大量的分支、跳轉和中斷的處理。這些都使得CPU的內部結構異常複雜。而GPU面對的則是

安卓開發環境搭建教程

此教程以32位win7旗艦版系統為基礎,其它版本系統請根據情況自行調整。教程在2015年1月05日整理;  基礎知識:      安裝虛擬機器的過程,就是我們裝街機模擬器的過程。一.設定classpath:去哪裡找需要執行的class檔案(jav

mysql8.0.13下載安裝圖文教程

一、進入mysql網站:https://dev.mysql.com/downloads/mysql/ 二、進入Community選擇MySQL Communtiy Server 三、將頁面拉到最下面選擇選擇作業系統後,選擇要下載的版本點選 Downloads      

Windows Server 2008 R2 IIS7.5+PHP5(FastCGI)+MySQL5環境搭建教程

有個別的版本可替換 準備篇 一、環境說明: 作業系統:Windows Server 2008 R2 PHP版本:php 5.4.4 MySQL版本:MySQL5.5.25 二、相關軟體下載: 1、PHP下載地址: http://windows.php.net/down