1. 程式人生 > >Hibernate的hibername.cfg.xml及Xxx.hbm.xml配置檔案

Hibernate的hibername.cfg.xml及Xxx.hbm.xml配置檔案

目錄

一、檔案描述

    下方是Hibernate的兩個配置檔案hibername.cfg.xml及Xxx.hbm.xml,hibername.cfg.xml是主配置檔案,Xxx.hbm.xml是對映檔案,大家可以直接複製到工程中,修改相應資料即可

二、檔案程式碼

    1.hibername.cfg.xml主配置檔案

  • Oracle資料庫hibername.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>
        <!-- Oracle 10g/11g方言 -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <!-- Oracle URL -->
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:資料庫</property>
         <!-- Oracle 驅動 -->
        <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="connection.username">資料庫使用者名稱</property>
        <property name="connection.password">資料庫密碼</property>
        
        <!-- 載入XML對映描述資訊 -->
        <mapping resource="Xxx.hbm.xml" />
    </session-factory>
</hibernate-configuration>
  • MySQL資料庫hibername.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>
        <!-- MySQL方言 -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- MySQL URL -->
        <property name="connection.url">jdbc:mysql://localhost:3306/資料庫</property>
        <!-- MySQL 驅動 -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">資料庫使用者名稱</property>
        <property name="connection.password">資料庫密碼</property>
        
        <!-- 載入XML對映描述資訊 -->
        <mapping resource="Person.hbm.xml" />
    </session-factory>
</hibernate-configuration>

    2.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="實體類物件所在包.類" table="表名">
        <!-- 關聯主鍵 -->
        <id name="id" column="ID"></id>
        <!-- 關聯欄位 -->
        <property name="實體類屬性欄位名" column="資料表對應欄位名"></property>
    </class>
</hibernate-mapping>