Java project 中獲取hibernate的Configuration的2種方式

    方式1、經過hibernate.cfg.xml文件配置
         1. hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<!-- 數據庫連接配置 -->
		<property name="connection.username">test</property>
		<property name="connection.password">123</property>
		<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
		<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:tran</property>
		<!-- 顯示實際操做數據庫時的SQL -->
              <property name="show_sql">true</property>
              <property name="format_sql">true</property>
              <property name="use_sql_comments">true</property>
		<!-- SQL方言 -->
		<property name="dialect">org.hibernate.dialect.OracleDialect</property>
		<!-- 編碼方式 -->
		<property name="connection.characterEncoding">GBK</property>
		<!-- C3P0數據源設置 -->
		<property name="c3p0.min_size">5</property>
		<property name="c3p0.max_size">20</property>
		<property name="c3p0.timeout">1800</property>
		<property name="c3p0.max_statements">50</property>
		<!-- 自動建立數據表 -->
		<property name="hbm2ddl.auto">update</property>
		<!-- 事務相關 -->
		<property name="connection.release_mode">auto</property>
		<property name="transaction.auto_close_session">false</property>
		<property name="connection.autocommit">false</property>
		<!-- 對象與數據庫表格映像文件 -->
		<mapping resource="com/lxh/transaction3/BankAccount.hbm.xml" />
	</session-factory>
</hibernate-configuration>
         2. 應用代碼
package com.lxh.transaction3;

import java.io.File;
import org.hibernate.cfg.Configuration;

public class HibernateTransactionTest {
	// path
	private static String path = HibernateTransactionTest.class.getResource("")
			.getPath().toString();

	// hibernate.cfg.xml文件方式獲取
	public static Configuration getConfigurationByXML() {
		// 加載配置文件
		Configuration config = new Configuration();
		Configuration cfg = config.configure(new File(path
				+ "hibernate.cfg.xml"));
		return cfg;
	}

	public static void main(String[] args) {
		// Configuration
		Configuration cfg = HibernateTransactionTest.getConfigurationByXML();
		System.out.println("****cfg="+cfg+"****\n****SessionFactory="+cfg.buildSessionFactory()+"****");
	}

}
         3. 測試結果

    方式2、經過hibernate.properties文件配置
         1. hibernate.properties
hibernate.connection.username=test
hibernate.connection.password=123
hibernate.connection.url=jdbc:oracle:thin:@127.0.0.1:1521:tran
hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
hibernate.dialect=org.hibernate.dialect.OracleDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.connection.characterEncodin=GBK
hibernate.connection.release_mode=auto
hibernate.transaction.auto_close_session=false
hibernate.connection.autocommit=false
c3p0.min_size=5
c3p0.max_size=20
c3p0.timeout=1800
c3p0.max_statements=50
         2. 應用代碼
package com.lxh.transaction3;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import org.hibernate.cfg.Configuration;

public class HibernateTransactionTest {
	// path
	private static String path = HibernateTransactionTest.class.getResource("")
			.getPath().toString();

	// properties文件方式獲取
	public static Configuration getConfigurationByProp() {
		// hibernate.properties
		Properties prop = new Properties();
		try {
			prop.load(new FileReader(new File(path + "hibernate.properties")));
		} catch (IOException e) {
			System.out.println("加載hibernate配置文件失敗:\t" + e.getMessage());
		}
		// 加載配置文件
		Configuration config = new Configuration();
		Configuration cfg = config.setProperties(prop);
		cfg.addClass(BankAccount.class);// 很是重要----自動搜索BankAccount.hbm.xml
		return cfg;
	}

	public static void main(String[] args) {
		// Configuration
		Configuration cfg = HibernateTransactionTest.getConfigurationByProp();
		System.out.println("****cfg="+cfg+"****\n****SessionFactory="+cfg.buildSessionFactory()+"****");
	}

}
         3. 測試結果

   補充:BankAccount.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>
	<class name="com.lxh.transaction3.BankAccount" table="BankAccount">
		<id name="id" type="string">
			<column name="id" />
		</id>
		<!-- name字段設置惟一約束 -->
		<property name="name" type="string" unique-key="true">
			<column name="name" not-null="true" />
		</property>
		<property name="balance" type="java.lang.Integer">
			<column name="balance" />
		</property>
	</class>
</hibernate-mapping>
    說明;
    1. 須要引入數據庫驅動包以及hibernate相關jar;
    2. 使用properties文件配置hibernate時,應該採用cfg.addClass(BankAccount.class);自動加載XXX.hbm.xm文件;
    3. XXX.hbm.xml文件配置時特別注意正確書寫表名和對應POJO的路徑。

    參考:
    1. hibernate使用Properties文件方式配置  http://blog.csdn.net/xiazdong/article/details/7562765
    2. hibernate.cfg.xml配置  http://www.blogjava.net/baoyaer/articles/172642.html
    3. hibernate.cfg.xml在hibernate與spring中配置的區別  http://kewen1989.iteye.com/blog/1747156
    4. hibernate配置文件中有關事務的配置說明   http://www.blogjava.net/freeman1984/archive/2011/08/04/355808.html
    5. hibernate配置參數解釋 http://www.cnblogs.com/elleniou/archive/2012/12/01/2797546.html
    6. hibernate展現SQL語句  http://www.mkyong.com/hibernate/hibernate-display-generated-sql-to-console-show_sql-format_sql-and-use_sql_comments/
    遇到的問題
       hibernate能顯示執行的SQL語句,可是不能執行格式化。原先配置以下圖所示:

    解決方法:去掉空格便可。
相關文章
相關標籤/搜索