1. 程式人生 > >hibernate框架學習筆記2:配置文件詳解

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

格式化 version validate ret root 主鍵生成策略 -m color 格式

實體類:

技術分享圖片
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>

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