hibernate框架學習筆記2:配置文件詳解

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

實體類: package domain; public class Customer { private Long cust_id; private String cust_name; private String cust_source; private String cust_industry; ...


實體類:

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

 

 

ORM元數據配置文件:

<?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="domain" >
    <!-- 
           class元素: 配置實體與表的對應關係的
             name: 完整類名
             table:資料庫表名
     -->
    <class name="Customer" table="cst_customer" >
        <!-- id元素:配置主鍵映射的屬性
                name: 填寫主鍵對應屬性名
                column(可選): 填寫表中的主鍵列名.預設值:列名會預設使用屬性名
                type(可選):填寫列(屬性)的類型.hibernate會自動檢測實體的屬性類型.
                        每個類型有三種填法: java類型|hibernate類型|資料庫類型
                not-null(可選):配置該屬性(列)是否不能為空. 預設值:false
                length(可選):配置資料庫中列的長度. 預設值:使用資料庫類型的最大長度
         -->
        <id name="cust_id"  >
            <!-- generator:主鍵生成策略(後邊介紹) -->
            <generator class="native"></generator>
        </id>
        <!-- property元素:除id之外的普通屬性映射
                name: 填寫屬性名
                column(可選): 填寫列名
                type(可選):填寫列(屬性)的類型.hibernate會自動檢測實體的屬性類型.
                        每個類型有三種填法: java類型|hibernate類型|資料庫類型
                not-null(可選):配置該屬性(列)是否不能為空. 預設值:false
                length(可選):配置資料庫中列的長度. 預設值:使用資料庫類型的最大長度
         -->
        <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>

 

 

核心配置文件:

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>
        <!-- 資料庫方言
            不同的資料庫中,sql語法略有區別. 指定方言可以讓hibernate框架在生成sql語句時.針對資料庫的方言生成.
            sql99標準: DDL 定義語言  庫表的增刪改查
                      DCL 控制語言  事務 許可權
                      DML 操縱語言  增刪改查
            註意: MYSQL在選擇方言時,請選擇最短的方言.
         -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        
        
        <!-- #hibernate.show_sql true 
             #hibernate.format_sql true
        -->
        <!-- 將hibernate生成的sql語句列印到控制台 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 將hibernate生成的sql語句格式化(語法縮進) -->
        <property name="hibernate.format_sql">true</property>
        <!-- 
        ## auto schema export  自動導出表結構. 自動建表
        #hibernate.hbm2ddl.auto create        自動建表.每次框架運行都會創建新的表.以前表將會被覆蓋,表數據會丟失.(開發環境中測試使用)
        #hibernate.hbm2ddl.auto create-drop 自動建表.每次框架運行結束都會將所有表刪除.(開發環境中測試使用)
        #hibernate.hbm2ddl.auto update(推薦使用) 自動生成表.如果已經存在不會再生成.如果表有變動.自動更新表(不會刪除任何數據).
        #hibernate.hbm2ddl.auto validate    校驗.不自動生成表.每次啟動會校驗資料庫中表是否正確.校驗失敗.
         -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 引入orm元數據
            路徑書寫: 填寫src下的路徑
         -->
        <mapping resource="domain/Customer.hbm.xml" />
        
    </session-factory>
</hibernate-configuration>

 


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

-Advertisement-
Play Games
更多相關文章
  • 在前端的開發過程中,免不了進行各種調試和測試。 在不同的平臺,不同的環境下的調試方法也不盡相同,這個系列文章將探索常見的一些前端調試場景,較為系統地整理出一些調試方法。 主要包含在 PC上的 IE、FireFox、Chrome、Safari、Edge瀏覽器開發工具調試,遠程真機 安卓微信頁面、安卓常 ...
  • 一、前言 這章非常重要,由於之後需要負責平臺手機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基礎知識,將知識與項目結合,學習面向對象編程思想,最主要的是編程的邏輯練習,代碼往往不像是寫文章從上到下一氣呵成完成,中間很可能為增加一個功能來添加 ...
一周排行
    -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.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...