Java project 中获取hibernate的Configuration的2种方式

原创
2015/12/09 17:26
阅读数 455
    方式一、通过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. 测试结果

    方式二、通过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语句,但是不能执行格式化。原先配置如下图所示:

    解决方法:去掉空格即可。
展开阅读全文
打赏
0
1 收藏
分享
加载中
更多评论
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部