參考文檔:hibernate-release-5.2.9.Final/documentation/userguide/html_single/Hibernate_User_Guide.htmlhtml
1)Hibernate配置文件主要用於配置數據庫鏈接和Hibernate運行時所須要的各類屬性。java
hibernate.cfg.xml經常使用的屬性:mysql
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <property name="hibernate.connection.username">root</property> 8 <property name="hibernate.connection.password">123456</property> 9 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 10 <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_01</property> 11 12 <!-- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 13 <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> --> 14 <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> 15 16 <property name="hibernate.show_sql">true</property> 17 18 <property name="hibernate.format_sql">true</property> 19 20 <property name="hibernate.hbm2ddl.auto">update</property> 21 22 <property name="hibernate.current_session_context_class">thread</property> 23 24 <mapping resource="com/dx/hibernate5/test/News.hbm.xml" /> 25 <mapping class="com.dx.hibernate5.test.News" /> 26 </session-factory> 27 </hibernate-configuration>
2)每一個Hibernate配置文件對應一個Hibernate Configuration類對象。sql
3)配置Hibernate配置文件能夠有兩種格式:數據庫
hibernate.properties緩存
hibernate.cfg.xml(推薦使用這種配置方式)session
1)C3P0數據庫鏈接池屬性app
// 數據庫鏈接池的最大鏈接數ide
hibernate.c3p0.max_size 測試
// 數據庫鏈接池的最大鏈接數
hibernate.c3p0.min_size
// 數據庫鏈接池中鏈接對象在多長時間沒有使用事後,就應該被銷燬
hibernate.c3p0.timeout
// 緩存Statement對象的數量
hibernate.c3p0.max_statements
// 表示鏈接池檢測線程多長時間檢測一次線程池內的全部鏈接對象是否超時。
// 鏈接池自己不會把本身從鏈接池中移除,而是專門有一個線程按照必定的時間間隔來作這個事情。
// 這個線程經過比較鏈接對象最後一次被使用時間和當前時間的時間差來和timeout作對比,進而決定是否銷燬這個鏈接對象。
hibernate.c3p0.max_idle_test_period
// 當數據庫鏈接池中的鏈接耗盡時,同一時刻獲取多少個新的數據鏈接。
hibernate.c3p0.accquire.increment
示例:
<?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> <!--other settings--> 。。。 <property name="hibernate.c3p0.max_size">500</property> <property name="hibernate.c3p0.min_size">20</property> <property name="hibernate.c3p0.max_statements">10</property> <property name="hibernate.c3p0.timeout">2000</property> <property name="hibernate.c3p0.idle_test_period">2000</property> <property name="hibernate.c3p0.acquire_increment">10</property> <mapping resource="com/dx/hibernate5/test/News.hbm.xml" /> <mapping class="com.dx.hibernate5.test.News" /> </session-factory> </hibernate-configuration>
2)導入c3p0須要的jar包
下載後解壓的開發包路徑中\hibernate-release-5.2.9.Final\lib\optional\c3p0下jar包導入進工程便可。
3)代碼測試:
package com.dx.hibernate5.test; import java.sql.Connection; import java.sql.SQLException; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.Metadata; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.jdbc.Work; public class HelloWord { public static void main(String[] args) { // 一、建立一個SessionFactory對象 // 可是若是你使用Hibernate5的版本,就會報錯。那麼Hibernate5應該怎樣構建SessionFactory呢,以下: // 和V4版本比,V5版本看不到configure對象了。直接使用建立者模式構建出了標準服務註冊對象 StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure().build(); // 這個對象metadata對象應該扮演了一個萬金油的角色,使用以上的註冊對象做爲入參構建這個對象 Metadata metadata = new MetadataSources(standardRegistry).getMetadataBuilder() .applyImplicitNamingStrategy(ImplicitNamingStrategyComponentPathImpl.INSTANCE).build(); // 最後由這個metadata使用構建出sessionFactory SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build(); // 二、建立一個Session對象 Session session = sessionFactory.getCurrentSession(); // 三、開啓事物 Transaction transaction = session.beginTransaction(); // 四、查看connection對象類型是否爲c3p0 session.doWork(new Work() { @Override public void execute(Connection connection) throws SQLException { // TODO Auto-generated method stub System.out.println(connection); } }); // 五、提交事物 transaction.commit(); // 六、關閉Session對象 session.close(); // 七、關閉SessionFactory對象 sessionFactory.close(); System.out.println("Complete..."); } }
打印結果:
com.mchange.v2.c3p0.impl.NewProxyConnection@58a55449 [wrapping: com.mysql.jdbc.JDBC4Connection@7a676440]