標籤:
1、配置的方式:
- hibernate.properties
- hibernate.cfg.xml
- 編碼方式建立Configuration對象
hibernate.properties 採用這種方式配置時:檔案配置時可以參考hibernate發布包中project\etc下hibernate.properties檔案,但該檔案沒有添加持久化類的方式,所以必須調用Configuration對象的addAnnotatedClass()或addPackage()添加持久化類。
Configuration cfg = new Configuration().addAnnotatedClass(Item.class)
.addAnnotatedClass(Bid.class);
實際開發中一般不用,因為當類很多時,工作量會很大。
hibernate.cfg.xml 在設定檔裡加了持久化類 Configuration cfg = new Configuration().configure(); 編程方式加(很少用) // 執行個體化Configuration,不載入任何設定檔 Configuration conf = new Configuration() // 通過addAnnotatedClass()方法添加持久化類 .addAnnotatedClass(org.crazyit.app.domain.News.class) // 通過setProperty設定Hibernate的串連屬性。 .setProperty("hibernate.connection.driver_class" , "com.mysql.jdbc.Driver") .setProperty("hibernate.hbm2ddl.auto" , "update"); 2、常用配置屬性
<!-- 指定串連資料庫所用的驅動 --> <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.MySQL5InnoDBDialect</property> <!-- 根據需要自動建立資料表 --> <property name="hbm2ddl.auto">update</property><!--①--> <!-- 顯示Hibernate持久化操作所產生的SQL --> <property name="show_sql">true</property> <!-- 將SQL指令碼進行格式化後再輸出 --> <property name="hibernate.format_sql">true</property> <!-- 羅列所有持久化類的類名 --> <mapping class="org.crazyit.app.domain.News"/>
View Code
hibernate設定檔