04-Hibernate的常見配置

來源:互聯網
上載者:User

標籤:resource   string   載入   create   hbm.xml   osi   控制   寫法   commit   

1.核心配置核心配置有兩種方式進行配置:
      •  屬性檔案的配置:

* hibernate.properties

* 格式:

* key=value

* hibernate.connection.driver_class=com.mysql.jdbc.Driver

***** 注意:沒有辦法在核心設定檔中載入對應檔.(必須手動編碼的方式進行載入.)

 

      • XML格式檔案配置:

* hibernate.cfg.xml

* 格式:

<property name="hibernate.connection.username">root</property>

 

核心配置中:
      • 1.必須的配置:

* 串連資料庫4個基本參數:

hibernate.connection.driver_class  串連資料庫驅動程式

hibernate.connection.url   串連資料庫URL

hibernate.connection.username  資料庫使用者名稱

hibernate.connection.password   資料庫密碼

 

* Hibernate的方言:

hibernate.dialect   操作資料庫方言

 

      • 2.可選的配置:

hibernate.show_sql  true 在控制台上輸出SQL語句

hibernate.format_sql  true  格式化控制台輸出的SQL語句

hibernate.connection.autocommit  true 事務是否自動認可

hibernate.hbm2ddl.auto   create/create-drop/update/validate

* create         :每次執行的時候,建立一個新的表.(如果以前有該表,將該表刪除重新建立.) 一般測試的時候的使用.

* create-drop    :每次執行的時候,建立一個新的表,程式執行結束後將這個表,刪除掉了. 一般測試的時候使用.

* update         :如果資料庫中沒有表,建立一個新的表,如果有了,直接使用這個表.可以更新表的結構.

* validate       :會使用原有的表.完成校正.校正對應檔與表中配置的欄位是否一致.不一致報錯.

 

      • 3.映射的配置:
  • 在核心設定檔中載入對應檔:

* <mapping resource="cn/itcast/hibernate3/demo1/Customer.hbm.xml" />

  • 使用手動編碼的方式進行載入 :

 

對應檔的配置:ORM:對象和關係映射.* 配置Java對象與表映射.
      • * 配置類與表的映射:

* name:類的全路徑:

* table:表的名稱:(可以省略的.使用類的名稱作為表名.)

<class name="cn.itcast.hibernate3.demo1.Order" table=”orders”>

 

      • * 配置普通屬性與欄位對應:

<property name="name" column="name" type="string" length=”20”/>

type:三種寫法

            * Java類型      :java.lang.String

            * Hibernate類型 :string

            * SQL類型       :不能直接使用type屬性,需要子標籤<column>

                * <column name="name" sql-type="varchar(20)"/>

 

      • * 配置唯一標識與主鍵映射:

* 一個表中只有一個主鍵的形式:

<id name=”id” column=”id”>

* 建置原則:

 

      • * 一個表對應多個主鍵形式:(複合主鍵:)---瞭解.

* <composite-id></composite-id>

 

      • * 關聯關係:
      • * 命名SQL:

<query name="findAll">

    from Customer

</query>

 

<sql-query name="sqlFindAll">

    select * from customer

</sql-query>

 

04-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.