<?xml version="1.0" encoding="GBK"?><!-- 指定Hibernate設定檔的DTD資訊 --><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><!-- hibernate- configuration是串連設定檔的根項目 --><hibernate-configuration><session-factory><!-- 指定串連資料庫所用的驅動 --><property name="connection.driver_class">com.mysql.jdbc.Driver</property><!-- 指定串連資料庫的url,hibernate串連的資料庫名 --><property name="connection.url">jdbc:mysql://localhost/hibernate</property><!-- 指定串連資料庫的使用者名稱 --><property name="connection.username">root</property><!-- 指定串連資料庫的密碼 --><property name="connection.password">32147</property><!-- 指定串連池裡最大串連數 --><property name="hibernate.c3p0.max_size">20</property><!-- 指定串連池裡最小串連數 --><property name="hibernate.c3p0.min_size">1</property><!-- 指定串連池裡串連的逾時時間長度 --><property name="hibernate.c3p0.timeout">5000</property><!-- 指定串連池裡最大緩衝多少個Statement對象 --><property name="hibernate.c3p0.max_statements">100</property><property name="hibernate.c3p0.idle_test_period">3000</property><property name="hibernate.c3p0.acquire_increment">2</property><property name="hibernate.c3p0.validate">true</property><!-- 指定資料庫方言 --><property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property><!-- 根據需要自動建立資料表 --><property name="hbm2ddl.auto">update</property><!-- 顯示Hibernate持久化操作所產生的SQL --><property name="show_sql">true</property><!-- 將SQL指令碼進行格式化後再輸出 --><property name="hibernate.format_sql">true</property><!-- 羅列所有的對應檔 --><mapping resource="org/crazyit/app/domain/News.hbm.xml"/></session-factory></hibernate-configuration>
首先hibernate.cfg.xml 一般放在src目錄下。
Hiberate內建的串連池僅有測試價值,並不推薦在實際項目中使用。實際項目中可以使用C3p0或Proxool串連池,為了使用c3p0串連池或proxool串連池,只需要在這些串連池配置代替hiberate.connection.pool_size配置屬性即可。
上面的例子使用了c3p0資料來源,因此還需要將hiberate-distribution-3.6.0.Final\lib 的optional子目錄下的c3p0目錄下的JAR包也添加到系統的類載入路徑下。