hibernate框架學習筆記1:搭建與測試

来源:https://www.cnblogs.com/xuyiqing/archive/2018/02/13/8445760.html
-Advertisement-
Play Games

hibernate框架屬於dao層,類似dbutils的作用,是一款ORM(對象關係映射)操作 使用hibernate框架好處是:操作資料庫不需要寫SQL語句,使用面向對象的方式完成 這裡使用eclipse工具搭建: 官網下載:https://sourceforge.net/projects/hib ...


hibernate框架屬於dao層,類似dbutils的作用,是一款ORM(對象關係映射)操作

使用hibernate框架好處是:操作資料庫不需要寫SQL語句,使用面向對象的方式完成

 

這裡使用eclipse工具搭建:

官網下載:https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/

下載.zip文件後解壓:

 

lib文件夾下的required文件夾內的jar包為必須包:

 

另外還需要MySQL的驅動包:

mysql-connector-java-5.1.7-bin.jar

 

放入WEB-INFO的lib中:

 

 

導入包完成:

 

導入約束:

 

複製這裡的dtd文件以及文件中的這一行:

 

上邊是文件路徑,下邊粘貼複製的那一行

 

 

資料庫創建一個表,做示例:

CREATE TABLE `cst_customer` (
      `cust_id` BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT '客戶編號(主鍵)',
      `cust_name` VARCHAR(32) NOT NULL COMMENT '客戶名稱(公司名稱)',
      `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;

 

src下新建一個domain實體包:

Customer類:

package domain;

public class Customer {
    
    private Long cust_id;
    private String cust_name;
    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 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;
    }
    @Override
    public String toString() {
        return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + "]";
    }
}
View Code

 

在domain包中,需要配置文件:Customer.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="domain.Customer" table="cst_customer" >
        <id name="cust_id"  >
            <!-- generator:主鍵生成策略 -->
            <generator class="native"></generator>
        </id>
        <property name="cust_name" column="cust_name" ></property>
        <property name="cust_source" column="cust_source" ></property>
        <property name="cust_industry" column="cust_industry" ></property>
        <property name="cust_level" column="cust_level" ></property>
        <property name="cust_linkman" column="cust_linkman" ></property>
        <property name="cust_phone" column="cust_phone" ></property>
        <property name="cust_mobile" column="cust_mobile" ></property>
    </class>
</hibernate-mapping>

 

 

 核心配置文件:src目錄下:

hibernate.cfg.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>
    <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">xuyiqing</property>
        
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- 將hibernate生成的sql語句列印到控制台 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 將hibernate生成的sql語句格式化(語法縮進) -->
        <property name="hibernate.format_sql">true</property>
        
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 引入orm元數據 路徑書寫: 填寫src下的路徑 -->
        <mapping resource="domain/Customer.hbm.xml" />

    </session-factory>
</hibernate-configuration>

 

好,到這裡配置文件完成:

書寫代碼測試:

package test;

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

import domain.Customer;

//測試Hibernate框架
public class Demo {
    
    @Test
    //保存
    public void function1(){
        Configuration conf = new Configuration().configure();
        SessionFactory sessionFactory = conf.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        
        Customer customer = new Customer();
        customer.setCust_name("騰訊");
        session.save(customer);
        
        tx.commit();
        session.close();
        sessionFactory.close();
    }
}

 

結果:

 

查看資料庫,成功添加一條新數據

 

搭建測試完畢

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一、前言 這章非常重要,由於之後需要負責平臺手機APP的日後維護,如何讓用戶在離線狀態下正常使用,以及聯網後的數據合併變得非常重要。 二、內容 離線檢測 數據存儲 ...
  • Factory負責處理生命周期的開始,而Repository幫助管理生命周期的中間和結束。 通俗的來說,Factory用於創建一個對象的新的實例,而Repository用於從資料庫中查找數據。 ...
  • 協程 協程又稱為微線程,協程是一種用戶態的輕量級線程 協程擁有自己的寄存器和棧。協程調度切換的時候,將寄存器上下文和棧都保存到其他地方,在切換回來的時候,恢復到先前保存的寄存器上下文和棧,因此:協程能保留上一次調用狀態,每次過程重入時,就相當於進入上一次調用的狀態。 協程的好處: 1.無需線程上下文 ...
  • 記憶體限制:256 MiB時間限制:500 ms標準輸入輸出 題目類型:傳統評測方式:文本比較 上傳者: hzwer 記憶體限制:256 MiB時間限制:500 ms標準輸入輸出 題目類型:傳統評測方式:文本比較 上傳者: hzwer 提交提交記錄統計討論 1 測試數據 題目描述 給出一個長為 nnn  ...
  • 在最早學習四則運算的過程中,我們其實就已經掌握了進位演算法,這一次我將對二進位運用這個進位演算法來實現四則運算。 四則運算 math.c 從遞歸角度看待代碼 遞歸是函數調用,考慮值的傳遞過程,適合閱讀。 迭代是具體的實現過程,往往代碼效率更加充分。 通常我們在寫代碼時,往往註重代碼的效率和正確性,而忽略 ...
  • jdk1.8.0_144 Object類作為Java中的頂級類,位於java.lang包中。所有的類直接或者間接都繼承自它。所以Object類中的方法在所有類中都可以直接調用。在深入介紹它的API時,先插一句它和泛型之間的關係。 在JDK1.5之前是沒有泛型的,集合能夠裝下任意的類型,這就導致了一個 ...
  • 這是我的坦克游戲大戰的最後一版,裡面添加很多新的功能。這個坦克大戰的有很多不足之處,但是對於初學者來說依然是一個很好的練習項目,從中我們可以學習Java基礎知識,將知識與項目結合,學習面向對象編程思想,最主要的是編程的邏輯練習,代碼往往不像是寫文章從上到下一氣呵成完成,中間很可能為增加一個功能來添加 ...
  • 實體類: package domain; public class Customer { private Long cust_id; private String cust_name; private String cust_source; private String cust_industry; ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...