hibernate設定檔範本

來源:互聯網
上載者:User

標籤:utf-8   不能   mys   config   ide   user   jdbc   tab   column   

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>          <!-- 啟動Hibernate時,重建資料庫 create;更新資料庫 update -->        <property name="hbm2ddl.auto">update</property>        <!--顯示執行的SQL語句-->        <property name="show_sql">true</property>        <!-- 資料庫方言 -->        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>                <!-- driver url username password -->        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>          <property name="connection.url">jdbc:mysql://localhost:3306/test_db?useUnicode=true&amp;characterEncoding=UTF8&amp;useSSL=false</property>        <property name="connection.username">root</property>        <property name="connection.password">Cs123456</property>                <!-- JDBC串連池(使用內建的串連池)-->        <property name="connection.pool_size">5</property>        <!-- 每次從資料庫中取出,並放到JDBC的Statment中的記錄條數,fetch_size設的越大,讀取資料庫的次數會越小,速度越快,但消耗更多記憶體 -->        <property name="jdbc.fetch_size">50</property>        <!-- 批量插入、刪除和更新時,每次操作的記錄數,batch_size越大,向資料庫發送SQL語句的次數越少,速度越快,但消耗更多記憶體 -->        <property name="jdbc.batch_size">30</property>        <!-- Hibernate自動管理內容相關的策略 -->        <property name="current_session_context_class">thread</property>        <!-- 配置mapping -->        <!-- (1)hbm.xml 對應檔配置 -->        <mapping resource="com/uzipi/entity/Student.hbm.xml" />        <mapping resource="com/uzipi/entity/Course.hbm.xml" />        <!-- (2)持久化類上的註解方式 -->        <mapping class="com.uzipi.entity.TClass" />      </session-factory>  </hibernate-configuration> 

 

 

 

hibernate.cfg.xml 中配置的<mapping>對應對應檔,resource屬性值需要填寫具體的路徑,比如:放在com.uzipi.entity包下的Student.hbm.xml,配成:

<mapping resource="com/uzipi/entity/Student.hbm.xml" />

如果是通過註解方式配置映射,在 hibernate.cfg.xml 檔案中配置的<mapping>元素的屬性是class ,填寫類的全限定類名:
<mapping class="com.uzipi.entity.TClass" />

 



XXX.hbm.xml 對應檔模版:

<?xml version="1.0" encoding="UTF-8"?>  <!DOCTYPE hibernate-mapping PUBLIC            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  <hibernate-mapping package="entity">    <class name="com.uzipi.entity.Student" table="t_student">                <id name="id" column="stu_id" type="int" unique="true"> <!-- 持久化類的唯一標識 -->            <generator class="native" />            <!-- <generator class="increment" /> -->        </id>                <property name="name" column="stu_name" type="string" not-null="true" length="200" />        <property name="sex" type="string" length="10"> <!-- 另一種映射欄位的方式 -->            <column name="stu_sex"/>        </property>        <property name="age" column="stu_age" type="int" not-null="true" length="11" />        <property name="classbj" column="class_id" type="string" length="200" />            </class></hibernate-mapping>

 <property>屬性元素有兩種配置方式:成對、單個。

成對配置方式需要在<property>元素內配置一個<column name="xxx" />元素。

 

如果對應檔中沒有配置 column 和 type 屬性,Hibernate 將會預設使用持久化類中的屬性名稱和屬性類型匹配資料庫表中的欄位。

 

<id>元素的子項目 <generator> 用來為持久化類的執行個體產生唯一的標識,映射資料庫中的主鍵欄位,class屬性的常用配置值:

increment:用於為Long、Short、Int類型產生唯一標識。叢集下不要使用該屬性

identity:由支援自增欄位類型的資料庫底層實現,提供產生主鍵的功能(比如:MySQL)

sequence:由支援序列的資料庫底層實現,提供產生主鍵的功能(比如:Oracle)

native:由 Hibernate 自動識別資料庫底層實現,自動選擇identity、sequence(一般配置該屬性)

assigned:由 Hibernate 程式負責主鍵的產生,此時持久化類的唯一標識不能聲明為 private 類型

select:通過資料庫觸發器產生主鍵

foreign:使用另一個相關聯的對象的標識符,通常和<one-to-one>一起使用

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.